Stacks and Queues.

Slides:



Advertisements
Similar presentations
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Advertisements

Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Computer Engineering Rabie A. Ramadan Lecture 6.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Linked Data Structures
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
September 29 – Stacks and queues
12 C Data Structures.
Chapter 15 Lists Objectives
CS 1114: Implementing Search
Dr. Bernard Chen Ph.D. University of Central Arkansas
CISC181 Introduction to Computer Science Dr
C++ Plus Data Structures
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Queues.
Lesson Objectives Aims
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Stacks and Queues CSE 373 Data Structures.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Linked Lists.
Dynamic allocation (continued)
CSE 373 Data Structures Lecture 6
CSCS-200 Data Structure and Algorithms
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

Stacks and Queues

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

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).

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

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

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

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

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).

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

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

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.

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

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.

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

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.

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

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; }

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; };

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.

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.

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.

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]

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;

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 ""

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.

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.

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;

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]

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.

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.

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.

Vocabulary Term Definition Stack A linear-list Data structure where addition of elements to and removal of elements from (the list) are restricted to the first element of the list. Top First element of a Stack. Push Stack Operation: add an element to the top. Pop Stack Operation: remove an element from the top. LIFO Order of adding/removing items to/from a Stack. Last In, First Out Queue A linear-list Data structure where addition of elements is restricted to the end of the list, and removal of elements is restricted to the beginning of the list. Front First element of a queue, the next to be removed. Rear Last element of a queue, the last to be added. Enqueue Queue Operation: add an element to the rear. Dequeue Queue Operation: remove an element from the front. FIFO Order of adding/removing items to/from a Queue. First In, First Out