Stack and Queues Stack implementation using Array

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§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.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Any Other Data Structure Array Linked List
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
STACKS & QUEUES for CLASS XII ( C++).
Linked List Insert as a first node Insert as a last node
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
Stacks.
STACKS.
Stacks Stack: restricted variant of list
Stacks Chapter 4.
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stack.
INSERTION INTO A LINEAR ARRAY Set J = N Repeat step 3 and 4 while J>= K Set LA[ J+1] = LA [ J ] Set J = J-1 Set LA [K] = ITEM Set N = N+1 Exit.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
CSC 143 Stacks [Chapter 6].
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks Data structure Elements added, removed from one end only
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks CS-240 Dick Steflik.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
5.3 Implementing a Stack Chapter 5 – The Stack.
LINEAR DATA STRUCTURES
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Stack and Queues Stack implementation using Array Stack implementation using Linked List Queue implementation using Array Queue implementation using Linked List Convert Infix Exp into Postfix Evaluate Postfix Expression

Stack using Array Push 10 Push 20 Push 30 Pop Push 50 4 3 30 2 Top 20 Top Top = -1 50

const int max=100; void main ( ) { int DATA [max ] ,Top= -1, c ; do clrscr( ); cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : push ( DATA , Top) ; break ; case 2 : pop ( DATA , Top) ; break ; case 3 : show ( DATA , Top) ; break ; } } while(c>=1 && c<= 3);

void push ( int Num [ ] , int & Top ) { int item ; //Check overflow condition if ( Top == max – 1 ) cout<<“Overflow “; } else cout << “Enter Item To Push into the stack\n”; cin >> item; Top = Top +1; Num [ Top ] =item; cout<<“Item Inserted “;

void pop ( int Num [ ] , int & Top ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << Num[ Top ]<<“is to be deleted \n“; Top = Top -1; cout<<“Item deleted “;

void show ( int Num [ ] , int Top ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << Num[ Top ]<<“Top \n“; for ( i= Top -1; i > = 0 ; i - - ) cout<< Num [ i ] << endl ;

char * msg [ ] = {“over flow”, “under flow”}; class Stack { int Top ; int DATA [10]; void error (int e_num) { cout<< msg [e_num ] ; } public: void init () { Top = -1 ; } void Push ( int ); void Pop ( ); }; Define Push which accept an item and insert into stack. Define Pop to delete an item from stack. ( Use error ( ) function for underflow and overflow condition

void push ( int Num [ ] , int & Top ) { int item ; //Check overflow condition if ( Top == max – 1 ) cout<<“Overflow “; } else cout << “Enter Item”; cin >> item; Top = Top +1; Num [ Top ] =item; cout<<“Item Inserted “; void Stack :: push ( int item ) { int item ; //Check overflow condition if ( Top == 10 – 1 ) error(0); } else cout << “Enter Item”; cin >> item; Top = Top +1; DATA [ Top ] =item; cout<<“Item Inserted “;

void Stack :: pop ( ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << DATA[ Top ]<<“is to be deleted \n“; Top = Top -1; cout<<“Item deleted “;

Linked List Linked list is a data structure in which an element of list point to another element of list. It is implemented with the help of pointers and self referential structure. Each element ( also called node ) contain two fields : Information part and Pointer. Pointer struct Student { int RollNo; char Name[20] ; Student * link ; } InfoPart Link InfoPart Link InfoPart Link Null

Stack Implementation Using Linked List Stack is a data structure in which top pointer point to top element of stack. It is implemented with the help of pointers and self referential structure. Each element ( also called node ) contain two fields : Information part and Pointer. Top struct Student { int RollNo; char Name[20] ; Student * link ; } InfoPart Link InfoPart Link InfoPart Link Null

cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; struct Student { int RollNo; char Name[20] ; Student * link ; } ; student *Top = NULL; void main ( ) int c ; do clrscr( ); cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : push ( ) ; break ; case 2 : pop ( ) ; break ; case 3 : show ( ) ; break ; } } while(c>=1 && c<= 3);

Top void push ( ) { //create node Student *ptr ; ptr = new Student ; if ( ptr == NULL ) // checking overflow { cout << “Overflow “ ; return ; } //assigning Values cout << “Enter RollNo “ ; cin >> ptr -> RollNo; cout << “Enter Name “ ; gets( ptr -> Name ) ; ptr - > link =NULL; // Insert Node (Element) if ( Top = = NULL ) { Top = ptr ; } else ptr - > link =Top ; top = ptr ; } Null InfoPart Link Ptr Null Top InfoPart Link InfoPart Link InfoPart Link Null Null Ptr

Top void Pop ( ) { student *ptr ; if ( Top = = NULL ) { cout<<“Underflow “ ; } else ptr=top; cout<<ptr->name<<“being deleted”; Top=Top - > link ; delete ptr ; } Null Top InfoPart Link InfoPart Link Null Ptr

void Display ( ) { student void Display ( ) { student *ptr ; if ( Top = = NULL ) { cout<<“Underflow “ ; } else ptr=top; while (ptr!=NULL) cout<<“Name”<<ptr->Name<<“,”<<“RollNo”<<ptr->RollNo; ptr=ptr - > link ; }

Convert Infix expression into Postfix expression Suppose expression X in infix notation and X is to be converted in equivalent postfix expression Y. Put X inside “(“ and “) “ Scan X from Left to Right and follow steps 3 to 6 until X is empty. If an operand is encountered , add it to Y. If an operator is encountered , then Repeatedly pop from STACK and add to Y each operator on the top of the stack which has the same precedence as or higher precedence than operator. Push operator to stack If a right “)” is encountered , then Repeatedly pop from STACK and add to Y each operator on the top of the stack until left “(” is encountered . Remove left “(“ from stack. If a left “)” is encountered , then Push to stack End

Convert Infix into Postfix Exp :- (( X + Y ) / ( Z * Y ) –R) No Ele Action Stack Postfix Exp Y 1 ( Push 2 ( , ( 3 X Add to Y 4 + ( , ( , + 5 Y XY 6 ) Pop + and add, Remove ( XY + 7 / ( , / 8 ( , / ,( 9 Z Add ( , / , ( XY+ Z 10 * ( , / , ( , * 11 ( , / , ( , * XY+ZY 12 Pop * ,Add to Exp , Remove ( ( , / XY+ZY * 13 - ( , - XY+ZY * / 14 R ( , - XY+ZY * / R 15 Pop - ,Add to Exp ,Remove ( Empty XY+ZY * / R -

Evaluation of Postfix expression Suppose expression X in postfix expression. Scan X from Left to Right and follow steps 2 to 4. If an operand is encountered , push on to stack. If an operator is encountered , then Pop two operand from stack (pop one operand for unary operator ) Evaluate expresseion formed by (Operand1) Operator (Operand2). Push the result on to stack If X is empty (no more element) , then pop the resul from stack End

Evaluation of Postfix Exp Ele Action Stack Calculation 1 10 Push 2 3 10 , 3 * Pop 10 , 3 / cal / push Result 30 10*3=30 4 7 30,7 5 30,7,1 6 - Pop 7, 1 / cal / push Result 30, 6 7-1=6 Pop 30 , 6 / cal / push Result 180 30*6=180 8 23 180,23 9 + Pop 180 , 23 / cal / push Result 203 180+23 Ans :- 203