Thursday, 24 November 2011

WAP for Stack implementation through Array


#include <stdio.h>
#include<conio.h>
# define MAXSIZE 200
void push(char []);
void display();
char stack[MAXSIZE];
int top; //index pointing to the top of stack
int main()
{
void pop();
int will=1,i;
char nam[10];


while(will ==1)
{
printf("\nMAIN MENU:\n1.Add element to stack\n2.Delete element from the stack");
scanf("%d",&will);

switch(will)
{
case 1:
printf("Enter the data... ");
scanf("%s",nam);
push(nam);
break;
case 2:pop();

break;

case 3:display();
 break;
default: printf("Invalid Choice . ");
}

printf("\n do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of  outer while
}               //end of main


void display()
{
int i;
for(i=0;i<top;i++)
printf("%c",stack[i]);
}


void push(char a[])
{

int t;
if(top>MAXSIZE)
{
printf("stack FULL");
return;
}
else
{
 t=strlen(a);
 strcpy(stack,a);
 top+=t;
 stack[top]=' ';
 top++;
 printf("\n top=  %d\n",top);
}
}

void pop()
{
char a;
if(top<0)
{
printf("stack EMPTY");
return;
}
else
{
top--;
while(stack[top] != ' ')
{

a=stack[top];
top--;
}
//return(a);
}
}

No comments:

Post a Comment