How to Create a Linked list in implementation in the stack using c program DSA
Program
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
/****************************************/
int isempty(struct node** top){
if(top==NULL){
return 1;
}
else{
return 0;
}
}
/****************************************/
int isfull(struct node* top){
struct node* n= (struct node*)malloc(sizeof(struct node));
if(n==NULL){
return 1;
}
else{
return 0;
}
}
/****************************************/
struct node* push(struct node* top, int x){
if(isfull(top)){
printf("Stack is Overflow\n");
}
else{
struct node* n=(struct node*)malloc(sizeof(struct node));
n->data =x;
n->next =top;
top=n;
return top;
}
}
/****************************************/
int pop(struct node** top){
if(isempty(top)){
printf("Stack is Underflow\n");
}
else{
struct node* n=*top;
*top=(*top)->next;
int x=n->data;
free(n);
return x;
}
}
/****************************************/
int linkedlisttraversal(struct node* ptr){
while(ptr!=NULL)
{
printf("Element :=>%d\n",ptr->data);
ptr=ptr->next;
}
}
/****************************************/
int main(){
struct node* top=NULL;
top=push(top, 3);
top=push(top, 6);
top=push(top, 9);
top=push(top, 4);
top=push(top, 10);
linkedlisttraversal(top);
int element =pop(&top);
printf("Poped the Element is %d\n",element);
element =pop(&top);
printf("Poped the Element is %d\n",element);
element =pop(&top);
printf("Poped the Element is %d\n",element);
linkedlisttraversal(top);
return 0;
}
output -
0 Comments