functions in mysql · stack a stack is an abstract data type ( adt ) based on principle of lifo (...

15
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.

Upload: others

Post on 08-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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.

Page 2: 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

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

Page 3: 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

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

for( int i = top ; i >= 0 ; i-- ) // LIFO

{ cout<<c[i].pincode; cout<<c[i].cityname<<endl; }

}

void sstack :: pop() { if(top == -1) cout<<“Underflow stack is empty"; else

top--; }

-----output (LIFO) -------

250002 Merut

282001 Agra

201304 Noida

122011 Gurgaon

110026 New Delhi

Page 4: 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

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

When Top equals -1 underflow occurs.

Page 5: 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

DYNAMIC STACK #include<iostream.h> #include<conio.h> #include<stdio.h> struct emp {

int empno; char empname[20]; emp *ptr;

}; class dstack { emp *top; public: dstack(); void push(); void pop(); void print(); ~dstack(); };

dstack :: dstack() { top = NULL; }

void dstack :: push() { emp* temp;

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);

temp -> ptr = top; // linking top=temp; // change top }

} void dstack :: print() { if(top == NULL) cout<<“Underflow stack is empty"; else

{ emp* temp; for( temp = top ; temp != NULL; temp = temp->ptr) { cout<<temp->empno<<endl; cout<<temp->empname<<endl; } }

}

Page 6: 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

DYNAMIC STACK

void main() { clrscr(); dstack d; 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 : 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

{ emp* temp; temp=top; top=top->ptr; delete temp; }

} dstack::~dstack() { while(top!=NULL) pop(); }

Page 7: 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

QUEUE

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.

Page 8: 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

STATIC QUEUE

#include<iostream.h> #include<conio.h> #include<stdio.h> #define max 5 struct book {

int bookno; char bookname[20];

}; class squeue {

book b[max]; int front , rear;

public: squeue() ; void insert(); void remove(); void print();

}; squeue :: squeue() { front=-1 ; rear=-1; }

void squeue :: insert() { if (rear == max-1) cout<<“Overflow queue is full";

else if( front == -1 && rear == -1 ) { front = rear = 0; // first insert

cout<<"enter new book number "; cin>>b[rear].bookno; cout<<"enter new book name "; gets(b[rear].bookname);

} else { rear++; //after first insert

cout<<"enter new book number "; cin>>b[rear].bookno; cout<<"enter new book name "; gets(b[rear].bookname); }

}

Page 9: 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

STATIC QUEUE void squeue :: remove() { if(front == -1 && rear == -1 ) cout<<“Underflow queue is empty"; else if ( front == rear) //only single node

{ front = rear = -1 ; } else // more than one node

front++ ; } void squeue :: print() { if(front == -1 && rear == -1) cout<<"underflow queue is empty "; else { for(int i =front ; i <= rear ; i++ ) { cout<<b[i].bookno<<"\t"; cout<<b[i].bookname<<"\t"; } } }

void main() { clrscr(); squeue s; 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: s.insert() ; break; case 2: s.remove() ; break; case 3: s.print();

} }while(ch!=4); getch(); }

Page 10: 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

DYNAMIC QUEUE

#include<iostream.h> #include<conio.h> #include<stdio.h> struct bus {

int busno; char passengername[20]; bus *ptr;

};

class dqueue { bus *front, *rear; public:

dqueue(); void insert(); void remove(); void print(); ~dqueue();

};

dqueue :: dqueue() { front = rear = NULL; }

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;

} }

Page 11: 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

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"; } } }

dqueue :: ~dqueue() { while(front!=NULL) { remove(); } }

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(); }

Page 12: 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

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.

Page 13: 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

CIRCULAR QUEUE #include<iostream.h> #include<conio.h> #include<stdio.h> #define max 6 struct flight { double flightno; char flightname[20]; }; class cqueue { flight f[max]; int front , rear ; public: cqueue(); void insert(); void remove(); void print(); }; cqueue :: cqueue() { front=-1;rear=-1; }

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 ; }

Page 14: 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

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"; }

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(); }

Page 15: 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

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