/* http://native-code.blogspot.com */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void push(struct node**,int);
int pop(struct node**);
void display(struct node**);
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *s=NULL;
int i,choice,x,item;
do
{
clrscr();
printf("\n\n\n\t :: menu:: \n");
printf("\n\n\t (1) push element");
printf("\n\n\t (2) pop element");
printf("\n\n\t (3) exit.");
printf("\n\n\t enter your choice ");
choice=getche();
clrscr();
switch(choice)
{
case '1':
printf("\n\n\t Enter a number");
scanf("%d",&x);
push(&s,x);
printf("\n\n\t The elements are");
display(&s);
getch();
break;
case '2':
item=pop(&s);
printf("\n\n\t The top element %d is popped...",item);
printf("\n\n\t The rest elements are...");
display(&s);
getch();
break;
case '3':
exit();
}
}while(1);
}
void push(struct node **top,int item)
{
struct node *new1;
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
printf("\n\n\t Stack is full");
new1->data=item;
new1->link=*top;
*top=new1;
}
int pop(struct node **top)
{
struct node *temp;
int item;
if(*top==NULL)
{
printf("\n\n\t Stack is empty.");
return NULL;
}
temp=*top;
item=temp->data;
*top=(*top)->link;
free(temp);
return item;
}
void display(struct node **head)
{
struct node *ptr;
ptr=*head;
while(ptr!=NULL)
{
printf(" %d",ptr->data);
ptr=ptr->link;
}
}
IDE Used To Test This Code : Turbo C
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.
Post a Comment