Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack and Queues Stack implementation using Array

Similar presentations


Presentation on theme: "Stack and Queues Stack implementation using Array"— Presentation transcript:

1 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 Convert Postfix into Infix

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

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

4 Front void Insert ( ) { //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 -> RollNo ) ; ptr - > link =NULL; // Insert Node (Element) if ( Front = = NULL ) { Front=Rear = ptr ; } else Rear- > link =Ptr ; Rear= ptr ; } Rear Null Null InfoPart Link Ptr Null Front InfoPart Link InfoPart Link Rear Null InfoPart Link Null Ptr

5 Front void Delete ( ) { student *ptr ; if ( Front = = NULL ) { cout<<“Underflow “ ; return; } ptr=front; if (front==rear) front=rear=NULL; } else front=front - > link ; cout<<ptr->name<<“is deleted”; delete ptr ; Null Front InfoPart Link InfoPart Link InfoPart Link Null Rear Ptr

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

7 Queue using Array Insert 10 Insert 20 Insert 30 Delete Insert 50 4 3
Front 30 2 Rear Front 20 1 Rear Front 10 Rear Front = -1,Rear=-1 50

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

9 void Insert ( int Num [ ] , int & Front , int & Rear) { int item ; //Check overflow condition if ( Rear == max – 1 ) cout<<“Overflow “; return; } if (Rear==-1) front=rear=0; else Rear =Rear +1; cout << “Enter Item To Push into the stack\n; cin >> item; Num [ Rear ] =item; cout<<“Item Inserted “;

10 void Delete ( int Num [ ] , int &Rear, int &Front ) { //Check underflow condition if ( Front == – 1 ) cout<<“underflow “; return; } if (Front==Rear) cout << Num[ Front ]<<“is to be deleted \n“; Front= Rear =-1; else Front=Front+1;

11 void show ( int Num [ ] , int Front ,int Rear ) { //Check underflow condition if ( Front== – 1 ) cout<<“underflow “; } else for ( i= Front; i <=Rear ; i ++ ) cout<< Num [ i ] << endl ;

12


Download ppt "Stack and Queues Stack implementation using Array"

Similar presentations


Ads by Google