7. Data Structures in C: Linked Lists, Stacks & Queues

Comprehensive Guide: Dynamic linked list nodes, LIFO stack operations (push/pop) & FIFO queue operations (enqueue/dequeue)

Module 1: Singly Linked List Data Structure

Linear collection of dynamic nodes connected via memory pointers.

🇬🇧 English Definition

A Linked List is a linear data structure where elements (nodes) are stored non-contiguously, each node containing a data field and a pointer link to the next node.

🇮🇳 Hinglish Definition

Linked List me har node do chize rakhta hai: Data aur Aagle Node ka Memory Address (Pointer). Ye dynamically grow karti hai.

Core Concepts & Rules

  • Node Structure: struct Node { int data; struct Node *next; };
  • Head Pointer points to the first node in list.
  • Last node next pointer is NULL.

🔍 Line-by-Line Code Breakdown

  1. Line 1-4: Define struct Node with int data and pointer next.
  2. Line 7-9: Allocate first node using malloc, set data = 10, next = NULL.
  3. Line 10-12: Allocate second node, link first->next = second.
💻 Practical C Code Example - Singly Linked List Data Structure
struct Node {
    int data;
    struct Node *next;
};

int main() {
    struct Node *head = (struct Node*)malloc(sizeof(struct Node));
    struct Node *second = (struct Node*)malloc(sizeof(struct Node));
    
    head->data = 10;
    head->next = second;
    second->data = 20;
    second->next = NULL;
    
    printf("Node 1: %d -> Node 2: %d\n", head->data, head->next->data);
    free(head);
    free(second);
    return 0;
}
📤 Compiled Program Output:
Node 1: 10 -> Node 2: 20

Module 2: Stack Data Structure (LIFO Principle)

Last-In, First-Out dynamic linear structure for push and pop operations.

🇬🇧 English Definition

A Stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle, performing insertions (push) and deletions (pop) at the top index.

🇮🇳 Hinglish Definition

Stack plates ke dher ki tarah LIFO principle par kaam karta hai. Jo element sabse aakhir me daala jayega wo sabse pehle bahar nikalega.

Core Concepts & Rules

  • Push: Inserts item onto top of stack.
  • Pop: Removes top item from stack.
  • Top Pointer tracks top index position.

🔍 Line-by-Line Code Breakdown

  1. Line 2-4: Stack array of size 5 with top = -1.
  2. Line 5-6: Push operations incrementing top index.
💻 Practical C Code Example - Stack Data Structure (LIFO Principle)
int stack[5];
int top = -1;

void push(int val) {
    if (top < 4) {
        stack[++top] = val;
    }
}

int main() {
    push(50);
    push(100);
    printf("Top Stack Element: %d\n", stack[top]);
    return 0;
}
📤 Compiled Program Output:
Top Stack Element: 100