Implement stack using arrays.

 #include<stdio.h>

#define size 5

int stack[size],top=-1;

void push();

void pop();

void status();

void traverse();

int main()

{

int ch;

while(1)

{

printf("\nEnter Your Choice\n 1.Push\n 2.Pop\n 3.Traverse\n 4.Status\n 5.Exit");

scanf("%d",&ch);

switch(ch)

{

case 1:push();

break;

case 2:pop();

break;

case 3:traverse();

break;

case 4:status();

break;

default:exit(0);

}

}

return 0;

}

void push()

{

int item;

if (top==size-1)

printf("\n Stack Overflow,insertion not possible");

else

{

printf("\n Enter data to insert");

scanf("%d",&item);

top=top+1;

stack[top]=item;

}

}

void pop()

{

if(top==-1)

printf("\n Stack Underflow,pop not possible");

else

{

printf("\n Deleted item is %d",stack[top]);

top=top-1;

}

}

void traverse()

{

int i;

if(top==-1)

printf("\n Stack Underflow");

else

{

for(i=top;i>=0;i--)

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

}

}

void status()

{

if(top==-1)

printf("\n Stack Underflow");

else if(top==size-1)

printf("\n Stack Overflow");

else

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

}

Comments

Popular posts from this blog

Create a Binary Search Tree of integers and perform the following operations (i)insert (ii) delete (iii). Search (iv) traversals (pre-order, in-order, post-order) BST

Creation Of DLL

Develop non-recursive functions to perform search for a Key value in a given list using (i) Binary Search Non Recursive