FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
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.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS Data Structures II Review COSC 2006 April 14, 2017
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
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 and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Stacks, Queues & Deques CSC212.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Objectives of these slides:
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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’
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,
Data Structures Using C++
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures & Algorithms
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 7 Queues Introduction Queue applications Implementations.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Computer Engineering Rabie A. Ramadan Lecture 6.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
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,
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Review Array Array Elements Accessing array elements
Stacks.
Queues.
CC 215 Data Structures Queue ADT
Dr. Bernard Chen Ph.D. University of Central Arkansas
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Type Abstract Data Type as a design tool
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks and Queues.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Lecture 9: Stack and Queue
Presentation transcript:

FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )

FIST, Multi Media University Stack What is a Stack? 1) It is a data structure for storing some values 2) It is an ADT. 3) Implemented using Arrays or Linked List 4) used in Operating System and in System Programming

FIST, Multi Media University Stack How to Store and Retrieve from a Stack? 1) All Insertions ( Storing) and Deletions (Removing) of entries are made at one end called the TOP of Stack 2) Last item added to a stack (PUSH) is always the first that will be removed (POP) from the stack. 3) This property is called Last In First Out also known as LIFO 4) The operations are done with a single pointer to the top of list of elements. Operations...

FIST, Multi Media University Stack A B C D E TOP F G EG - Plates Books (LIFO)

FIST, Multi Media University Operations There are Four operations : 1) Push ( ) used to add the data at the top of the stack. Check whether the stack is full or not 2) Pop ( ) used to remove data at the top of the stack. Check whether the stack is empty or not 3) Empty ( ) Checks whether stack is empty, TOP = -1 4) Full ( ) checks whether stack is full, TOP = maxstacksize

FIST, Multi Media University Stack - implementation Uses ARRAY to represent a stack Class stack { private : int stackelement[10],TOP; public: stack( ) { top = -1} void push(int); int Pop( ); int Empty( ); int Full ( ); };

FIST, Multi Media University Stack How to Push? Void stack :: Push(int data2add) { if (!Full( ))Full { TOP++; Stackelement[TOP] = data2add; } else cout << “Stack is Full!!!”<<endl; }

FIST, Multi Media University Stack How to Pop? Int stack :: Pop( ) {int data2remove; if (!Empty( ))Empty { data2remove = Stackelement[TOP]; TOP--; return data2remove; } else{ cout << “Stack is Empty!!!”<<endl; return 0; }}

FIST, Multi Media University Stack How to check for full? Int Stack :: Full( ) { if (TOP==9) return 1; else return 0; }

FIST, Multi Media University Stack How to check for Empty? Int Stack :: Empty( ) { if (TOP== -1) return 1; else return 0; }

FIST, Multi Media University Stack Void main( ) { stack stackobj; stackobj.Push(23); stackobj.Push(46); stackobj.Push(37); stackobj.Push(10); stackobj.Push(55); cout << stackobj.Pop( ); }

FIST, Multi Media University Applications of Stack We shall see two applications of the stack 1) Use of Stacks in Function Calls 2) Palindrome Checking

FIST, Multi Media University Use of Stacks in Function Calls Whenever a function begins execution an ACTIVATION RECORD (Stack Frame) is created to store the current environment for that function which includes Parameters Caller’s state information Local variables Temporary storage This structure should LIFO because, when a function terminates, the function with which to resume execution is the last function whose activation record was saved.LIFO

FIST, Multi Media University Stack Function1 Function2 Function3 Function4 Activation Record Function1 Function2 Function3

FIST, Multi Media University Palindrome Checking What is a palindrome? A palindrome is a word/sentence that is the same if read both ways. You might have noticed that if you push some characters into a stack and pop them out, they will appear in reverse order. So Stacks are used for this purpose.

FIST, Multi Media University Palindrome  Here are some palindromes MALAYALAM (a south Indian language) RATS LIVE ON NO EVIL STAR DAD POP RADAR STEP ON NO PETS MADAM

FIST, Multi Media University implementation #include class ADTstack { char stack[10]; int topstack; public: ADTstack( ) {topstack = -1;}; int empty() {if (topstack == -1) return 1; else return 0;}; int full() {if (topstack == 9) return 1; else return 0;}; void push(char num) { if (!full()) { topstack++; stack[topstack] = num; } else cout<<" Stack is Full"<<endl; }

FIST, Multi Media University implementation char pop( ) { char num; if (!empty()) { num = stack[topstack]; topstack--; return num; } else {cout<<"Stack is Empty"<<endl; return '0';} } };

FIST, Multi Media University implementation void main() { ADTstack st; char str[10]; int palin, l,i; cout<<"type in a string to check if it is a palindrome"<<endl; cin>>str; l=strlen(str); if (l==1) {palin=1;} //all strings with 1 character are palindromes else {

FIST, Multi Media University implementation i=0; while(str[i]!='\0') //push the string into stack { st.push(str[i]); i++; } i=0; while(str[i]!='\0') //pop the string and compare with original {if (str[i]!=st.pop()) {palin=0;break;} i++; } }if (palin==0) cout<<"Not Palindrome"<<endl; else cout<<"Palindrome"<<endl; }

FIST, Multi Media University Queue What is a Queue? 1) It is a data structure for storing some values 2) It is an ADT. 3) Implemented using Arrays or Linked List

