STACK A STACK is an Abstract Data Type ( ADT ) based on principle of LIFO ( Last In First Out ) that serves as a collection of elements. A STACK has only one end called TOP. It has two operations : PUSH which adds an element to the collection at the TOP and POP which removes the most recently added element from the TOP. Overflow : When the Stack is full , new elements cannot be Pushed into it . This condition is called Overflow. Underflow : When the Stack is empty, elements cannot be Popped from it . This condition is called Underflow.
15
Embed
Functions in mysql · STACK A STACK is an Abstract Data Type ( ADT ) based on principle of LIFO ( Last In First Out ) that serves as a collection of elements. A STACK has only one
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
STACK
A STACK is an Abstract Data Type ( ADT ) based on principle of LIFO ( Last In First Out ) that serves as a collection of elements. A STACK has only one end called TOP. It has two operations : PUSH which adds an element to the collection at the TOP and POP which removes the most recently added element from the TOP.
Overflow : When the Stack is full , new elements cannot be Pushed into it . This condition is called Overflow. Underflow : When the Stack is empty, elements cannot be Popped from it . This condition is called Underflow.
STATIC STACK DYNAMIC STACK
A Static Stack is implemented using a predefined Array .
Dynamic Stack is implemented using a Linked List which uses pointers to dynamically allocate memory for new elements.
The size of Static Stack remains constant through out execution and is not increased or decreased by the Push or Pop operations.
A Dynamic Stack allocates memory when it’s needed and frees memory when the element no longer exists on the stack. Therefore a Dynamic Stack grows and shrinks as per Push and Pop operations.
IMPLEMENTATION OF STACK
STATIC STACK
#include<iostream.h> #include<conio.h> #include<stdio.h> #define MAX 5 struct city {
double pincode; char cityname[50];
};
class sstack { city c[MAX]; int top; public: sstack(); void push(); void pop(); void print(); };
sstack::sstack() {
top=-1; }
void sstack :: push() { if( top == MAX-1 ) cout<<“Overflow stack is full"; else
{ top++; cout<<"enter the pincode of new city "; cin>>c[top].pincode; cout<<"enter the name of new city "; gets(c[top].cityname); }
}
void sstack :: print() { if( top == -1) cout<<“Underflow stack is empty"; else
main() for STATIC STACK void main() { clrscr(); sstack s; int ch; do {
cout<<"\n 1.push operation"; cout<<"\n 2.pop operation"; cout<<"\n 3.display the contents of the stack"; cout<<"\n 4.exit"; cout<<"\n enter your chioce "; cin>>ch;
switch(ch) { case 1 : s.push(); break; case 2 : s.pop(); break; case 3 : s.print(); }
} while(ch!=4); getch(); }
Points to Remember: A Static stack is implemented through a
predefined Array. Top is an integer variable which keeps track of the
topmost element. To push a new element , Top is incremented to
refer to the next element and data is input there. When the Top reaches (MAX-1), overflow occurs. To Pop an element you need to decrement the
value of Top so that it now refers to the element below the current element , although physical deletion is not possible
temp = new emp; //create new node if(temp == NULL) cout<<“Overflow memory is full"; else { cout<<"enter the new employee number "; cin>>temp->empno; cout<<"enter the new employee name "; gets(temp->empname);
cout<<"\n 1.push operation"; cout<<"\n 2.pop operation"; cout<<"\n 3.display the contents of the stack"; cout<<"\n 4.exit"; cout<<"\n enter your chioce "; cin>>ch;
switch(ch) { case 1 : d.push(); break; case 2 : d.pop(); break; case 3 : d.print(); }
} while(ch!=4); getch(); }
void dstack::pop() { if(top==NULL) cout<<“Underflow stack is empty"; else
A QUEUE is an Abstract Data Type ( ADT ) based on principle of FIFO ( First In First Out ) that serves as a collection of elements. A Queue has two ends called FRONT and REAR. It has two operations : Insertion or Enqueue which adds an element at the REAR end and Deletion or Dequeue which removes the first element from the FRONT.
Overflow : When the Queue is full , new elements cannot be Inserted into it . This condition is called Overflow. Underflow : When the Queue is empty, elements cannot be removed. This condition is called Underflow.
STATIC QUEUE
#include<iostream.h> #include<conio.h> #include<stdio.h> #define max 5 struct book {
void dqueue::insert() { bus *temp; temp = new bus; //create new node
if(temp == NULL) cout<<“Overflow - Queue is full"; else if ( front == NULL && rear == NULL ) { front = rear= temp; // first insert
cout<<"enter the new bus number "; cin>> temp -> busno; cout<<"enter the new passenger name "; gets(temp -> passengername); temp -> ptr = NULL;
} else { rear->ptr=temp; //after first insert
rear=temp; cout<<"enter the new bus number "; cin>> temp -> busno; cout<<"enter the new passenger name "; gets(temp -> passengername); temp -> ptr = NULL;
} }
void dqueue :: remove() { if( front== NULL && rear == NULL) cout<<“Underflow -Queue is empty"; else if (front == rear ) //only one node { bus *temp;
temp=front; front= rear=NULL; delete temp;
} else //more than one node { bus* temp; temp=front; front=front->ptr; delete temp; } }
void dqueue :: print() { bus *temp; if ( front == NULL && rear == NULL) cout<<“Underflow – Queue is empty"; else { for(temp = front ; temp != NULL ; temp = temp->ptr ) { cout<<temp->busno<<"\t"; cout<<temp->passengername<<"\n"; } } }
void main() { clrscr(); dqueue d; int ch; do{ cout<<"\n1.insert operation"; cout<<"\n2.remove operation"; cout<<"\n3.display the contents of queue"; cout<<"\n4.exit"; cout<<"\nenter your choice "; cin>>ch; switch(ch) { case 1 : d.insert();break; case 2 : d.remove();break; case 3 : d.print(); } }while(ch!=4); getch(); }
Circular Queue is implemented in circular form rather then a straight line. The opposite ends in circular queue are considered contiguous. They overcome the problem of unutilized space in static queues implemented as arrays. If queue is not full and space is available in the beginning then the rear shifts back to the beginning after reaching the maximum index number.
Overflow : When the Queue is full , new elements cannot be Inserted into it . This condition is called Overflow. Underflow : When the Queue is empty, elements cannot be Removed. This condition is called Underflow.
void cqueue :: insert() { if ( front == (rear+1) % max ) cout<<“Overflow - Queue is full"; else if(front == -1 && rear == -1 ) { front= rear=0; // first insert
cout<<"enter the new flight number "; cin>>f[rear].flightno; cout<<"enter the new flight name "; gets(f[rear].flightname);
} else { rear= (rear +1) % max ; cout<<"enter the new flight number "; cin>>f[rear].flightno; cout<<"enter the new flight name "; gets(f[rear].flightname);
} } void cqueue :: remove() { if ( front == -1 && rear == -1 ) cout<<“Underflow queue is empty "; else if(front==rear) front= rear=-1; // only node removal
else front = (front+1) % max ; }
CIRCULAR QUEUE void cqueue :: print() { if ( front == -1 && rear == -1 ) cout<<“Underflow – queue is empty "; else { for(int i = front ; i != rear ; i = ( i+1) % max )
cout<<f[i].flightno<<"\t"<<f[i].flightname<<"\n“; //display rear also }
}
void main() { clrscr(); cqueue c; int ch; do{
cout<<"\n 1.insert operation"; cout<<"\n 2.delete operation"; cout<<"\n 3.display the contents of queue"; cout<<"\n 4.exit"; cout<<"\n enter your choice "; cin>>ch; switch(ch) {
case 1 : c.insert();break; case 2 : c.remove();break; case 3 : c.print();
} }while ( ch != 4 ) ; getch(); }
NO. OF ELEMENTS IN CIRCULAR QUEUE
= ( front > rear ) ? (MAX + (rear - front + 1) ) : (rear - front + 1)
Ques : Give the number of elements in circular queue with 12 memory locations if front=10 and rear=3. Also specify the value of front and rear , if 4 elements are deleted from the queue. Ans: No. of elements = MAX + (rear - front + 1) = 12 + 3 -10 +1 = 6 There are 12 elements from 0 till 11. Now, If 4 elements are deleted ( i.e. 10 , 11 , 0 , 1 ) Then front =2 and rear =3