Tuesday, September 2, 2014

Stack in C

Stack in C





//Use Code blocks

#include<stdio.h>

#define size 5

char stack[size][15],ele[20];

int tos;

void push();

char* pop();

void show();

int isempty();

int isfull();

int main()
{
int choice;
tos=0;
do
{
printf("\tEnter 1 for push, 2 for pop, 3 to show, and 4 to exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if (isfull())
printf("\nThe stack is full");
else
{
printf("\n Enter element to insert");
scanf("%s",ele);
push(ele);
}
break;
case 2:
if(isempty())
printf("\n The stack is empty");
else
printf("\nThe removed element is:%s",pop());
break;
case 3:
if(isempty())
printf("\nThe stack is empty, I cant show you any elements");
else
show();
break;
case 4:
exit(1);
default:
printf("\nDo you understand english and numbers??");
}

}while(1);


    return 0;
}
int isempty()
{
return(tos==0);
}
int isfull()
{
return(tos==size);
}
void push(ele)
{
strcpy(stack[tos],ele);

tos++;
}

char* pop()
{
tos--;

return(stack[tos]);
}
void show()
{
int x=tos;

printf("\nThe Stack elements are.....\n");

while(x!=0)

printf("\t%s\n",stack[--x]);

}

No comments:

Post a Comment