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

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

ADTs: Stacks and Queues

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

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.

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

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Chapter 18: Stacks And Queues

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

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

Chapter 18: Stacks And Queues

Lecture Data Structure Stack

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

STACKS AND QUEUES. Problem Solving with Computers-II

CS24 Week 4 Lecture 2

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

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


Programming Abstractions

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

UNIT-2 Stack & Queue

Stacks and their Applications

[CS302-Data Structures] Homework 2: Stacks

The Stack and Queue Types

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

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

Lecture 4 Stack and Queue

CS 206 Introduction to Computer Science II

Queue with Array Implementation

// The next 4 functions return true on success, false on failure

An Introduction to Queues With Examples in C++

Lecture No.04. Data Structures

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

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

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

Stack and Queue. Stack:

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

Object Oriented Programming COP3330 / CGS5409

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Stacks and Queues. CSE Data Structures April 12, 2002

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Abstract Data Types. Abstract Data Types

15. Stacks and Queues

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

12 Abstract Data Types

Programming Abstractions

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

CS 106B Lecture 5: Stacks and Queues

1 P age DS & OOPS / UNIT II

Associate Professor Dr. Raed Ibraheem Hamed

CS 206 Introduction to Computer Science II

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSEN 301 Data Structures and Algorithms

Abstract Data Types 1

ITI Introduction to Computing II

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

BBM 201 DATA STRUCTURES

Stacks and Queues. Chapter 8 (Cont.)

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

BBM 201 DATA STRUCTURES

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

CSC 1052 Algorithms & Data Structures II: Queues

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

ADTs Stack and Queue. Outline

Data Structures And Algorithms

Stack and Its Implementation

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CS 216 Exam 1 Fall SOLUTION

Discussion of project # 1 permutations

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

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

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

Data Structure - Stack and Queue-

Queue: Queue Representation: Basic Operations:

Ashish Gupta, Data JUET, Guna

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Data Structures and Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Data Structures G5029

.:: UNIT 4 ::. STACK AND QUEUE

Data Structures and Algorithms Winter Semester

Linear Data Structure

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

C++ For Science and Engineering Lecture 27

More Data Structures (Part 1) Stacks

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Data Structures (INE2011)

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Transcription:

Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are specific, - not concerned with how the data will be represented or how the operations will be implemented. An ADT may be implemented by specific data types or data structures, in many ways and in many programming languages. Examples: - BookInventory (impl d using array AND linked list) - Stacks and Queues 2

Introduction to the Stack Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed according to LIFO order: last in, first out Examples: - plates in a cafeteria - bangles... (bracelets) 3 Operations: Stack Operations - push: add a value onto the top of the stack make sure it s not full first. - pop: remove a value from the top of the stack make sure it s not empty first. - 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 4

Stack illustrated X X X X 5 Stack Application: Postfix notation Postfix notation is another way of writing arithmetic expressions. We normally use infix, the operator is between the operands In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: 2 5 + Expressions are evaluated from left to right. Precedence rules and parentheses are never 6 needed!!

Postfix notation evaluation from left to right replace evaluated expression with result 7 Postfix notation: using a stack evaluation from left to right: push operands for operator: pop two values, perform operation, and push the result 8

Evaluate Postfix Expression algorithm Using a stack: WHILE more input items exist get next item IF item is an operand stack.push(item) ELSE stack.pop(operand2) stack.pop(operand1) Compute result stack.push(result) end WHILE stack.pop(result) 9 Implementing a Stack Class Static stacks: - fixed size - implemented using arrays - uses a dynamically allocated array, but once allocated, the array does not change size Dynamic stacks - grow in size as needed - implemented using linked list 10

A static stack class class IntStack private: int *stackarray; // Pointer to the stack array int stacksize; // The stack size (will not change) int top; // Index to the top of the stack public: // Constructor IntStack(int); // Destructor ~IntStack(); ; // Stack operations void push(int); void pop(int &); bool isfull() const; bool isempty() const; const here indicates these functions will not change any of the member variables in the object the functions are called from 11 A static stack class: functions //*********************************************** // Constructor * // This constructor creates an empty stack. The * // size parameter is the size of the stack. * //*********************************************** IntStack::IntStack(int size) stackarray = new int[size]; // dynamic alloc stacksize = size; // save for reference top = -1; // empty //*********************************************** // Destructor * //*********************************************** IntStack::~IntStack() delete [] stackarray; 12

