C Stack using Linked List

beginner
18 min

C Stack using Linked List

Welcome to the exciting world of C Programming! Today, we're going to dive deep into understanding and implementing a Stack data structure using Linked List. Let's get started! 🚀

Understanding the Stack and Linked List

What is a Stack? 📝

A Stack is a linear data structure that follows the Last In First Out (LIFO) principle. It's like a pile of books on a table – when you add a new book (push), it goes on top, and when you take out a book (pop), it's the last one you remove.

What is Linked List? 🎯

A Linked List is a sequence of nodes where each node contains a data part and a reference (or link) to the next node in the sequence. It's a flexible and dynamic data structure, ideal for implementing stacks.

Designing a Linked List Stack

Node Structure 📝

Before we start, let's define the structure of our node.

c
typedef struct Node { int data; struct Node* next; } Node;

In this structure, data represents the value stored in the node, and next is a pointer to the next node in the list.

Implementing the Stack Operations 💡

We'll implement the following basic stack operations:

  • Push: Add an element to the stack
  • Pop: Remove an element from the stack
  • Peek: Check the top element of the stack without removing it
  • IsEmpty: Check if the stack is empty or not

Practical Implementation

Now, let's write the code for our Stack using Linked List.

c
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; return newNode; } Node* push(Node* stack, int data) { Node* newNode = createNode(data); newNode->next = stack; return newNode; } int pop(Node** stack) { if (*stack == NULL) { printf("Stack is empty.\n"); return -1; } Node* temp = *stack; *stack = (*stack)->next; int poppedData = temp->data; free(temp); return poppedData; } int peek(Node* stack) { if (stack == NULL) { printf("Stack is empty.\n"); return -1; } return stack->data; } int isEmpty(Node* stack) { return stack == NULL; } int main() { Node* stack = NULL; printf("Push 5 onto the stack:\n"); stack = push(stack, 5); printf("Push 10 onto the stack:\n"); stack = push(stack, 10); printf("Peek at the top of the stack: %d\n", peek(stack)); printf("Pop from the stack: %d\n", pop(&stack)); printf("Peek at the top of the stack: %d\n", peek(stack)); printf("Pop from the stack: %d\n", pop(&stack)); printf("Is the stack empty? %s\n", isEmpty(stack) ? "Yes" : "No"); return 0; }

This code defines the functions for creating a new node, pushing elements onto the stack, popping elements off the stack, checking the top element, and checking if the stack is empty. The main function demonstrates the practical implementation of these functions.

Quick Quiz
Question 1 of 1

What happens when you pop an element from an empty stack?

Conclusion

Congratulations! You've learned how to implement a Stack using Linked List in C programming. This knowledge can be leveraged in various real-world projects, such as parsing expressions or implementing undo/redo functionality. Keep practicing and exploring, and happy coding! 🤓🏆