#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct node* next;
}node;
node* top;
void create()
{
top=NULL;
}
int isEmpty()
{
if(top==NULL)
return 1;
return 0;
}
void push(int element)
{
node* curr;
if(isEmpty())
{
top = (node*)malloc(sizeof(node));
top->data = element;
top->next = NULL;
}
else
{
curr = (node*)malloc(sizeof(node));
curr->data = element;
curr->next = top;
top = curr;
}
}
int pop()
{
node* curr;int element = top->data;
if(isEmpty())
{
printf("Stack is empty.Can't pop");
return -1;
}
else
{
curr=top->next;
free(top);
top=curr;
return element;
}
}
void display()
{
node* curr;
if(isEmpty())
printf("\nStack is empty");
else
{
printf("\n");
for(curr=top;curr!=NULL;curr=curr->next)
printf("%d ",curr->data);
}
}
int main()
{
int element;
create();
push(1);
display();
push(2);
display();
push(3);
display();
element = pop();
if(element!=-1)printf("\nPopped element = %d",element);
display();
return 0;
}
No comments:
Post a Comment