2082 3

BIT201 ยท 2082 ยท 10 marks

Distinguish between static and dynamic list structure.Explain about linked list implementation of stack and queue.[2+8]

Distinguish between static and dynamic list structure.Explain about linked list implementation of stack and queue.[2+8]

Data Structures and AlgorithmsChapter 1: Fundamentals of Data Structures and AlgorithmsStatic versus dynamic list structures

Quick answer

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. In linked list implementation, each node contains data and a pointer to the next node. The top pointer always points to the most recently inserted node.

Static vs Dynamic List Structure & Linked List Implementation of Stack and Queue


(a) Static vs Dynamic List Structure

FeatureStatic List StructureDynamic List Structure
Memory AllocationMemory is allocated at compile time (fixed size)Memory is allocated at run time (as needed)
SizeSize is fixed and cannot change during executionSize can grow or shrink during execution
ImplementationImplemented using arraysImplemented using linked lists
Memory UtilizationMay waste memory (if underused) or overflow (if overused)Efficient memory utilization; no wastage
Insertion/DeletionCostly; requires shifting of elementsEasy; only pointer adjustments needed
AccessDirect/random access using indexSequential access only
Exampleint arr[100]Singly/doubly linked list

(b) Linked List Implementation of Stack and Queue


A. Linked List Implementation of Stack

Node Structure

struct Node {
    int data;
    struct Node* next;
};
struct Node* top = NULL;
[data | next] --> [data | next] --> [data | next] --> NULL
   ^
  TOP

i. PUSH Operation (Insert at top)

Steps:

  1. Create a new node.
  2. Assign data to the new node.
  3. Make new node's next point to current top.
  4. Update top to the new node.
void push(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Stack Overflow\n");
        return;
    }
    newNode->data = value;
    newNode->next = top;   // new node points to old top
    top = newNode;         // top updated to new node
}

Diagram (Push 10, 20, 30):

After Push(10):   TOP -> [10 | NULL]

After Push(20):   TOP -> [20 | *] -> [10 | NULL]

After Push(30):   TOP -> [30 | *] -> [20 | *] -> [10 | NULL]

ii. POP Operation (Delete from top)

Steps:

  1. Check if stack is empty (top == NULL).
  2. Store the top node in a temporary pointer.
  3. Move top to the next node.
  4. Free the temporary node.
void pop() {
    if (top == NULL) {
        printf("Stack Underflow\n");
        return;
    }
    struct Node* temp = top;
    printf("Popped: %d\n", temp->data);
    top = top->next;   // move top to next node
    free(temp);        // free old top
}

iii. PEEK / Display

void peek() {
    if (top == NULL)
        printf("Stack is Empty\n");
    else
        printf("Top element: %d\n", top->data);
}

B. Linked List Implementation of Queue

A queue is a linear data structure that follows the FIFO (First In First Out) principle. In linked list implementation, two pointers are maintained:

  • FRONT -- points to the first node (deletion end)
  • REAR -- points to the last node (insertion end)

Node Structure

struct Node {
    int data;
    struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
FRONT                          REAR
  |                              |
[data|next] -> [data|next] -> [data|NULL]

i. ENQUEUE Operation (Insert at rear)

Steps:

  1. Create a new node.
  2. Assign data to the new node; set its next to NULL.
  3. If queue is empty, both front and rear point to new node.
  4. Otherwise, link the current rear's next to new node and update rear.
void enqueue(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Queue Overflow\n");
        return;
    }
    newNode->data = value;
    newNode->next = NULL;
    if (rear == NULL) {          // queue is empty
        front = rear = newNode;
        return;
    }
    rear->next = newNode;        // link at rear
    rear = newNode;              // update rear
}

Diagram (Enqueue 10, 20, 30):

After Enqueue(10):
FRONT -> [10|NULL] <- REAR

After Enqueue(20):
FRONT -> [10|*] -> [20|NULL] <- REAR

After Enqueue(30):
FRONT -> [10|*] -> [20|*] -> [30|NULL] <- REAR

ii. DEQUEUE Operation (Delete from front)

Steps:

  1. Check if queue is empty (front == NULL).
  2. Store the front node in a temporary pointer.
  3. Move front to the next node.
  4. If front becomes NULL, set rear to NULL as well.
  5. Free the temporary node.
void dequeue() {
    if (front == NULL) {
        printf("Queue Underflow\n");
        return;
    }
    struct Node* temp = front;
    printf("Dequeued: %d\n", temp->data);
    front = front->next;         // move front forward
    if (front == NULL) {         // queue became empty
        rear = NULL;
    }
    free(temp);                  // release the old front node
}

Diagram (Dequeue from FRONT -> [10|] -> [20|] -> [30|NULL] <- REAR):

After Dequeue() (removes 10):
FRONT -> [20|*] -> [30|NULL] <- REAR

After Dequeue() (removes 20):
FRONT -> [30|NULL] <- REAR

After Dequeue() (removes 30):
FRONT = NULL, REAR = NULL   (queue is empty again)

iii. DISPLAY Operation

void display() {
    if (front == NULL) {
        printf("Queue is empty\n");
        return;
    }
    struct Node* temp = front;
    printf("Queue (front to rear): ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

Conclusion

A linked list removes the fixed-size limitation of an array, so both structures grow and shrink with the data. In the stack only one pointer (top) is needed and both PUSH and POP work at the head of the list, while the queue needs two pointers, since ENQUEUE attaches at rear and DEQUEUE detaches at front. In both cases every operation touches a constant number of nodes, so PUSH, POP, ENQUEUE and DEQUEUE all run in O(1) time, and overflow occurs only when malloc itself fails.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.