#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
struct stack
{
 int data;
 int count;
 struct stack *next;
};
typedef struct stack stack;
void push(stack **,int);
void pop(stack **);
void display(stack *);
void main()
{
 stack *s=NULL;
 clrscr();
 s->count=0;
 push(&s,5);
 push(&s,10);
 push(&s,15);
 display(s);
 pop(&s);
 printf("\n");
 display(s);
 getch();
}
void push(stack **s,int d)
{
 stack *newnode=(stack *)malloc(sizeof(stack));
 if(newnode==NULL)
 {printf("memory overflow");
 exit(0);}
 (*s)->count++;
 newnode->data=d;
 newnode->next=(*s);
 (*s)=newnode;
}
void pop(stack **s)
{
 stack *p;
 if(*s==NULL)
 {printf("underflow");
 exit(0);}
 p=*s;
 (*s)->count--;
 *s=(*s)->next;
 free(p);
}
void display(stack *s)
{
 while(s!=NULL)
 {printf("%d ",s->data);
 s=s->next;}
}
 
No comments:
Post a Comment