using arrays
#include <stdio.h>
int stack[50],size,top=-1;
int main()
{
int ch;
int i;
void push();
void pop();
void display();
int isfull();
int isempty();
printf(“enter size of stack”);
scanf(“%d”,&size);
do
{
printf(“enter your choice”);
printf(“1.push”);
printf(“2.pop”);
printf(“3.display”);
printf(“4.exit”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf(“enter valid choice”);
}
}while(ch<=5);
}
void push()
{
int ele;
if(isfull())
{
printf(“overflow”);
}
else
{
printf(“enter element “);
scanf(“%d”,&ele);
top++;
stack[top]=ele;
printf(“element is inserted”);
}
}
void pop()
{
int ele;
if(isempty())
{
printf(“underflow”);
}
else
{
ele=stack[top];
top–;
printf(“element is deleted”);
}
}
void display()
{
int i;
if(top==-1)
{
printf(“stack is empty”);
}
else
{
printf(“the stack elements are”);
for(i=0;i<top;i++)
{
printf(“%d”,stack[i]);
}
}
}
int isfull()
{
if(top==size-1)
{
return 1;
}
else
{
return 0;
}
}
int isempty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
using linked list
#include <stdio.h>
#include <stdlib.h>
typedef struct stack
{
int data;
struct stack *link;
}node;
node *top=NULL,*temp,*newnode;
int main()
{
void push();
void pop();
void peek();
void display();
int ch;
char op;
//printf(“enter your choice”);
printf(“1.push”);
printf(“2.pop”);
printf(“3.peek”);
printf(“4.display”);
printf(“5.exit”);
do
{
printf(“enter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf(“enter valid choice”);
}
}while(ch<5);
return 0;
}
void push()
{
int dp;
printf(“enter datapart”);
scanf(“%d”,&dp);
newnode=(node*)malloc(sizeof(node));
newnode->data=dp;
newnode->link=NULL;
newnode->link=top;
top=newnode;
}
void pop()
{
if(top==NULL)
{
printf(“list is empty”);
}
else
{
temp=top;
printf(“deleted element is %d”,top->data);
top=top->link;
free(temp);
}
}
void peek()
{
if(top==NULL)
{
printf(“list is empty”);
}
else
{
printf(“the top most element is %d”,top->data);
}
}
void display()
{
temp=top;
printf(“start->”);
while(temp!=NULL)
{
printf(“%d->”,temp->data);
temp=temp->link;
}
}