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

# Singly linked list the first node in the list

• Notes
• 16

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)
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)
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 )
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
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
Add a Tail Pointer enqueue() is O (1), front() is now O (1) (Why?) dequeue() is still O ( n ) (Why?)
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
Efficient Implementation

#### 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

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern