LAB PROGRAMME

 

STACK  USING  ARRAY   1     

#include

#include

#include

int top=-1,n;

void main()

{

            void push(int[],int);

void pop(int[]);

void peep(int[],int);

void display(int[]);

int stack[50],op,ele,pos,i,ch;

clrscr();

printf("\n\t\t\tSTACK USING ARRAY");

printf("\n\t\t\t*********************");

printf("\nEnter the maximum size of the stack:");

scanf("%d",&n);

do

{

             printf("\nOperations:");

             printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit");

             printf("\nEnter your choice:");

             scanf("%d",&op);

             switch(op)

                        {

                                     case 1:

                                     printf("\nEnter the value to be store:");

                                     scanf("%d",&ele);

                                     push(stack,ele);

                                     break;

                                     case 2:

                                     pop(stack);

                                     break;

                                     case 3:

                                     printf("\nEnter the position of element to print:");

                                     scanf("%d",&pos);

                                     peep(stack,pos);

                                     break;

                                     case 4:

                                     display(stack);

                                     break;

 case 5:

             exit(0);

 break;

                         }

                         printf("\nDo you want to continue(y/n):");

 ch=getche();

}

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

}

void push(int stack[],int ele)

{

 top++;

 if(top>n-1)

 printf("\nStack is full");

 else

 {

  stack[top]=ele;

 }

}

void pop(int stack[])

{

 if(top<0)

 printf("\nStack is empty");

 else

 {

  printf("\nThe element to be removed is %d",stack[top]);

  top--;

 }

}

void peep(int stack[],int pos)

{

 if(top>=pos-1)

 {

  printf("\nElements stored at position %d is %d",pos,stack[pos-1]);

 }

 else

 printf("\nPosition %d exceeds the top value",pos);

}

void display(int stack[])

{

 int i;

 printf("\nElements present in the stack are:");

 for(i=0;i<=top;i++)

 printf("%d\t",stack[i]);

}

 

 

OUTPUT :

Stack using Array

*********************

Enter the maximum size of the stack

5

Possible operations are

1. Push

2 .Pop

3. Peep

4. Display

5. Exit

Enter your choice1

Enter the value to be store

5

Do u want to continue (y/n) y

Enter your choice1

Enter the value to be store

10

Do u want to continue (y/n) y

Enter your choice 2

The element to be removed id 10

Do u want to continue (y/n) y

Enter your choice 3

Enter the Position of element to print

1

Element stored at position 1 is 5

Do u want to continue (y/n) y

Enter your choice4

Elements present in the Stack are

5

Do u want to cntinue (y/n) n

Search site

© 2010 All rights reserved.