FIST, Multi Media University How to Store and Retrieve from a Queue? 1) A Data Structure in which all Additions to the list are made at one end, and all Deletions from the list are made at other end. 2) First item added to a queue (ADD) is always the first that will be removed (REMOVE) from the queue. 3) This property is called First In First Out also known as FIFO 4) It can be maintained with two pointers namely FRONT (REMOVE) and REAR ( ADD). Queue

FIST, Multi Media University Queue FrontRear EG - Bank Counter

FIST, Multi Media University Operations There are Four operations : 1) Addend ( ) used to add the data at the Tail of the Queue. Check whether the Queue is full or not 2) Serve ( ) used to remove data at the top of the stack. Check whether the Queue is empty or not 3) Empty ( ) Checks whether Queue is empty, Tail = -1 4) Full ( ) checks whether Queue is full, Tail = maxqueuesize

FIST, Multi Media University implementation Uses ARRAY to represent a Queue Class Queue { private : int Queueelement[4],Head,Tail; public: Queue( ) { Head =0 ; Tail = -1} void Addend(int); int Serve( ); int Empty( ); int Full ( ); };

FIST, Multi Media University implementation How to check for full? Int Queue :: Full( ) { if (Tail == 3) return 1; else return 0; }

FIST, Multi Media University implementation How to check for Empty? Int Queue :: Empty( ) { if (Tail == -1) return 1; else return 0; }

FIST, Multi Media University implementation How to Add? Void Queue :: Addend(int data2add) { if (!Full( )) { Tail++; Queueelement[Tail] = data2add; } else cout << “Queue is Full!!!”<<endl; }

FIST, Multi Media University implementation How to Delete? int Queue :: Serve( ) {int data2remove; if (!Empty( )) { data2remove = Queueelement[0]; for ( i =0;i<Tail;i++) Queueelement[i] = Queueelement[i+1] Tail--; return data2remove; } else{ cout << “Queue is Empty!!!”<<endl; return 0; }} Methods

FIST, Multi Media University implementation Void main( ) { Queue queueobj; queueobj.Addend(23); queueobj.Addend(46); queueobj.Addend(37); queueobj.Addend(10); queueobj.Addend(55); cout << queueobj.Serve(); }

FIST, Multi Media University Serving Three Methods of Serving 1)increment head to 1 runs out of storage 2) when the first person has be served the remaining persons in the Queue shuffle forward one place. Can serve unlimited number of persons as long as the queue never exceeds the maximum size 3) Use cyclic queues One of the application of Queues is simulation

FIST, Multi Media University Lecture 6 Cyclic Queues

FIST, Multi Media University Cyclic Queues What is a Cyclic Queue? we need to imagine that the array allocated to the queue is not linear but cyclic, i.e. that it forms a circle. EG: So that if for instance 10 elements are allocated to the queue with the element positions running from 0 to 9 then when the tail gets to position 9 the next element to be added can go to position 0 providing that position has already been “served” (vacant)..Thus the two problems of the queues are solved.

FIST, Multi Media University Cyclic Queue Head Tail The following actions will cause the “head” and “tail” to move through the array as listed below: Example: Queue implemented using an array of size 6 with 4 data items inserted in to the queue

FIST, Multi Media University Cyclic Queue ActionHeadTail 0 3 Delete 1 3 Insert 1 4 Delete 2 4 Insert 2 5 Insert 2 0 Delete 3 0 Delete 4 0 HeadTail

FIST, Multi Media University Cyclic Queue ActionHeadTail Insert 4 1 Delete 5 1 Insert 5 2 Delete 0 2 Note: A Cyclic queue will always be of fixed size (say n) and the size information must be available to update the pointers “head” and “tail”. HeadTail

FIST, Multi Media University Cyclic Queue The wrapping around can be achieved by updating the pointers as follows: Delete: head = (head+1) % n Insert: tail = (tail + 1) % n i.e., Whenever a pointer is incremented, take modulo n. The above operations is needed to check if the queue is empty or full.

FIST, Multi Media University Cyclic Queue Consider when there is only one element in the queue In this case, both the pointers coincide (head == tail) The queue becomes empty when the element is deleted TailHead Head,Tail Only one element Queue Empty Queue

FIST, Multi Media University Cyclic Queue For an empty queue, we have the condition head = (tail + 1) % n The above condition is also satisfied when the queue is full We resolve the ambiguity between two cases if the condition head = (tail+1)% n is satisfied up on deleting an item from the queue, then queue is empty if the condition head = (tail + 1) % n is satisfied up on inserting an item in to the queue, then queue is full.

FIST, Multi Media University implementation How to Add? void Addend(int x) { if (!Full()) { tail=(tail+1)%MAX; a[tail]=x; count++; cout<<x<<" added to the queue"<<endl; } else cout<<"Full Queue"<<endl; }

FIST, Multi Media University implementation How to Delete? int Serve() { int x; if (!Empty()) { x=a[head]; head=(head+1)%MAX; count--; cout<<x<<" served from the queue"<<endl; return x; } else cout<<"Empty Queue"<<endl; }