DSA SERIES Chaper 5 — STACK USING LINKED LIST (IN C)

 


[Follow here] for daily coding insights.

DSA SERIES — STACK USING LINKED LIST (IN C)


1. INTRODUCTION

A stack using a linked list works by treating the head of the linked list as the top of the stack. Every push inserts a new node at the beginning, and every pop removes from the beginning.

Why Linked List Stack?

  • No fixed size

  • Efficient insertion and deletion (O(1))

  • Memory used only when needed


2. NODE STRUCTURE (IMPORTANT)

struct Node {
    int data;          // value stored in the stack
    struct Node* next; // pointer to next node
};

Explanation of Members:

  • data → Holds the stack element

  • next

    • Points to the next node

    • For top node, next points to the node below it

    • For last node, next = NULL


3. STACK TOP POINTER

struct Node* top = NULL;

Meaning:

  • When top == NULL, stack is empty

  • Top always points to the most recently added node


4. PUSH OPERATION (INSERT AT BEGINNING)

void push(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (!newNode) {
        printf("Memory allocation failed! Stack Overflow\n");
        return;
    }

    newNode->data = value;
    newNode->next = top;  // new node points to previous top
    top = newNode;        // update top to new node

    printf("%d pushed to stack\n", value);
}

Explanation:

  • A new node is created using malloc

  • newNode->next = top links node to existing stack

  • top = newNode makes new node the first element of stack


5. POP OPERATION (DELETE BEGINNING)

int pop() {
    if (top == NULL) {
        printf("Stack Underflow! Cannot pop\n");
        return -1;
    }

    struct Node* temp = top;
    int poppedValue = temp->data;

    top = top->next;   // move top to next node
    free(temp);        // delete old top node

    return poppedValue;
}

Explanation:

  • If no node, underflow

  • Store top value

  • Move top pointer

  • Free removed node memory


6. PEEK / TOP ELEMENT

int peek() {
    if (top == NULL) {
        printf("Stack is Empty\n");
        return -1;
    }
    return top->data;
}

7. DISPLAY STACK

void display() {
    if (top == NULL) {
        printf("Stack is Empty\n");
        return;
    }

    struct Node* temp = top;
    printf("Stack elements (Top to Bottom):\n");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

8. COMPLETE PROGRAM — STACK USING LINKED LIST (C VERSION)

#include <stdio.h>
#include <stdlib.h>

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

struct Node* top = NULL;

void push(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (!newNode) {
        printf("Memory allocation failed! Stack Overflow\n");
        return;
    }

    newNode->data = value;
    newNode->next = top;
    top = newNode;

    printf("%d pushed to stack\n", value);
}

int pop() {
    if (top == NULL) {
        printf("Stack Underflow! Cannot pop\n");
        return -1;
    }

    struct Node* temp = top;
    int poppedValue = temp->data;

    top = top->next;
    free(temp);

    return poppedValue;
}

int peek() {
    if (top == NULL) {
        printf("Stack is Empty\n");
        return -1;
    }
    return top->data;
}

void display() {
    if (top == NULL) {
        printf("Stack is Empty\n");
        return;
    }

    struct Node* temp = top;
    printf("Stack elements (Top to Bottom):\n");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main() {
    push(10);
    push(20);
    push(30);

    display();

    printf("Top element: %d\n", peek());
    printf("Popped element: %d\n", pop());

    display();

    return 0;
}

ALSO CHECK 

DS(Data Structure) Python&C Edition

DSA Series Chapter 1 Time & Space Complexity ( C Edition)

DSA Series Chapter 2 Arrays  (C Version)


DSA Series Chapter 2 Arrays  (Python Version)

DSA Series Chapter 3 Strings (C Version)


DSA Series Chapter 3 Strings (Python Version)


DSA Series Chapter 4 Linked Lists (Python Version)


DSA Series Chapter 4 Linked Lists (C Version)


DSA Series Chapter 5 Stacks (Python Version)


DSA Series Chapter 5  Stacks  (C Version)









Tail Recursion Optimization: How Compilers Convert Recursion Into Iteration


Advanced Recursion: Memoization vs. Tabulation — The Deep Optimization Blueprint for Professionals


Advanced Sorting Algorithms: Merge Sort Internals — Merge Tree, Cache Behavior & Real Cost Analysis


Enjoyed this post? [Follow here] for daily coding insights.

Comments