Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Similar documents
Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

ADTs: Stacks and Queues

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Chapter 17: Linked Lists

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18


An Introduction to Queues With Examples in C++

CS24 Week 4 Lecture 2

Stack and Queue. Stack:

STACKS AND QUEUES. Problem Solving with Computers-II

.:: UNIT 4 ::. STACK AND QUEUE

Chapter 17: Linked Lists

1 P age DS & OOPS / UNIT II

Lecture Data Structure Stack

CMSC 132: Object-Oriented Programming II. Stack and Queue

Object Oriented Programming COP3330 / CGS5409

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Ashish Gupta, Data JUET, Guna

ITI Introduction to Computing II

csci 210: Data Structures Stacks and Queues

Abstract Data Types 1

CSEN 301 Data Structures and Algorithms

1 Short Answer (7 Points Each)

Data Structures (INE2011)

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

LIFO : Last In First Out

Queue with Array Implementation

CS302 - Data Structures using C++

Queues. CITS2200 Data Structures and Algorithms. Topic 5

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types 1

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Unit 4: Stacks and Queues

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Dynamic Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

UNIT-2 Stack & Queue

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Chapter 5. ADTs Stack and Queue

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Stacks and Queues. Chapter 8 (Cont.)

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CS 206 Introduction to Computer Science II

Name CPTR246 Spring '17 (100 total points) Exam 3

Linked List. April 2, 2007 Programming and Data Structure 1

Stacks and Queues. CSE 373 Data Structures Lecture 6

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Propedéutico de Programación

Queue Implementations

Queue Definition. Ordered list with property:

Data Structure. Recitation VII

Queues Fall 2018 Margaret Reid-Miller

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSC 1052 Algorithms & Data Structures II: Queues

Queue: Queue Representation: Basic Operations:

ADTs Stack and Queue. Outline

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Information Science 2

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Data Abstraction and Specification of ADTs

ADTs Stack and Queue. Outline

Programming. Lists, Stacks, Queues

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Your Topic Goes Here Queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Data Structures And Algorithms

Chapter 9 STACK, QUEUE

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Data Structures -- Introduction

Introduction to the C programming language

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Unit 4 Basic Collections

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Transcription:

Chapter 18: Stacks And Queues Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

18.1 Introduction to the Stack ADT Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: plates in a cafeteria return addresses for function calls Implementation: static: fixed size, implemented as array dynamic: variable size, implemented as linked list Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-3

A LIFO Structure Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-4

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 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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-5

Stack Operations - Example A stack that can hold char values: G push('e'); push('k'); push('g'); E K E K E Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-6

Stack Operations - Example A stack that can hold char values: pop(); (remove G) K E pop(); (remove K) E pop(); (remove E) Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-7

(See IntStack.cpp for the implementation.) Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-8

18.2 Dynamic Stacks Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Dynamic Stacks Grow and shrink as necessary Can't ever be full as long as memory is available Implemented as a linked list Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-10

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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-11

18.3 The STL stack container Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The STL stack container Stack template can be implemented as a vector, a linked list, or a deque Implements push, pop, and empty member functions Implements other member functions: size: number of elements on the stack top: reference to element on top of the stack Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-13

Defining a stack Defining a stack of chars, named cstack, implemented using a vector: stack< char, vector<char> > cstack; implemented using a list: stack< char, list<char> > cstack; implemented using a deque: stack< char > cstack; Spaces are required between consecutive >>, << symbols Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-14

18.4 Introduction to the Queue ADT Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-16

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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-17

Queue Operations - Example A currently empty queue that can hold char values: enqueue('e'); front E rear Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-18

Queue Operations - Example enqueue('k'); front E K enqueue('g'); rear front E K G rear Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-19

Queue Operations - Example dequeue(); // remove E front K G rear dequeue(); // remove K front G rear Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-20

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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-21

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 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-22

Contents of IntQueue.h (Continued) 13 public: 14 // Constructor 15 IntQueue(int); 16 17 // Copy constructor 18 IntQueue(const IntQueue &); 19 20 // Destructor 21 ~IntQueue(); 22 23 // 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 Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-23

18.5 Dynamic Queues Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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 NULL front rear Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-25

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 Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-26

18.6 The STL deque and queue Containers Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The STL deque and queue Containers deque: a double-ended queue. Has member functions to enqueue (push_back) and dequeue (pop_front) queue: container ADT that can be used to provide queue as a vector, list, or deque. Has member functions to enque (push) and dequeue (pop) Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-28

Defining a queue Defining a queue of chars, named cqueue, implemented using a deque: deque<char> cqueue; implemented using a queue: queue<char> cqueue; implemented using a list: queue< char, list<char> > cqueue; Spaces are required between consecutive >>, << symbols Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-29