Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.

Slides:



Advertisements
Similar presentations
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
CS Data Structures II Review COSC 2006 April 14, 2017
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)
Stacks, Queues & Deques CSC212.
Stacks and Queues.
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 and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
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.
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.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. 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,
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.
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 & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Lists, Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
 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.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
Lists, Stacks and Queues
ADT list.
Stacks and Queues 1.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Presentation transcript:

Stack Overview

Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list

What is a stack? A stack is a list in which insertion and deletion take place at the same end – This end is called top – The other end is called bottom Stacks are known as LIFO (Last In, First Out) lists. – The last element inserted will be the first to be retrieved

Primary Operations Primary operations: Push and Pop Push – Add an element to the top of the stack Pop – Remove the element at the top of the stack

An Example top empty stack A top push an element top push another A B top pop A

Implementation of stack Any list implementation could be used to implement a stack – Arrays (static: the size of stack is given initially) – Linked lists (dynamic: never become full) We will explore implementations based on array and linked list Let’s see how to use an array to implement a stack first

Array implementation of stack # define maxTop 100 class Stack { public: Stack(int size = 10);// constructor ~Stack() { delete [] values;}// destructor bool IsEmpty() { return top == -1; } bool IsFull() { return top == maxTop; } double Top(); // examine, without popping void Push(const double x); double Pop(); void DisplayStack(); private: int top;// current top of stack int values[maxTop];// element array };

Class “Stack” Attributes of Stack – maxTop : the max size of stack – top : the index of the top element of stack – values : An array which stores elements of stack Operations of Stack – IsEmpty : return true if stack is empty, return false otherwise – IsFull : return true if stack is full, return false otherwise – Top : return the element at the top of stack – Push : add an element to the top of stack – Pop : delete the element at the top of stack – DisplayStack : print all the data in the stack

Array implementation of stack The constructor of Stack – Allocate a stack array of size. By default, size = 10. – Initially top is set to -1. It means the stack is empty. – When the stack is full, top will have its maximum value, i.e. size – 1. – Size of the stack is fixed.

Array implementation of stack void Push(const double x); – Push an element onto the stack – Note top always represents the index of the top element. After pushing an element, increment top void Stack::Push(const double x) { if (IsFull()) // if stack is full, print error cout << "Error: the stack is full." << endl; else values[++top]=x;

Array implementation of stack double Pop() – Pop and return the element at the top of the stack – Decrement top double Stack::Pop() { if (IsEmpty()) { //if stack is empty, print error cout << "Error: the stack is empty." << endl; return -1; } else { return values[top--]; }

Array implementation of stack double Top() – Return the top element of the stack – Unlike Pop, this function does not remove the top element double Stack::Top() { if (IsEmpty()) { cout << "Error: the stack is empty." << endl; return -1; } else return values[top]; }

Array implementation of stack void DisplayStack() – Print all the elements void Stack::DisplayStack() { cout "; for (int i = top; i >= 0; i--) cout << "\t|\t" << values[i] << "\t|" << endl; cout << "\t| |" << endl; }

Linked List implementation of stack- Insertion 1NULL temp Top = NULL 1NULL toptemp 1NULL top 2 Top !=NULL temp->next = top top = temp 1NULL top 2 temp 2NULL temp struct node { int data; struct node *next; }; struct node *top;

Linked List implementation of Stack – Pop 44 top 90 top->next NIL 44 top top != NULL temp = top; top = top -> next; temp = 90 top->next NIL top

Applications – Balancing parenthesis Check that every right brace, bracket, and parentheses must correspond to its left counterpart – e.g. [( )] is legal, but [( ] ) is illegal Algorithm (1) Make an empty stack. (2) Read characters until end of file i. If the character is an opening symbol, push it onto the stack ii. If it is a closing symbol, then if the stack is empty, report an error iii. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error (3) At end of file, if the stack is not empty, report an error

Queue ADT

Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue – insert an element at the rear of the queue Dequeue – remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) rearfront

Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

Queue Implementation of Array There are several different algorithms to implement Enqueue and Dequeue Naïve way – When enqueuing, the front index is always fixed and the rear index moves forward in the array. front rear Enqueue(3) 3 front rear Enqueue(6) 3 6 front rear Enqueue(9) 3 6 9

Queue Implementation of Array Naïve way (cont’d) – When dequeuing, the front index is fixed, and the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!) Dequeue() front rear 6 9 Dequeue() front rear 9 rear = -1 front

Queue Implementation of Array A better way – When an item is enqueued, the rear index moves forward. – When an item is dequeued, the front index also moves forward by one element XXXXOOOOO (rear) OXXXXOOOO (after 1 dequeue, and 1 enqueue) OOXXXXXOO (after another dequeue, and 2 enqueues) OOOOXXXXX (after 2 more dequeues, and 2 enqueues) (front) The problem here is that the rear index cannot move beyond the last element in the array.

Implementation using Circular Array Using a circular array When an element moves past the end of a circular array, it wraps around to the beginning, e.g. – OOOOO7963  4OOOO7963 (after Enqueue(4)) – After Enqueue(4), the rear index moves from 3 to 4. How to detect an empty or full queue, using a circular array algorithm? – Use a counter of the number of elements in the queue.

Queue Implementation of Linked List # define maxSize 100 class Queue { public: Queue();// constructor ~Queue() { delete [] values; }// destructor bool IsEmpty(void); bool IsFull(void); void Enqueue(double x); double Dequeue(double x); void DisplayQueue(void); private: int front;// front index int rear;// rear index double values[maxSize]; };

Queue Class Attributes of Queue – front/rear : front/rear index – maxSize : capacity of the queue – values : An array which stores elements of the queue Operations of Queue – IsEmpty : return true if queue is empty, return false otherwise – IsFull : return true if queue is full, return false otherwise – Enqueue : add an element to the rear of queue – Dequeue : delete the element at the front of queue – DisplayQueue : print all the data

Create Queue Queue() – Allocate a queue array of size. By default, size = 10. – front is set to 0, pointing to the first element of the array – rear is set to -1. The queue is empty initially.

IsEmpty & IsFull bool Queue::IsEmpty() { if ((rear+1)%maxSize == front)return true; elsereturn false; } bool Queue::IsFull() { if ((front+2)%maxSize == front)return true; elsereturn false; }

Enqueue void Queue::Enqueue(double x) { if (IsFull()) { cout << "Error: the queue is full." << endl; return false; } else { // calculate the new rear position (circular) rear= (rear + 1) % maxSize; // insert new item values[rear]= x; // update counter }

Dequeue double Queue::Dequeue(double & x) { if (IsEmpty()) { cout << "Error: the queue is empty." << endl; return false; } else { // retrieve the front item x= values[front]; // move front front= (front + 1) % maxSize; // update counter return x; }

Printing the elements void Queue::DisplayQueue() { cout "; for (int i = 0; i !=rear; i=(i+1)%maxSize) { cout << values[i]; cout << values[rear]<< endl; }

Linked List implementation of Queues- Enqueue 1NULL temp front == NULL and rear == NULL 1NULL front, rear Enqueue(45) 1NULL front, rear 45NULL temp 1 front, rear 45NULL temp rear->next = temp temp = rear rear != NULL 1 front 45NULL rear

Linked List implementation of Queues - Dequeue 90 front>next NULL 44 front rear front !=NULL temp = front front = front->next 44 front temp = 90 front>next NULL front rear front ==rear temp = front front = NULL rear = NULL 12NULL front, rear 12NULL Front=NULL Rear = NULL After Deletion