A static stack class: push //************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push(int num) if (isfull()) cout << "The stack is full.\n"; else top++; stackarray[top] = num; 13 A static stack class: pop //**************************************************** // Member function pop pops the value at the top * // of the stack off, and copies it into the variable * // passed as an argument. * //**************************************************** void IntStack::pop(int &num) if (isempty()) cout << "The stack is empty.\n"; else num = stackarray[top]; top--; 14

A static stack class: functions //*************************************************** // Member function isfull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull() const return (top == stacksize - 1); //**************************************************** // Member funciton isempty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool IntStack::isEmpty() const return (top == -1); 15 Introduction to the Queue Queue: a data structure that holds a collection of elements of the same type. - The elements are accessed according to FIFO order: first in, first out Examples: - people in line at a theatre box office - print jobs sent to a printer 16

Operations: Queue Operations - enqueue: add a value onto the rear of the queue (the end of the line) make sure it s not full first. - dequeue: remove a value from the front of the queue (the front of the line) Next! make sure it s not empty first. - isfull: true if the queue is currently full, i.e.,has no more space to hold additional elements - isempty: true if the queue currently contains no 17 elements Queue illustrated X X X X X Note: front and rear are variables used by the implementation to carry out the operations int item; q.enqueue(2): q.enqueue(3); q.enqueue(5); q.dequeue(item); //item is 2 q.dequeue(item); //item is 3 q.enqueue(10); 18

Queue Applications The best applications of queues involve multiple processes. For example, imagine the print queue for a computer lab. Any computer can add a new print job to the queue (enqueue). The printer performs the dequeue operation and starts printing that job. While it is printing, more jobs are added to the Q When the printer finishes, it pulls the next job 19 from the Q, continuing until the Q is empty Queue implemented Just like stacks, queues can be implemented as arrays (static queues) or linked lists (dynamic queues). The previous illustration assumed we were using an array to implement the queue When an item was dequeued, the items were NOT shifted up to fill the slot vacated by dequeued item why not? efficiency Instead, both front and rear indices move in the 20 array.

Queue implemented When front and rear indices move in the array: problem: rear hits end of array quickly solution: wrap index around to front of array 7 9 6 front rear 3 7 9 6 rear front 3 4 7 9 6 21 rear front Queue implemented To wrap the index back to the front of the array, use this code to increment rear during enqueue: if (rear = queuesize-1) rear = 0; else rear = rear+1; This code is equivalent to the following rear = (rear + 1) % queuesize; Do the same for advancing front index. Now, how do we know if the queue is empty 22 or full?

Queue implemented An easy solution for isfull and isempty: - Use a counter variable to keep track of the total number of items in the queue. enqueue: numitems++ dequeue: numitems-- isempty is true when numitems is 0 isfull is true when numitems is equal to queuesize 23 Queue implemented In the implementation that follows: the queue is a dynamically allocated array, whose size does not change front and rear are initialized to -1. If the queue is not empty: - rear is the index of the last item that was enqueued. - front+1 is the index of the next item to be dequeued. numitems: how many items are in the queue queuesize: the size of the array 24

A static queue class // Specification file for the IntQueue class class IntQueue private: int *queuearray; // Points to the queue array int queuesize; // The queue size int front; // Subscript of the queue front int rear; // Subscript of the queue rear int numitems; // Number of items in the queue public: // Constructor IntQueue(int); ; // Destructor ~IntQueue(); // Queue operations void enqueue(int); void dequeue(int &); bool isempty() const; bool isfull() const; 25 A static queue class: functions // Creates an empty queue of a specified size. * IntQueue::IntQueue(int s) queuearray = new int[s]; queuesize = s; front = -1; rear = -1; numitems = 0; // Destructor * IntQueue::~IntQueue() delete [] queuearray; 26

A static queue class: enqueue // Enqueue inserts a value at the rear of the queue. * void IntQueue::enqueue(int num) if (isfull()) cout << "The queue is full.\n"; else // Calculate the new rear position rear = (rear + 1) % queuesize; // Insert new item queuearray[rear] = num; // Update item count numitems++; 27 A static queue class: dequeue // Dequeue removes the value at the front of the * // queue and copies t into num. * void IntQueue::dequeue(int &num) if (isempty()) cout << "The queue is empty.\n"; else // Move front front = (front + 1) % queuesize; // Retrieve the front item num = queuearray[front]; // Update item count numitems--; 28

A static queue class: functions // isempty returns true if the queue is empty, * // otherwise false. * bool IntQueue::isEmpty() const return (numitems == 0); // isfull returns true if the queue is full, otherwise * // false. * bool IntQueue::isFull() const return (numitems == queuesize); 29