1 / 46

Stacks & Queues Using Arrays

Stacks & Queues Using Arrays. Stack. A stack is a data structure in which all insertions and deletions of entries are made at one end , called the top of the stack. The last entry which is inserted is the first one that will be removed. ( Last In First Out : LIFO ). Application of Stack.

Download Presentation

Stacks & Queues Using Arrays

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. Stacks & QueuesUsing Arrays

  2. Stack • A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. • The last entry which is inserted is the first one that will be removed. (Last In First Out : LIFO)

  3. Application of Stack • Page visited history in a Web-browser • Undo sequence in a text editor • Program run-time environment

  4. Pushing and popping a stack • Push box A onto the stack • Push box B onto the stack • Pop a box from the stack • Pop a box from the stack • Push box C onto the stack • Push box D onto the stack • Push box E onto the stack • Push box F onto the stack

  5. Implementation of Stack • Some Basic Operations for Stacks • Create a stack, leaving it empty. • Test whether the stack is Empty. • Retrieve the Top entry from the stack, provided the stack is not empty. • Pop the entry of the top of the stack, provided the stack is not empty. • Push a new entry onto the top off the stack, provided that the stack is not full.

  6. Basic Operation

  7. Basic Operation

  8. Basic Operations for Stacks • Create a stack, leaving it empty. • Test whether the stack is Empty. • Test whether the stack is Full. • Return the Size (number of entries) of the stack. • Retrieve the Top entry from the stack, provided the stack is not empty. • Pop the entry off the top of the stack, provided the stack is not empty. • Push a new entry onto the top of the stack, provided that the stack is not full. • Print all the entries of the stack.

  9. The Stack Class • Methods: • Stack (constructor) • empty • full • size • top • pop • push • print • Data members

  10. Stack Implementation - Array typedef double Stack_entry; const int max = 100; class Stack { public: Stack(); bool empty(); bool full(); int size(); bool top(Stack_entry &item); bool pop(); bool push(Stack_entry item); void print(); private: int count; Stack_entry entry[max]; };

  11. Data Structure - Array

  12. Create Stack • We use a constructor to initialize a new created stack as empty. Stack::Stack() // O(1) // Initialize Stack to be empty { count = 0; }

  13. Empty bool Stack::empty() // A result of true is returned if the Stack // is empty, otherwise false is returned. { return count == 0; }

  14. Full bool Stack::full() // A result of true is returned if the Stack // is full, otherwise false is returned. { return count == max; }

  15. Size int Stack::size() // return the # of entries in the stack. { return count; }

  16. Top bool Stack::top(Stack_entry &item) // The top of a nonempty Stack is copied to // item. A code of underflow is returned if the // Stack is empty. { if(empty()) return false; item = entry[counter-1]; return true; }

  17. Pop bool Stack::pop() // If the Stack is not empty, the top of the // Stack is removed. If the Stack is empty, an // Error code of underflow is returned and the // Stack is left unchanged. { if(empty()) return false; count--; return true; }

  18. Push bool Stack::push(Stack_entry item) // If the Stack is not full, item is added // to the top of the Stack. If the Stack is full, // an Error code of overflow is returned and the // Stack is left unchanged. { if(full()) return false; entry[count++] = item; return true; }

  19. Print void Stack::print() // Display the entries of the Stack. { if (empty()) cout << "Empty stack"; else{ cout << endl << "The Stack is: "; for (int i=count-1; i>=0; i--) cout << entry[i] << " "; } cout << endl; }

  20. Queues

  21. Queue • A data structure modeled after a line of people waiting to be served. • The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO)

  22. The Queue Class • Functions: • Queue (constructor) • clear • empty • full • size • retrieve • dequeue • enqueue • print • Data members

  23. Queue Implementation - Array typedef double Queue_entry; const int max = 100; class Queue { public: Queue(); void clear(); bool empty(); bool full(); int size(); bool retrieve(Queue_entry &item); bool dequeue(); bool enqueue(Queue_entry item); void print(); private: int count; Queue_entry entry[max]; };

  24. Data Structure - Array

  25. Create Queue • We use a constructor to initialize a new created queue as empty. Queue::Queue() // O(1) { count = 0; }

  26. Clear Queue::clear() // O(1) { count = 0; }

  27. Empty bool Queue::empty() { return count == 0; }

  28. Full bool Queue::full() { return count == max; }

  29. Size int Queue::size() { return count; }

  30. Retrieve bool Queue::retrieve(Queue_entry &item) { if(empty()) return false; item = entry[0]; return true; }

  31. Dequeue bool Queue::dequeue() // O(n) { if (empty()) return false; count--; for(int i = 0; i < count; i++) entry[i] = entry[i+1]; return true; }

  32. Enqueue bool Queue::enqueue(Queue_entry item) // O(1) { if(full()) return false; entry[count] = item; count++; return true; }

  33. Print void Queue::print() { for (int i=0; i<count; i++) cout << entry[i] << " "; }

  34. Data Structure – Circular Array

  35. Data Structure – Circular Array

  36. Circular Queue

  37. Create Queue Queue::Queue() { front = 0; rear = -1; }

  38. Clear Queue::clear() { rear = -1; }

  39. Empty bool Queue::empty() { return rear == -1; }

  40. Next int Queue::next(int n) { return ((n+1)==max) ? 0 : (n+1); }

  41. Full bool Queue::full() { return (rear!=-1) && (next(rear)==front); }

  42. Size int Queue::size() { int s; if (empty()) s = 0; else{ s = rear-front+1; if (front>rear) s = max + s; } return s; }

  43. Retrieve bool Queue::retrieve(Queue_entry &item) { if (empty()) return false; item = entry[front]; return true; }

  44. Dequeue bool Queue::dequeue() { if (empty()) return false; if (front==rear) // one element rear = -1; else front = next(front); return true; }

  45. Enqueue bool Queue::enqueue(Queue_entry item) { if (full()) return false; if (empty()) rear = front; else rear = next(rear); entry[rear] = item; return true; }

  46. Print void Queue::print() { if (empty()) cout << "The queue is empty" << endl; else{ cout << endl << "The Queue is: "; int i = front; do{ cout << entry[i] << " "; i = next(i); } while (i != next(rear)); } cout << endl; }

More Related