Friday 7 December 2012

CS2208 Data Structure Lab - Implemention of Stack Using Array




Alogarithm steps:

Step 1: Define a stack size.

Step 2: Read the stack operation.

Step 3: Read the stack element.

Step 4: Check the stack operation is Push or Pop.

Step 5: If operation is push then check the stack status.

i. If stack status is over flow we can’t push the element in to stack.
ii. Other wise we can add the data into stack .
iii. Move top to next position.






USING C PROGRAM

//Program for Stack implementation through Array
#include
//#include
#define MAXSIZE 5
int stack[MAXSIZE];
int top=0;    //index pointing to the top of stack
    void main()
    {
        void push();
        void pop();
        void display();
        int will=1,i;
        clrscr();
        while(1)
        {
            printf("\n\n\nMAIN MENU:\n\n1.PUSH\n2.POP\n3.EXIT\n\nENTER YOUR CHOICE: ");
            scanf("%d",&will);
            switch(will)
            {
                case 1:
                push();
                display();
                break;
                case 2:
                pop();
                display();
                break;
                case 3:
                exit(0);
                break;
                default:
                printf("Invalid Choice . ");
            }


        } //end of  outer while
    }               //end of main


    void push()
    {
        int num;
        if(top>=MAXSIZE)
        {
            printf("\nSTACK FULL");
            return;
        }
        else
        {       if(top<0 br="br">             top=0;
            printf("\n\nENTER THE STACK ELEMENT :  ");
            scanf("%d",&num);
            stack[top++]=num;
        }
    }

    void pop()
    {
            if(top>=0)
            top--;

    }

    void display()
    {
        int i;
        if(top<=0)
        printf("\n\nSTACK EMPTY");
        else
        for(i=top-1;i>=0;i--)
        {
          printf("\n\n%d ",stack[i]);
          if(i==(top-1))
          printf("---->TOP");
        }
    }




USING C++ PROGRAM


//Stack implementation as a class.
# include
# include
# include
# define SIZE 5
class stack
{
int a[SIZE];
int tos; // Top of Stack
public:
    stack();
    void push(int);
    void display();
    void pop();
    int isempty();
    int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}

int stack::isempty()
{
return (tos==0?1:0);
}
int stack::isfull()
{
return (tos==SIZE?1:0);
}

void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
 cerr<}
}
void stack::pop()
{
if(!isempty())
{
--tos;
cout<}
else
{
cerr<}
}
void stack ::display()
{
  for(int i=tos-1;i>=0;i--)
  cout<
}
void main()
{
stack s;
int ch=1,num;
clrscr();
while(ch!=0)
{
    cout<    cout<<"ENTER YOUR CHIOCE : ";
    cin>>ch;
    switch(ch)
    {
    case 0:
        exit(1); //Normal Termination of Program
    case 1:
        cout<        cin>>num;
        s.push(num);
        s.display();
        break;
    case 2:
        s.pop();
        s.display();
        break;
    case 3:
        (s.isempty())?(cout<        break;
    case 4:
        (s.isfull())?(cout<        break;
    default:
        cout<    }
}//end of while
getch();
}




SAMPLE INPUT OUTPUT:
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 1
ENTER THE STACK ELEMENT : 10
10 ---->TOP
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 1
ENTER THE STACK ELEMENT : 20
20 ---->TOP
10
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 1
ENTER THE STACK ELEMENT : 30
STACK FULL
30 ---->TOP
20
10
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 2
20 ---->TOP
10
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 2
10 ---->TOP
MAIN MENU :
1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE : 1
STACK EMPTY




Don't You Think this Awesome Post should be shared ??
| CS2208 Data Structure Lab - Implemention of Stack Using Array |
Back To Top Related Posts Plugin for WordPress, Blogger...