1 / 19

Queue Implementation In Linked List | Data Structures And Algorithms Tutorial |

This presentation is based on Queue Implementation In Linked List. It will help you understand the implementation details of a linear queue in the data structure. This tutorial will acquaint you with the C program for Implementing Queue Using a linked list. The topics covered in this slide are:<br><br>1. Introduction<br>2. Queue Functionalities: Quick Recap<br>3. What Is the Need for Linked List Implementation of the Queue?<br>4. Queue Representation Using Linked List<br>5. The Challenge while Implementing Queue<br>6. Coding Implementation of Queue Data Structure

Simplilearn
Download Presentation

Queue Implementation In Linked List | Data Structures And Algorithms Tutorial |

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s in It for You? Queue Functionalities What Is the Need for Linked List Implementation of the Queue? Queue Representation Using Linked List The Challenge While Implementing Queue Using Linked Lists Coding Implementation of Queue Data Structure

  2. Queue Functionalities Queue is a linear data structure which follows First-Come-First-Serve approach while performing insertion and deletion. For example- Customer Service Queue. First to Enter, First to Leave!

  3. Click here to watch the video

  4. Queue in Data Structure Queue Functionalities Insertion is performed at Front end where Deletion is Performed at Rear end. Dequeue() Enqueue() REAR (Insertion) FRONT (Deletion)

  5. Queue in Data Structure Time-Complexity = O(1) Enqueue() Dequeue() Peek() isFull() isEmpty() TIME All these operations must take constant execution time. O(1) 1 N

  6. What Is the Need for Linked List Implementation of the Queue? • Limitation of Fixed Size in Queue Implementation Using Array: • Declaration of an array can only be done at compile time (Before execution of program). • When the array is completely filled we cannot perform further insertions of data-elements. • In this case, the only left options are: • 1. Limiting Enqueue operation • 2. Storing exhausted array into new larger array

  7. Storing Exhausted Array in Larger Array Queue Functionalities Time complexity for this copy operation will be O(N). i.e. Time taken No. of Elements in Filled array This is not an ideal solution to this problem as 70% memory space of larger array is waisted. Filled Array New Larger Array

  8. Memory Wastage In array implementation of queue, the memory space left empty after dequeue operation cannot be reutilized. Deleted DeletedDeleted space spacespace 7 3 Size of Array = 10 Empty EmptyEmptyEmptyEmpty 5 -3 6 0 1 Front Front 0 1 2 3 4 5 6 7 8 9 50% Memory space is wasted! The optimal solution – Use of dynamic data structure instead of static

  9. Queue Representation Using Linked List • Linked list is linear collection of data elements having non-contiguous memory allocation (Dynamic nature). • Linked list is made up of Nodes. Where each node is comprised of two fields: Data Field and Reference Field. • Structure of Linked list: Head Tail START 400 500 200

  10. Linked List Representation of Queue Linked List Queue Tail Head START Add 1 Add 2 Address Front Rear Front Rear All operations must take a constant execution time independent of number of elements and end at which they are operating. Execution time = O(1)

  11. The Challenge While Implementing Queue Using Linked List • The cost of insertion and deletion at the head of linked list is O(1). But, the cost of both the operation at tail of linked list is O(N). • That means, for queue implementation of linked list we will have O(N) cost for one operation. • Which violates the primary requirement of queue implementation, that all operations must take O(1) execution time.

  12. Challenge in Linked List Implementation of Queue Let’s understand and resolve this problem with the help of an example. Head = 100 Tail START 350 Temp Rear Front 200 400 100 350 New Node

  13. Solution to Dodge the Challenge It is clear that the pointer incrementation from head node to tail node is responsible for the time complexity O(N). temp = temp -> next To avoid this traversal, we should keep track of both head and tail node using pointers. Rear Front Head Tail START Add 1 Add 2 Address

  14. Solution to Dodge the Challenge Head = 100 Tail START 350 200 400 100 Rear Front 350 New Node

  15. Coding Implementation Let’s now formulate the code for implementation of queue using linked list!

  16. void Enqueue(int x) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data =x; temp->next = NULL; if(front == NULL && rear == NULL){ front = rear = temp; return; } rear->next = temp; rear = temp; } 1. Enqueue(2) Front Rear NULL NULL 100 100 Temp 100

  17. Enqueue(2) 2. Enqueue(4) void Enqueue(int x) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data =x; temp->next = NULL; if(front == NULL && rear == NULL){ front = rear = temp; return; } rear->next = temp; rear = temp; } Front Rear 100 100 200 200 200 100

More Related