problem 27(b)stack using link list
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void push(int val)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
if(top==NULL)
{
temp->next=NULL;
top=temp;
}
else{
temp->next=top;
top=temp;
}
}
void print()
{
struct node *temp;
temp=top;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void pop()
{
if(top==NULL)
{
printf("Underflow\n");
}
else{
top=top->next;
}
}
int main()
{
push(10);
push(20);
push(30);
push(40);
print();
pop();
print();
}
0 Comments