1 / 32

Stacks and Queues

Learn about the definition, operations, and examples of stacks and queues in data structures. Understand their implementation using linear data structures like arrays, vectors, and linked lists.

pando
Download Presentation

Stacks and Queues

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 and Queues

  2. Stack Definition: A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list. Top - the first element of the list

  3. Stack Operations: Push - add a new element to (the Top of) the stack. Pop - remove an element from (the Top of) the stack (and return it).

  4. Stack Examples: the stack (of ints) starts Empty push(1) push(2) push(3) pop() - returns 3 pop() - returns 2 pop() - returns 1

  5. Stack LIFO: Last In, First Out Describes the order that elements are added to and removed from a Stack.

  6. Stack Uses: - Automatic Memory allocation in a computer program. - Editing: allows Undo/Redo - Web Browsing: allows Back/Forward - Compilers: used for syntax checking

  7. Queue Definition: A linear-list Data structure where - addition of elements is restricted to the end (rear) of the list. - removal of elements is restricted to the beginning (front) of the list. Front: first element in the queue Rear: last element in the queue

  8. Queue Operations: Enqueue - add a new element to (the Rear of) the Queue. Dequeue- remove an element from (the Front of) the Queue (and return it).

  9. Queue Examples: the Queue (of ints) starts Empty enQ(1) enQ(2) enQ(3) deQ() - returns 1 deQ() - returns 2 deQ() - returns 3

  10. Queue FIFO: First In, First Out Describes the order that elements are added to and removed from a Queue.

  11. Queue Uses: - Web Server: processing service requests - Cell Tower: processing data packets from cell phones. - OS Scheduling: sharing of a single CPU by multiple "running" programs. - Fast Food Drive-Through order processing.

  12. Implementation Stacks and Queues may be implemented in a program using any Linear Data structure, including: - Arrays - Vectors - Linked Lists

  13. Dynamic Node For a Linked List (dynamic) implementation, the node class would be: class node { friend class stack; // or queue private: node * next = NULL; string data = ""; }; The node may contain any number of data members needed. Here, there is just one, a string.

  14. Dynamic Stack: Push Push: After allocating and populating a new node (pointed to by n), we make the new node the new top. Left: general case Right: empty stack case blue: before Pushing red: after Pushing

  15. Dynamic Stack: Push void stack::push(string newdata) { node * n = new node; // allocate n->data = newdata; // populate n->next = top; // connect new node // to old top. top = n; // new node is the } // new top.

  16. Dynamic Stack: Pop Pop: Remove the top node and deallocate it. Return the data from the popped node.

  17. Dynamic Stack: Pop string stack::pop() { string oldData = top->data; //save node * n = top; // pnt to old top top = top->next; // pnt top to next delete n; // dealloc old top return oldData; }

  18. Dynamic Queue For a Linked List, the Q class would be: class queue { public: void enQ(string newdata); string deQ(); private: node * front = NULL; node * rear = NULL; };

  19. Dynamic Queue There are 3 cases we should check for enqueue and dequeue: 1. empty queue 2. queue of one node 3. queue of two or more nodes The diagrams in the following slides show the new node already allocated and populated.

  20. Dynamic Queue: enQ EnQ: Empty When front and rear are NULL, the queue is empty. Just make both front and rear point to the new node.

  21. Dynamic Queue: enQ EnQ: 1 node in queue When front and rear both point to the same node, there is only one node in the queue. No change to front, just attach the new node after the current rear and make it the new rear.

  22. Dynamic Queue: enQ EnQ: 2 or more nodes When front and rear point to different nodes, there are 2 or more nodes in the queue. No change to front, just attach the new node after the current rear and make it the new rear. [same action as for queue of 1 node, so these two cases can be combined for enqueue]

  23. Dynamic Queue: enQ void queue::enQ(string newdata) { node * n = new node; // allocate n->data = newdata; // populate if (front == NULL) { // empty Q front = n; rear = n; } else { // Q of 1 or more rear->next = n; rear = n; }

  24. Dynamic Queue: deQ DeQ: empty queue When front and rear are NULL, the queue is empty. There is nothing to remove and return, so just return some data that means "none", such as Empty String ""

  25. Dynamic Queue: deQ DeQ: queue of one node Save a copy of the data in the front node. Deallocate the one node. Set both front and rear to NULL to make it empty. Return the data.

  26. Dynamic Queue: deQ DeQ: 2 or more nodes Save a copy of the data in the front node. Make front point to the next node. Deallocate the old front. Return the data.

  27. Dynamic Queue: deQ string queue::deQ() { if (front==NULL) return ""; // empty Q case node * n = front; // ptr to old front string oldData = n->data; // save for return if (front==rear) { // Q of 1 node front = NULL; // make the queue rear = NULL; // empty. } else // 2 or more nodes front = front->next; // move front to next delete n; // dealloc old front return oldData; }

  28. Design Decisions Normally: We want to write methods that are flexible and "do only one job." The four methods written above do: - allocate, populate and insert [push and enQ] OR - save return data, remove, deallocate, return data. [pop and deQ]

  29. Design Decisions In many cases, we need to Push/Enqueue an already allocated and populated node; and we may need to use a Popped/Dequeued node elsewhere instead of destroying it.

  30. Design Decisions So, it would have been better to split these into at least two methods: - one that only manipulates pointers - another that allocates/deallocates and populates (or saves/returns data), that invokes the first method.

  31. Design Decisions void stack::push(node * n) { // ptr to node already alloc/pop n->next = top; top = n; } void stack::push(string newdata) { node * n = new node; // allocate n->data = newdata; // populate push(n); // put on stack } Now programmers have a choice: Just give data, and have it allocated/populated/pushed; OR, I already have a node with data populated...just push it.

  32. Vocabulary

More Related