Stack

 STACK

>> stack is a linear Data Structure . i.e the data elements organised in sequential order like array.

>> Stack is a static data structure, i.e it will stare limit elements.

>>It is based on array concept.

>>Stack is called FILO( first in last out ) i.e that elements which are inserted first displays last (or) LIFO( last in first out )i.e the elements which are inserted last displays first.

TERMINOLOGY OF STACK

>>Top of stack

>>Over flow

>>Under flow

>>In stack insertion ,deletion will be held at only on end called 'Top' of stack.

>>If stack is full, when you call member function push( ),if will display o/pas over flow.

>>If stack is empty ,when you call member function pop( ),it will display o/p as under flow

Functions of STACK

1.push()

2.pop()

3.show()

4.quit()

Push():- It is a Member-function of a stack which is used to insert a new element, into stack .

pop ():-It is a Member function, which is used to delete an element from stack .

>>In stack insertion ,deletion will be healed on only one end called 'Top' of stack .

show ():- It is Member-function of stack which is used to display elements of a stack in in FILO (or) LIFO method .

quit():-It is a Member function, which is used to exit from a stack operation .

Operations of stack:-


int sta[3];
1】show( )
2】pop( )
3】i.push(11)

     ii.push(22)

    iii.push(33)

4】push(44)



5】show()

6】pop( )


7】show( )

8】push(55)

9】show( )

10】pop( )

11】pop()

12】show( )

13】pop( )

14】show( )   


15】pop( )

     o/p Under flow

Applications of stack

>> converting infix expression to postfix and postfix expression

>> Evaluation postfix expression .

>>Checking well fermed (nested) parenthesis

>>Reversing a string.

>>Processing function call.

>>Paring (Analysis the structure) of computer Programs.

>> Simulating recursion .

>>In backtracking algorithm i.e often used in optimisation and in games.

Program on stack


#include<stdio.h>

#include<process.h>

#include<iostream.h>

#include<conio.h>

#define MASTK 3

class stack

{

private:

int a[MASTK],top;

public:

stack()

{

top=-1;

}

void push();

void pop();

void show();

void menu();

};

void stack :: push()

{

if(top==(MASTK-1))

{

cout<<"over flow\n";

return;

}

top=top+1;

cout<<"Enter ele into stack\n";

cin>>a[top];

}

void stack ::pop()

{

if(top==-2)

{

cout<<"under flow\n";

return;

}

cout<<"ele is deleted ="<<a[top]<<endl;

top=top-1;

}

void stack :: show()

{

if(top==-1)

{

cout<<"stack is empty\n";

return;

}

cout<<"stack ele are \n";

int i;

for(i=top;i>=0;i--)

cout<<a[i]<<endl;

}

void stack :: menu()

{

cout<<"stack operations\n";

cout<<"1.push()\n2.pop()\n3.show()\n4.exit()\n";

}

void main()

{

int opt;

char ch;

stack s;

do

{

clrscr();

s.menu();

cout<<"Enter your opction\n";

cin>>opt;

switch(opt)

{

case 1:s.push();

break;

case 2:s.pop();

break;

case 3:s.show();

break;

case 4:exit(0);

}

cout<<"Do you want to continue\n";

cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

}



Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat