/* Stack Using Linked List*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct stack
{
int data;
struct stack *next;
}node;
node *top=NULL;
node *getnode()
{
int val;
node *temp;
printf("Enter val");
scanf("%d",&val);
temp=(node *)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
return temp;
}
void push()
{
node *neww;
neww=getnode();
neww->next=top;
top=neww;
}
void pop()
{
if(top==NULL)
{
printf("\nStack is Empty!\n");
}
else
{
node *t;
t=top;
printf("\n%d is deleted\n",top->data);
top=top->next;
t->next=NULL;
free(t);
}
}
void peep()
{
printf("\nTop element is %d",top->data);
}
void display()
{
if(top==NULL)
{
printf("\nEmpty!\n");
}
else
{
node *t;
t=top;
while(t)
{
printf("\n%d",t->data);
t=t->next;
}
printf("\n");
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\nYour choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("\nProper choice plz!\n");
}
}
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct stack
{
int data;
struct stack *next;
}node;
node *top=NULL;
node *getnode()
{
int val;
node *temp;
printf("Enter val");
scanf("%d",&val);
temp=(node *)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
return temp;
}
void push()
{
node *neww;
neww=getnode();
neww->next=top;
top=neww;
}
void pop()
{
if(top==NULL)
{
printf("\nStack is Empty!\n");
}
else
{
node *t;
t=top;
printf("\n%d is deleted\n",top->data);
top=top->next;
t->next=NULL;
free(t);
}
}
void peep()
{
printf("\nTop element is %d",top->data);
}
void display()
{
if(top==NULL)
{
printf("\nEmpty!\n");
}
else
{
node *t;
t=top;
while(t)
{
printf("\n%d",t->data);
t=t->next;
}
printf("\n");
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\nYour choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("\nProper choice plz!\n");
}
}
}
No comments:
Post a Comment