/*Implementation of a Stack with basic (push & pop) operations using Link list.*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
}node;
typedef struct stack
{
struct node *top;
}stack;
/*Function to initilization of the Stack*/
void init(stack *);
/*Function to PUSH an element into the Stack*/
void push(int,stack *);
/*Function to POP an element into the Stack*/
int pop(stack *);
/*Function to Display the elements of the Stack*/
void display(stack *);
/*Main Function*/
void main()
{
int choice,poped,pushval;
stack *s;
s=(stack *) malloc(sizeof(stack));
init(s);
do
{
printf("\n\nENTER YOUR CHOICE \n1) To PUSH \n2) To POP\n3) To EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER A NUMBER TO PUSH: ");
scanf("%d",&pushval);
push(pushval,s);
printf("\nAFTER PUSH THE STACK IS:\n");
display(s);
break;
case 2:
poped=pop(s);
if (poped != -1)
{
printf("\n%d IS POPED\n",poped);
printf("\nAFTER POP THE STACK IS:\n");
}
display(s);
break;
case 3:
exit(1);
break;
default:
printf("\nEnter a Valid Choice.\n");
}
}
while(1);
}
void init(stack *s)
{
s->top=NULL;
}
void push(int val,stack *s)
{
node *p;
if((p=(node *) malloc(sizeof(node)))==NULL)
{
printf("\nMEMORY ALLOCATION UNSUCCESSFUL");
exit(1);
}
p->info=val;
p->next=s->top;
s->top=p;
}
int pop(stack *s)
{
int popval;
node *p;
if(s->top==NULL)
{
printf("\nSTACK IS EMPTY");
popval = -1;
}
else
{
p=s->top;
s->top=p->next;
popval=p->info;
free(p);
}
return popval;
}
void display(stack *s)
{
node *temp;
temp=s->top;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}
/*End of the program*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
}node;
typedef struct stack
{
struct node *top;
}stack;
/*Function to initilization of the Stack*/
void init(stack *);
/*Function to PUSH an element into the Stack*/
void push(int,stack *);
/*Function to POP an element into the Stack*/
int pop(stack *);
/*Function to Display the elements of the Stack*/
void display(stack *);
/*Main Function*/
void main()
{
int choice,poped,pushval;
stack *s;
s=(stack *) malloc(sizeof(stack));
init(s);
do
{
printf("\n\nENTER YOUR CHOICE \n1) To PUSH \n2) To POP\n3) To EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER A NUMBER TO PUSH: ");
scanf("%d",&pushval);
push(pushval,s);
printf("\nAFTER PUSH THE STACK IS:\n");
display(s);
break;
case 2:
poped=pop(s);
if (poped != -1)
{
printf("\n%d IS POPED\n",poped);
printf("\nAFTER POP THE STACK IS:\n");
}
display(s);
break;
case 3:
exit(1);
break;
default:
printf("\nEnter a Valid Choice.\n");
}
}
while(1);
}
void init(stack *s)
{
s->top=NULL;
}
void push(int val,stack *s)
{
node *p;
if((p=(node *) malloc(sizeof(node)))==NULL)
{
printf("\nMEMORY ALLOCATION UNSUCCESSFUL");
exit(1);
}
p->info=val;
p->next=s->top;
s->top=p;
}
int pop(stack *s)
{
int popval;
node *p;
if(s->top==NULL)
{
printf("\nSTACK IS EMPTY");
popval = -1;
}
else
{
p=s->top;
s->top=p->next;
popval=p->info;
free(p);
}
return popval;
}
void display(stack *s)
{
node *temp;
temp=s->top;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}
/*End of the program*/
No comments:
Post a Comment