Singly Linked List The first node in the list represents the front of the queue

Singly linked list the first node in the list

This preview shows page 6 - 14 out of 16 pages.

Singly Linked List The first node in the list represents the front of the queue 10 20 30 front dequeue() removes the first node, and is O (1)
Image of page 6
Singly Linked List The first node in the list represents the front of the queue 10 20 30 front front() retrieves the value in the first node, and is O (1)
Image of page 7
Singly Linked List The last node in the list represents the rear of the queue 10 20 30 front enqueue() adds a node after the current last node, and is O ( n )
Image of page 8
Another Implementation What if we change things, so that the first node in the list represents the rear of the queue? enqueue() is now O (1) (Why?) • But, front() and dequeue() are now O ( n ) (Why?) 10 20 30 rear front() , dequeue() deal with the last node enqueue() adds a node ahead of the current first node
Image of page 9
Add a Tail Pointer rear points to the first node in the list, which represents the rear of the queue; front points to the last node, which represents the front of the queue 10 20 30 rear front enqueue() adds a node ahead of the current first node front() , dequeue() deal with the last node
Image of page 10
Add a Tail Pointer enqueue() is O (1), front() is now O (1) (Why?) dequeue() is still O ( n ) (Why?)
Image of page 11
Efficient Implementation Change things again, so that the node referred to by head (the first node in the list) represents the front of the queue front() and dequeue() are O (1) (Why?) enqueue() is also O (1) (Why?) 10 20 30 front rear enqueue() adds a node after the current last node front(), dequeue() deal with the first node
Image of page 12
Efficient Implementation
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 16 pages?

  • Spring '12
  • schram
  • Array data structure

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors