Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
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.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
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,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CE 221 Data Structures and Algorithms
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 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.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Introduction to Data Structure
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
CSC 143 Queues [Chapter 7].
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
Abstract Data Type Abstract Data Type as a design tool
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Stacks, Queues, and Deques
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Stacks and Queues

2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you create them Using arrays Using pointers What are the basic operations

3 Differences to Lists The common uses The way elements are accessed The degree of abstraction

4 Differences to Lists : Common Uses A programmers tool A conceptual aid, rather than a full fledged data storage object. They are created with a specific task in mind.  Modelling physical situations Queues: Shop checkouts, motorway congestion, information processing of internet traffic. Stacks: Games, problems solving, compiler and operating system programming (function control is done using stacks. Calculators (parsing/analysing expressions).

5 Differences to Arrays : Element Access A Lists allows access to any element directly via an index, this is sometimes called random access. Stack and Queues by definition have restricted access, only one particular element can be read or removed at a time. Access to other elements is not allowed (in theory anyway).

6 Differences to Arrays : More Abstract The Abstract Data Type specification defines the list of legitimate operations, not C++. For example the underlying mechanism for a stack could be an array. In theory this allows access to any element. The ABSTRACT definition outlaws this.

7 Understanding Stacks In theory a stack is like a pile of plates. You can only get at the top plate only. If you want to get at the bottom plate, you have to remove all the plates in the pile. Top plate Bottom plate You can only add a plate to the top of the pile. Therefore this representation is sometimes called a Last In First Out (LIFO) data structure.

8 Some comments on stacks Given a stack, that constantly has items added and removed, it is possible that the bottom items never get processed. If items arrive faster than they are removed, the “stack overflows”. In stack terminology, adding an items to the top of a stack is called pushing Removing an items is called popping. Looking at the top item and not removing is called peeking.

9 Basic operations of stacks const int MAX; //set max size of stack //Basic structures needed for a stack struct mydata { int x;//can be as complex as you want } struct stack { int top; int nItems; mydata data[MAX]; } Push (put an item on the stack) Pop (remove an item from the stack) Peek (look at the value of the top item)

10 void Push(stack &S, int val) { S.top++; S.nItems++; S.data[S.top] = val; } void Pop(stack &S) { S.top--; S.nItems--; } int Peek(stack &S) { return = S.data[S.top]; } Demo 1 Basic Operations

11 Initial state of stack top = -1 index 0 index 1 index 2 index 3 index 4

12 State of stack after push(mystack,10) 10 top = 0 index 0 index 1 index 2 index 3 index 4

13 State of stack after push(mystack,9) 10 9 top = 1 index 0 index 1 index 2 index 3 index 4

14 State of stack after push(mystack,8) top = 2 index 0 index 1 index 2 index 3 index 4 available slots

15 State of stack after first pop(mystack) top = 1 index 0 index 1 index 2 index 3 index 4 available slots Note that technically the value 8 is still there, but in theory, because the top is now moved down, this is effectively the next available slot

16 State of stack after second pop(mystack) top = 0 index 0 index 1 index 2 index 3 index 4 available slots Note that technically the values 8 and 9 are still there, but in theory, the top is now moved down.

17 Error Handling What happens if you try to push to a full stack or pop from an empty stack. A simple solution is to check the state of the stack before attempting a stack operation. For example if(mystack.nItems < MAX) Push(mystack, 10); else cout << “Error stack overflow!” << endl; OR if(mystack.nItems > 0) Pop(mystack); else cout << “Error empty stack” << endl;

18 Efficiency Pushing and popping are done in O(1) time. Very fast, but remember that you use this for specific non database tasks, you do not search stacks, you do not sort stacks, so these algorithmic performance issues are not relevant.

19 Queues Similar to a stack, except you add items from one end and remove items from the other. First In First Out (FIFO) Also by convention the names of the operations are different. enqueue and serve are the queue equivalent to push and pop. A stack has a variable top a queue has head/front and a tail/rear. variables

20 Uses of Queue’s Modelling real world situations Bank queues Road congestion Computer processes  E.g. Windows has an event queue to process mouse and menu events  Queue’s are used to store keystrokes, or information arriving at communication ports

21 Simple Implementation of Queues Using Arrays structures required const int MAX = 5; struct mydata { int x;//can be as complex as you want } struct queue { int front; int rear; int nItems; mydata data[MAX]; }

22 Initialisation and operation A empty queue should initialise varaibles front to 0, rear to –1 and nItems to 0 When an item enqueued rear is incremented. Shuffle queue down one whenever an item is served (very inefficient I know) and decrements rear and nItems variables.

23 void Enqueue(queue &Q, int val) { Q.rear++; Q.nItems++; Q.data[Q.rear] = val; } void Serve(queue &Q,int & val) { int i; val = Q.data[0]; for (i = 0; i < Q.rear;i++) Q.data[i] = Q.data[i+1]; Q.rear--; Q.nItems--; } int Peek(queue &Q) { return = Q.data[Q.front]; } Basic Operations Shuffle whole queue down one and move rear index down one too

24 Demo 2 Implementation of Enqueue(), Dequeue() and Peek for a queue

25 Errors Again check before using an operation whether queue is full or empty. Make use of nItems variable. nItems == 0 means queue is empty nItems >= maxitems means queue is full

26 Improvement in efficiency of queue. Use a circular queue, rather than shuffling down the entire queue every time an item is removed, we could simply move the front index to the next item in the queue. We be able to wrap around the indexes. That is when rear comes to the end of the array we must see if there is room at the beginning of the array.

27 Diagrams for circular queue 1. An empty queue Q Front = 0 Rear = -1 nItems = 0 Q

28 Diagrams for circular queue 2. enqueue(Q,10); 10 Front = 0 Rear = 0 nItems =

29 Diagrams for circular queue 3. enqueue(Q,20); Front = 0 Rear = 1 nItems =

30 Diagrams for circular queue 4. enqueue(Q,30); Front = 0 Rear = 2 nItems =

31 Diagrams for circular queue 5. enqueue(Q,40); Front = 0 Rear = 3 nItems =

32 Diagrams for circular queue 6. serve(Q,N); Front = 1 Rear = 3 nItems =

33 Diagrams for circular queue 7. serve(Q,N); Front = 2 Rear = 3 nItems =

34 Diagrams for circular queue 8. serve(Q,N); Front = 3 Rear = 3 nItems =

35 Diagrams for circular queue 9. enqueue(Q,50); Front = 3 Rear = 4 nItems =

36 Diagrams for circular queue 10. enqueue(Q,60); Note wrap around Front = 3 Rear = 0 nItems =

37 Diagrams for circular queue 11. enqueue(Q,70); Front = 3 Rear = 1 nItems =

38 Demo 3 Implementation of circular queue