Stacks and Queues 2014, Fall Pusan National University Ki-Joune Li.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
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.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
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.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
 Balancing Symbols 3. Applications
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.
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.
CS Data Structures Chapter 3 Stacks and Queues.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Chapter 3. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be.
Stacks and Queues.
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.
Algorithms and Data Structures Representing Sequences by Arrays and Linked Lists.
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.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks Stack is a data structure that can be used to store data which can later be retrieved in the reverse or last in first out (LIFO) order. Stack is.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Chap 3 Stack and Queue. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable.
STACK Data Structure
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
 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.
STACKS & QUEUES for CLASS XII ( C++).
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
COMPUTER 2430 Object Oriented Programming and Data Structures I
Instructor: Mr.Vahidipour
2017, Fall Pusan National University Ki-Joune Li
Lists, Stacks and Queues
2018, Fall Pusan National University Ki-Joune Li
Stacks and Queues CSE 373 Data Structures.
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.
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks CS-240 Dick Steflik.
Stacks and Queues CSE 373 Data Structures.
Python: Stacks and Queues (as an Array)
CSE 373 Data Structures Lecture 6
CH3. STACKS AND QUEUES.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Stacks and Queues 2014, Fall Pusan National University Ki-Joune Li

STEMPNU 2 Stack  A Container  Last-In First-Out (LIFO)  Access only to the element at the top Push : Insert an element at the top Pop : Remove an element from the top Example  Function Invocation Previous frame pointer (registers, local variables) Return Address, Parameters Bottom top

STEMPNU 3 Operations and Implementation Operations  Maintenance : creation of a new stack, deletion of stack  Push and Pop  IsEmpty and IsFull Data Structures Class Stack { private: inttop,MaxSize; Type*stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Typepop(); voidpush(Type element); }; Class Stack { private: inttop,MaxSize; Type*stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Typepop(); voidpush(Type element); }; Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; }

STEMPNU 4 Queue  A Container  First-In First-Out (FIFO)  Access only to the elements at the front and rear Add: Insert an element to the rear Delete: Remove an element from the front Example  Process Scheduling front rear Process 3 Process 9 Process 2Process 8Process 4 Ready Queue CPU

STEMPNU 5 Operations and Implementation Operations  Maintenance : creation of a new queue, deletion of queue  Add and Delete  IsEmpty and IsFull Data Structures Class Queue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Typedelete(); voidadd(Type element); }; Class Queue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Typedelete(); voidadd(Type element); }; Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=-1; } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=-1; } Boolean Queue::isFull() { if(rear==MaxSize-1) return YES; else return NO; } Boolean Queue::isFull() { if(rear==MaxSize-1) return YES; else return NO; } Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; } What’s the problem ?

STEMPNU 6 Circular Queue Class CircularQueue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Typedelete(); voidadd(Type element); }; Class CircularQueue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Typedelete(); voidadd(Type element); }; void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::delete(Type v) { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } void Queue::delete(Type v) { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=1; } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=1; }

STEMPNU Example 7 012…MaxSize-1 front rear void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } newRear front=1 rear=1 front=1 rear=1 newRear=2 front=1 rear=2 front=1 rear=2 newRear=3 front=1 rear=3 front=1 rear=MaxSize-1 newRear=0 front=1 rear=0 3 front=1 rear=0 newRear=1 Typr Queue::delete() { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } Typr Queue::delete() { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } front=2 rear=0 front=MaxSize-1 rear=0 front=0 rear=0 front=0 rear=0

STEMPNU 8 Application of Stack : Mazing Problem How to find the path ?

STEMPNU 9 Path Finding Algorithm for Mazing Problem Algorithm PathFinding(int p,int q,int maze[p][q]) // (p,q): coorinates of exit cell pathStack  initialize Stack; pathStack.push((0,0)); while(pathStack.isEmpty()==NO) { (i,j)  pathStack.getTop(); // read but not remove while(there is an unvisited cell (m,n) from (i,j)) { pathStack.push((m,n)); if(m==p and n==q) { // path found pathStack.print(); // pop and print return; } (i,j)  (m,n); } pathStack.pop(); } print “No path”; End Algorithm PathFinding Algorithm PathFinding(int p,int q,int maze[p][q]) // (p,q): coorinates of exit cell pathStack  initialize Stack; pathStack.push((0,0)); while(pathStack.isEmpty()==NO) { (i,j)  pathStack.getTop(); // read but not remove while(there is an unvisited cell (m,n) from (i,j)) { pathStack.push((m,n)); if(m==p and n==q) { // path found pathStack.print(); // pop and print return; } (i,j)  (m,n); } pathStack.pop(); } print “No path”; End Algorithm PathFinding

STEMPNU 10 Evaluation of Postfix Infix to Postfix Application of stack : Evaluation of Expressions X = A / B – C + D * E – A * C How to evaluate this ? X = (((A / B) – C) + (D * E)) – (A * C) Infix Notation X = (((A B / ) C – ) (D E * ) +) (A C * ) – Postfix Notation X = A B / C – D E * + A C * –

STEMPNU 11 Evaluation of Expression in Postfix Notation X = A B / C – D E * + A C * – AA B T 1 =A / B /AB T1T1 C T1T1 C - T 2 = T 1 - C T2T2 T2T2 DE … T4T4 T5T5 T4T4 - T5T5 T 6 = T 4 – T 5 T6T6 T6T6 = D T2T2 E D

STEMPNU 12 Infix to Postfix X = A / ( B – C ) + D * E – A * C Infix Notation X = A B C – / D E * + A C* – Postfix Notation A A / / / ( ( B B / - ( - C C / ) ( - / ( - / / + + D D + * * E E+ - * -