dsfile
dsfile
dsfile
Top element: 8
Popped 8 from the stack
Top element: 2
Popped 2 from the stack
Top element: 5
Popped 5 from the stack
Top element: 3
Popped 3 from the stack
STACK
Implementation Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void push(int val)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
void pop()
{
struct Node *temp;
if(head == NULL)
printf("Stack is Empty\n");
else
{ printf("Poped element = %d\n", head->data);
temp = head;
head = head->next;
free(temp);
}
printf("\n");
}
void display()
{ struct Node *temp = head;
while(temp != NULL)
{ printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{ push(10);
push(20);
push(30);
push(40);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
return 0;
}
OUTPUT:
Linked List
40->30->20->10->NULL
Poped element = 40