🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the last element added to the stack will be the first element to be removed. Unlike using arrays, when we implement a stack using linked lists, it can grow dynamically and there's no need to specify a maximum limit.
2. Program Overview
In this program, we will:
1. Create a node structure for our linked list.
2. Implement the following stack operations using a linked list:
- Push: To add an element to the top of the stack.
- Pop: To remove the top element from the stack.
- Peek: To view the top element without removing it.
- Display: To display the elements of the stack.
3. Code Program
#include <stdio.h>
#include <stdlib.h>
// Define the structure for our linked list node
typedef struct Node {
int data;
struct Node* next;
} Node;
// Pointer to the top of our stack
Node* top = NULL;
// Function to push an element onto the stack
void push(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Stack Overflow. Unable to allocate memory.\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}
// Function to pop an element from the stack
int pop() {
if (top == NULL) {
printf("Stack Underflow. Stack is empty.\n");
return -1;
}
Node* temp = top;
int poppedData = temp->data;
top = top->next;
free(temp);
return poppedData;
}
// Function to get the top element of the stack
int peek() {
if (top == NULL) {
printf("Stack is empty.\n");
return -1;
}
return top->data;
}
// Function to display the stack
void display() {
Node* temp = top;
if (temp == NULL) {
printf("Stack is empty.\n");
return;
}
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
push(10);
push(20);
push(30);
printf("Top element: %d\n", peek());
display();
printf("Popped element: %d\n", pop());
display();
return 0;
}
Output:
Top element: 30 30 20 10 Popped element: 30 20 10
4. Step By Step Explanation
1. We begin by defining a node structure for our linked list. Each node contains an integer data and a pointer to the next node.
2. Our top pointer points to the top of the stack, which is initially NULL (indicating an empty stack).
3. The push function creates a new node, checks if memory allocation is successful, and then adds the new node to the top of the stack.
4. The pop function removes the top element from the stack, adjusts the top pointer, and frees the memory of the popped node.
5. The peek function simply returns the data of the top node without modifying the stack.
6. The display function iterates through the linked list (stack) and prints all the elements.
7. In the main function, we demonstrate pushing elements onto the stack, viewing the top element, popping an element, and displaying the stack.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment