IMPLEMENTATION OF STACK USING LINKED LIST

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data ;
struct node* next;
};
struct node* head = NULL;

void push(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node*));
if(head == NULL)
{
temp->data = data;
temp->next = NULL;
head = temp;
}
else
{
temp->data = data;
temp->next = head;
head = temp;
}
}

void pop()
{
struct node* temp = head;
head = temp->next;
free(temp);
}
void print()
{
struct node* temp = head ;
while(temp!=NULL)
{
printf("%d",temp->data);
temp = temp->next;
}
printf("\n");
}
void main()
{
push(1);
push(2);
push(3);
print();
pop();
print();
pop();
print();
}

Comments