Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.”
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Starting Out with C++, 3 rd Edition Chapter 18 Stacks and Queues.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Starting Out with C++, 3 rd Edition Chapter 18 – Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
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.”
CS 204 Stacks and Queues (not in any book that we are using) From Starting out with C++, 7th edition, by Gaddis, Walters and Muganda (chapter 18, pp
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Chapter 12: Data Structures
Queue.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 19: Stacks and Queues.
(not in any book that we are using)
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
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.
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Data Structures & Programming
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues

Copyright © 2012 Pearson Education, Inc Introduction to the Stack ADT

Copyright © 2012 Pearson Education, Inc. Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for function calls

Copyright © 2012 Pearson Education, Inc. Introduction to the Stack ADT Stack: –a LIFO (last in, first out) data structure Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list

Copyright © 2012 Pearson Education, Inc. Stack Operations and Functions Operations: –push: add a value onto the top of the stack –pop: remove a value from the top of the stack

Copyright © 2012 Pearson Education, Inc. Stack Operations - Example A stack that can hold char values: K E G K E E push('E');push('K');push('G');

Copyright © 2012 Pearson Education, Inc. Stack Operations - Example A stack that can hold char values: E K E pop(); G K E

Copyright © 2012 Pearson Education, Inc. Stack Operations and Functions Other Functions: –isFull : true if the stack is currently full, i.e., has no more space to hold additional elements –isEmpty : true if the stack currently contains no elements

Copyright © 2012 Pearson Education, Inc. (See IntStack.cpp for the implementation.)

Copyright © 2012 Pearson Education, Inc Dynamic Stacks

Copyright © 2012 Pearson Education, Inc. Dynamic Stacks Grow and shrink as necessary Can't ever be full as long as memory is available Implemented as a linked list

Copyright © 2012 Pearson Education, Inc. Implementing a Stack Programmers can program their own routines to implement stack functions See DynIntStack class in the book for an example. Can also use the implementation of stack available in the STL

Copyright © 2012 Pearson Education, Inc Introduction to the Queue ADT

Copyright © 2012 Pearson Education, Inc. Introduction to the Queue ADT Queue: a FIFO (first in, first out) data structure. Examples: –people in line at the theatre box office –print jobs sent to a printer Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list

Copyright © 2012 Pearson Education, Inc. Queue Locations and Operations rear: position where elements are added front: position from which elements are removed enqueue: add an element to the rear of the queue dequeue: remove an element from the front of a queue

Copyright © 2012 Pearson Education, Inc. Queue Operations - Example A currently empty queue that can hold char values: enqueue('E'); E front rear

Copyright © 2012 Pearson Education, Inc. Queue Operations - Example enqueue('K'); enqueue('G'); EK EKG front rear front rear

Copyright © 2012 Pearson Education, Inc. Queue Operations - Example dequeue(); // remove E dequeue(); // remove K KG G front rear front rear

Copyright © 2012 Pearson Education, Inc. dequeue Issue, Solutions When removing an element from a queue, remaining elements must shift to front Solutions: –Let front index move as elements are removed (works as long as rear index is not at end of array) –Use above solution, and also let rear index "wrap around" to front of array, treating array as circular instead of linear (more complex enqueue, dequeue code)

Copyright © 2012 Pearson Education, Inc Contents of IntQueue.h 1 // Specification file for the IntQueue class 2 #ifndef INTQUEUE_H 3 #define INTQUEUE_H 4 5 class IntQueue 6 { 7 private: 8 int *queueArray; // Points to the queue array 9 int queueSize; // The queue size 10 int front; // Subscript of the queue front 11 int rear; // Subscript of the queue rear 12 int numItems; // Number of items in the queue

Copyright © 2012 Pearson Education, Inc. 13 public: 14 // Constructor 15 IntQueue(int); // Copy constructor 18 IntQueue(const IntQueue &); // Destructor 21 ~IntQueue(); // Queue operations 24 void enqueue(int); 25 void dequeue(int &); 26 bool isEmpty() const; 27 bool isFull() const; 28 void clear(); 29 }; 30 #endif (See IntQueue.cpp for the implementation) Contents of IntQueue.h (Continued)

Copyright © 2012 Pearson Education, Inc Dynamic Queues

Copyright © 2012 Pearson Education, Inc. Dynamic Queues Like a stack, a queue can be implemented using a linked list Allows dynamic sizing, avoids issue of shifting elements or wrapping indices front rear NULL

Copyright © 2012 Pearson Education, Inc. Implementing a Queue Programmers can program their own routines to implement queue operations See the DynIntQue class in the book for an example of a dynamic queue Can also use the implementation of queue and dequeue available in the STL