Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the stack by using linked list in C language
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. When implementing a stack using a linked list in C, we can avoid stack overflow and underflow issues by dynamically allocating memory. This approach provides flexibility in memory usage and can grow or shrink based on requirements.
Syntax
struct node {
int data;
struct node *next;
} *top;
void push(int value);
void pop();
int peek();
int isEmpty();
Stack Operations
The primary operations performed on a stack using linked list are −
- Push − Insert an element at the top of the stack
- Pop − Remove the top element from the stack
- Peek/Top − View the top element without removing it
- isEmpty − Check if the stack is empty
Push Operation
In the push operation, a new node is created and inserted at the beginning of the linked list ?
Pop Operation
In the pop operation, the top node is removed and the top pointer is updated to point to the next node ?
Example: Complete Stack Implementation
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
} *top, *temp;
void push(int data);
void pop();
void display();
int isEmpty();
int peek();
int main() {
top = NULL;
int choice, value;
printf("Stack Operations using Linked List<br>");
printf("1. Push<br>2. Pop<br>3. Display<br>4. Peek<br>5. Exit<br>");
while(1) {
printf("\nEnter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter element: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
if(!isEmpty()) {
printf("Top element: %d<br>", peek());
} else {
printf("Stack is empty<br>");
}
break;
case 5:
exit(0);
default:
printf("Invalid choice<br>");
}
}
return 0;
}
void push(int data) {
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL) {
printf("Memory allocation failed<br>");
return;
}
temp->info = data;
temp->ptr = top;
top = temp;
printf("Element %d pushed successfully<br>", data);
}
void pop() {
if(isEmpty()) {
printf("Stack underflow - Stack is empty<br>");
return;
}
temp = top;
printf("Popped element: %d<br>", top->info);
top = top->ptr;
free(temp);
}
void display() {
if(isEmpty()) {
printf("Stack is empty<br>");
return;
}
printf("Stack elements: ");
temp = top;
while(temp != NULL) {
printf("%d ", temp->info);
temp = temp->ptr;
}
printf("<br>");
}
int isEmpty() {
return (top == NULL);
}
int peek() {
if(!isEmpty()) {
return top->info;
}
return -1;
}
Stack Operations using Linked List 1. Push 2. Pop 3. Display 4. Peek 5. Exit Enter choice: 1 Enter element: 10 Element 10 pushed successfully Enter choice: 1 Enter element: 20 Element 20 pushed successfully Enter choice: 3 Stack elements: 20 10 Enter choice: 4 Top element: 20 Enter choice: 2 Popped element: 20 Enter choice: 3 Stack elements: 10 Enter choice: 5
Key Advantages
- Dynamic Size − No need to specify stack size beforehand
- Memory Efficient − Memory is allocated only when needed
- No Overflow − Stack can grow as long as memory is available
- Flexible − Easy to implement and modify
Time Complexity
| Operation | Time Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| Display | O(n) |
Conclusion
Implementing a stack using linked list provides dynamic memory allocation, preventing stack overflow issues. All basic operations (push, pop, peek) execute in O(1) time complexity, making it an efficient data structure for LIFO operations.
