Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)

Similar presentations


Presentation on theme: "Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)"— Presentation transcript:

1 Lecture 20 Stacks and Queues

2 Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack) – very useful for backtracking algorithms – Heavily used in compiler design Queues – first-in-first-out structure – used in resource sharing applications Priority Queues – queue with special priority allocation

3 Stack Operations Stack is a restricted data structure – data can only be added through the top (push) – data can only be removed from the top (pop) Stack Data and Methods – container to hold the values of the stack linked list vector/array – push(value) - pushes the value into the stack (if not full) – pop( ) - pops the top value of the stack (if not empty) – top( ) - returns the top value of the stack – empty() - returns true if empty – full() - returns true if full – clear() - clear the stack

4 Class Node class Node { int data; Node next; Node(int i, Node link) { data=I; next=link;} };

5 Linked List Implementation of Stack class Stack { public: // default constructor Stack () {Top= NULL ; size=0;} // accessors boolean Empty(); boolean Full() ; int TopOfStack() ; // returns the top value of the stack int SizeOfStack() ; // returns the size of the stack // mutators void Push(int x); // push a value to the top of the stack boolean Pop(int x); // remove the most recent item void Clear(); // clear the entire stack private: Node Top; // is a pointer to the top element of the stack int size; };

6 Stack Method Implementations boolean Empty() { if (Top == null) return true; else return false; } Boolean Full() { if (size == MaxSize) return true; else return false; }

7 Stack Method Implementations ctd.. int TopOfStack() { if (!Empty()) return Top.getData(); else System.out.println(“Stack is empty “) ; } Boolean SizeOfStack() { return size; }

8 Stack Method Implementations ctd.. int Push( int n) { if (!Full()) Top = new Node(n, Top); // newnode->next = Top; Top = newnode; size++; else System.out.println( “ The Stack is full “); } boolean Pop(int x) { if (!Empty()){ x = Top.getData(); Node temp = Top; Top = Top.getNext(); delete temp; size--; return true; } else return false; } x temp Top

9 Stack Method Implementations ctd.. Void clear( ) { Node temp = Top; while (Top ! = null) { Top = Top.getNext(); delete temp; temp = Top; } size = 0; } top temp

10 Queues Behavior – add item only from the back (enqueue) – remove items only from the front (dequeue) Many Applications – printer queue – network queues – common resource sharing Queue Operations – enqueue – dequeue – clear – empty – full

11 Queue Implementation class Queue { public: Queue () {Front=null; Back=null;size=0;} // accessors boolean Empty() ; boolean Full() ; int BackOfQueue() ; // returns the back value of the queue int FrontOfQueue() ; // returns the front value of the queue // mutators void Enqueue( int x); // add a value to the end of the queue boolean Dequeue(); // remove a value from the beginning of the queue void Clear(); // clear the entire queue private: Node Back; // is a pointer to the front element of the queue Node Front; // is a pointer to the back element of the stack int size; // holds the size of the queue };

12 Queue Method Implementation boolean Empty() { if (size==0) return true; else return false; } boolean Full() const { if (size == MaxSize) return true; else return false; } int BackOfQueue() { if (!Empty()) return Back.getData(); else System.out.println( “Queue is Empty “); }

13 Queue Method Implementation int FrontOfQueue() { if (!Empty()) return Front.getData(); else System.out.printl( “Queue is Empty “); } void Enqueue(const int &x){ if (!Full()) Node temp = new Node(x); Back.getNext() = temp; Back = temp; else cerr << “Queue is Full “ << endl; } void Queue:: Dequeue(){ if (!Empty()) { Node temp = Front; Front = Front.getNext(); delete temp; }

14 Queue Method Implementation Void clear( ) { Node temp = Front; while (Front ! = null) { delete temp; Front = Front.getNext(); temp = Front; } Back = null; size = 0; }


Download ppt "Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)"

Similar presentations


Ads by Google