Download Queue Implementation: Linear Array and Linked List and more Lecture notes Algorithms and Programming in PDF only on Docsity!
QUEUE
DATA STRUCTURE AND ALGORITHMS
Real-World Applications Computer Science Applications Recognizing palindromes
OBJECTIVES FOR STUDENTS
- Understand the queue concept and the purpose of queuing operation.
- Understand the implementation of basic queuing algorithm.
- Able to implement queuing technique in problem solving using array and linked list. KEY CONCEPT 1.0 INTRODUCTION TO QUEUE
2.0 QUEUE : LINEAR ARRAY IMPLEMENTATION
2.1 Queue : Linear Array Implementation Queue front rear items createQueue() destroyQueue() isEmpty(); isFull(); enQueue(); deQueue(); getFront(); getRear();
- Number of elements in Queue are fixed during declaration.
- Need isFull() operation to determine whether a queue is full or not.
- Queue structure need at least 3 elements: o Element to store items in Queue o Element to store index at head o Element to store index at rear 2.2 Queue declaration
0 1 2 3 …. maxQ- 1 2.3 createQueue() operation
- Linear Array implementation o front & back are indexes in the array o Initial condition: front =0 & back = - 1 1 // Program 9. Queue::Queue() { front = 0; back = - 1; }
Initial state for a queue linear array 2.4 Queue operations
- Destructor – To destroy Queue o All elements in the queue will be disposed. 1 // Program 9. queue::~queue() { delete [ ] items; }
- isEmpty()- Check whether a queue is empty o Queue is empty when: back < front 1 // Program 9. bool queue::isEmpty() { return bool(back < front); }
- isFull()- Check whether a queue is full o Queue is full when : back = size - 1 o No more item can be insert into a queue, when a queue is full
1 // Program 9. char queue:: getFront() // get item at front { return items[front] ; }
- getRear() – Retrieve item at back of the queue. o Statement to access item at front or back of the queue are as follows: cout << “Item at front queue: “ << myQueue.getFront(); cout << “Item at the back queue:” << myQueue.getRear();
- deQueue()- delete from a queue o Retrieve item at front queue (if necessary) o Increment front // Program 9. 1 void queue::deQueue() 2 { if (isEmpty()) 3 cout<<"\nCannot remove item.Empty Queue!"; 4 else 5 { //retrieve item at front 6 deletedItem = items[front]; 7 front++; 8 } // end else if 9 } o deQueue() example 1 // Program 9. char queue::getRear() // get item at back { return items[back] ; }
2.5 Linear Queue Array Problem: Rightward-Drifting
- After a sequence of additions and removals, items in the queue will drift towards the end of the array. enQueue operation cannot be performed on the queue as shown below, since back = max_queue – 1. 2.6 Rightward drift solutions
- Two solutions can be performed to solve rightward drift
- Shift array elements after each deletion to occupy the space being deleted at front o However, shifting dominates the cost of the implementation
- Use a circular array: When front or back reach the end of the array, wrap them around to the beginning of the array. o However there is a problem o front and back cannot be used as condition to distinguish between queue-full and queue-empty o Therefore, use counter as a solution: o count == 0 means empty queue o count == MAX_QUEUE means full queue
- Deletion operation - deQueue() o Increment front using modulo arithmetic o Decrement count 1 //Program 9. 2 front = ( front + 1 ) % MAX_QUEUE; 3 --count;
- Disadvantage o The overhead of maintaining a counter or flag o Queue Operation examples: o front passes back when the queue becomes empty; o back catches up to front t when the queue becomes full o The figure below shows the enQueue() and deQueue process implemented in circular array Figure extracted from Carrano, F (2007)
4.0 QUEUE IMPLEMENTATION LINKED LIST
4.1 Pointer-Based Implementation
- More straightforward than array-based
- There are 2 possible options, which are: o Linear linked list § Have two external pointers front and back that are used to point to the first node in the queue and point to the last node in the queue h a b i t front (^) back o Circular linked list § Have one external pointer, back that point to the last node of the queue. 4.2 Queue Linear Linked List
- Need 2 structures, which are declaration of class node and class queue o Declaration of the node with one data item 1 //Program 9. 2 struct nodeQ { 3 char item; 4 nodeQ * next; 5 } o Declaration of the queue which has pointers, backPtr and frontPtr 1 2 3 4 //Program 9. *class queue {public: nodeQ backPtr, * frontPtr;
- Insertion into a queue implementation linear linked list o Create a new node, newPtr o Insertion to an empty queue is as follows 1 //Program 9. 2 frontPtr = backPtr = newPtr ; o Insertion to a non-empty queue is as follows 1 //Program 9. 2 newPtr - > next = NULL; 3 backPtr - > next = newPtr ; 4 backPtr = newPtr ;
- Deletion o Delete from the front of the queue o Only one pointer change is needed, frontPtr o Special case: § If the queue contains one item only, both backPtr and frontPtr will be set to NULL o Deletion code for a queue with more than one node. 1 //Program 9. 2 tempPtr = frontPtr 3 frontPtr = frontPtr - > next
tempPtr - > next = NULL delete tempPtr ; o If the queue contains one item only, need to add this statement 1 //Program 9. 2 if (!frontPtr) 3 backPtr = NULL; 5.2 Circular linear linked list with one external pointer
- Insertion operation o Insert into an empty queue 1 //Program 9. 2 // the node point to itself 3 NewPtr - > Next = NewPtr 4 Back pointer point to the newNode 5 backPtr = NewPtr o Insert into a non-empty queue 1 //Program 9. 2 NewPtr - > Next = backPtr-> Next 3 backPtr - > Next = NewPtr 4 backPtr = NewPtr
§ More efficient in insert and delete operations § More complicated than Abstract Data Type (ADT) list § Most flexible, since no size restrictions
- Array-Based o No overhead of pointer manipulation o Prevents adding elements if the array is full 5.4 Summary of Position Oriented ADTs
- Stacks : o Operations are defined in terms of position of data items o Position is restricted to the Top of the stack. Only one end position can be accessed o Operations: § Create(): Creates an empty ADT of the Stack type § isEmpty(): Determines whether an item exists in the ADT § push(): Inserts a new item into the top position § pop(): Deletes an item from the top position § satckTop(): Retrieves the item from the top position
- Queues : o Operations are defined in terms of position of data items o Position is restricted to the front and the back of the queue. Only the end positions can be accessed. o Operations: § create (): Creates an empty ADT of the Queue type § isEmpty(): Determines whether an item exists in the ADT § dequeue(): Deletes an item from the front position § enqueue(): Inserts a new item in the back position § peek(): Retrieves the item from the front position
- Stacks and queues are very similar in terms of : o Operations of stacks and queues can be paired off as § createStack() and createQueue() § Stack isEmpty() and queue isEmpty() § Push() and enqueue() § Pop() and dequeue() § getTop() and queue getFront()