Friday 14 September 2012

Program that implement stack and its operation by using the arrays

Description:
In this program we have to implement the stack operation by using the arrays. Here they stack operation are  push and pop. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements in to a stack
           ALGORITHM FOR INSERTING AN ELEMENT IN A STACK:

Function Push(s,top,x)
Step 1: [Check for stack overflow]
                         If top>=n
                         Then printf(“stack overflow”)
                          Return
Step 2: [Increment Top]
                        Top<-top+1

Step 3: [ Insert element]
                        S[top]=x

Step 4:[finished]
                        Return

         ALGORITHM FOR DELETING AN ELEMENT FROM A STACK:
Function POP(s,top)
Step 1: [Check for stack underflow]
             If top=0
                         Then printf(“stack underflow”)
                          Exit
Step 2: [Decrement Top]
                        Top=top-1

Step 3: [Return former top element of stackwwwww]
                        Return(S[top+1])

Step 4:[finished]
                        Return

Program:

# include <stdio.h>
# define size 4
int choice,top=0,a[size],item;
main()
{
clrscr();
while(1)
{
printf(" *** MENU ***\n 1. PUSH\n 2. POP\n 3.   
         TRAVERSE\n  4. EXIT\n");
printf("enter your choice from menu:");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
       break;
case 2:pop();
       break;
case 3:traverse();
       break;
case 4:exit();
default:printf("wrong choice\n");
}
}
getch();
}
push()
{
if(size==top)
printf("*** stack is full ***\n");
else
{
printf("enter the item to be pushed into the stack:");
scanf("%d",&item);
top++;
a[top]=item;
}
}

pop()
{
if(top==0)
printf("*** stack is empty ***\n");
else
{
item=a[top];
top--;
printf("the deleted item from stack is %d\n",item);
}
}
traverse()
{
int i;
if(top==0)
printf("**** stack is empty ****");
else
{
printf("*** stack display ***\n");
for(i=1;i<=top;i++)
if(i==top)
printf("%d at %d ->top\n",a[i],i);
else
printf("%d at %d\n",a[i],i);
}
}

                   Input/Output:

*** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:1
enter the item to be pushed into the stack:11
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:1
enter the item to be pushed into the stack:12
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:1
enter the item to be pushed into the stack:13
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:1
enter the item to be pushed into the stack:14
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:1
*** stack is full ***
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:3
*** stack display ***
11 at 1
12 at 2
13 at 3
14 at 4 ->top
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:2
the deleted item from stack is 14
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:2
the deleted item from stack is 13
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:2
the deleted item from stack is 12

 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:2
the deleted item from stack is 11
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:2
*** stack is empty ***
 *** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:3
**** stack is empty ****
*** MENU ***
 1. PUSH
 2. POP
 3. TRAVERSE
 4. EXIT
enter your choice from menu:4


            conclusion: the program is error free


VIVA QUESATIONS:
1) Define Stack ?
Ans: A stack is a linear data structure in which a data item is inserted and deleted at one end
2) Define data structure ?
Ans: A data structure is a collection of organized data that are related to each other

3)  What are the various operation performed on the stack ?
Ans: push(), pop()






No comments:

Post a Comment