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]
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
| Feature | Static List Structure | Dynamic List Structure |
|---|---|---|
| Memory Allocation | Memory is allocated at compile time (fixed size) | Memory is allocated at run time (as needed) |
| Size | Size is fixed and cannot change during execution | Size can grow or shrink during execution |
| Implementation | Implemented using arrays | Implemented using linked lists |
| Memory Utilization | May waste memory (if underused) or overflow (if overused) | Efficient memory utilization; no wastage |
| Insertion/Deletion | Costly; requires shifting of elements | Easy; only pointer adjustments needed |
| Access | Direct/random access using index | Sequential access only |
| Example | int 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:
- Create a new node.
- Assign data to the new node.
- Make new node's
nextpoint to currenttop. - Update
topto 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:
- Check if stack is empty (top == NULL).
- Store the top node in a temporary pointer.
- Move
topto the next node. - 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:
- Create a new node.
- Assign data to the new node; set its
nextto NULL. - If queue is empty, both
frontandrearpoint to new node. - Otherwise, link the current
rear's next to new node and updaterear.
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:
- Check if queue is empty (front == NULL).
- Store the front node in a temporary pointer.
- Move
frontto the next node. - If
frontbecomes NULL, setrearto NULL as well. - 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.