1
DSA (IA-2)
Module-2
1. QUEUE
Definition
• A Queue is a linear data structure where insertion is done at REAR and deletion is done at
FRONT.
• It follows FIFO (First In – First Out).
Array Representation (2 marks)
• Queue uses an array Q[MAX] and two indices:
front → index before first element
rear → index of last element
• Empty condition: front == rear
• Full condition: rear == MAX-1
Operations
1. Insert / Enqueue(item)
Steps:
1. Check IsFull.
2. Increase rear = rear + 1.
3. Store: Q[rear] = item.
2. Delete / Dequeue()
Steps:
1. Check IsEmpty.
2. Increase front = front + 1.
Only for reference
2
3. Return Q[front].
3. IsEmpty()
Returns true if queue has no elements.
4. IsFull()
Returns true if queue array is completely filled.
5. Display()
Print elements from front+1 to rear.
Diagram to write in exam:
Front → [ 10 ] [ 20 ] [ 30 ] ← Rear
2. Circular Queue
Reason for circular queue
To avoid wastage of memory when front moves forward in normal queue.
Circular Queue Program
Write this in exam:
#include <stdio.h>
#define MAX 5
int cq[MAX];
int front = 0, rear = 0;
int isFull() { return (rear + 1) % MAX == front; }
int isEmpty() { return front == rear; }
void insert(int item) {
if (isFull()) printf("Queue Full\n");
else {
Only for reference
3
rear = (rear + 1) % MAX;
cq[rear] = item;
}
}
int delete() {
if (isEmpty()) printf("Queue Empty\n");
else {
front = (front + 1) % MAX;
return cq[front];
}
}
void display() {
int i = (front + 1) % MAX;
if (isEmpty()) printf("Empty\n");
else {
while (i != (rear + 1) % MAX) {
printf("%d ", cq[i]);
i = (i + 1) % MAX;
}
}
}
int main() {
insert(10);
insert(20);
Only for reference
4
insert(30);
display();
}
3. Advantages of Circular Queue (4 Marks)
Write FOUR points:
1. No memory wastage → free spaces at beginning can be reused.
2. Efficient → insertion and deletion take constant time O(1).
3. Better than linear queue because front and rear move in circular way.
4. No shifting required, unlike simple queue.
4. Multiple Queues and Stacks
Multiple Stacks
• A single array is divided into parts, each part behaves like a separate stack.
• Each stack has its own top pointer.
• Example:
Stack1: 0–4 Stack2: 5–9 Stack3: 10–14
Multiple Queues
• Array divided into sections OR linked list used.
• Each queue has:
front[i], rear[i].
• Used in scheduling, printers, OS processes etc.
Simple diagram to draw:
Only for reference
5
Q1: F → [ ] [ ] [ ] ← R
Q2: F → [ ] [ ] [ ] ← R
5. Linked List + Types
Definition (1 mark)
A linked list is a collection of nodes where each node contains data and a pointer to next node.
Nodes are not stored continuously.
Types
1. Singly Linked List
• Each node has: data | next
• Last node points to NULL.
10 → 20 → 30 → NULL
2. Circular Linked List
• Last node points back to first node.
10 → 20 → 30 ↺
3. Doubly Linked List
• Node has three fields: prev | data | next
• Can move forward and backward.
NULL ← 10 ↔ 20 ↔ 30 → NULL
6. Linked Stack and Linked Queue Operations
Linked Stack
Push Operation
Only for reference
6
1. Create new node.
2. Set new->next = top.
3. Update top = new.
Pop Operation
1. Check empty.
2. Store top->data.
3. Move top to next node.
4. Delete old node.
Diagram:
TOP → 30 → 20 → 10 → NULL
Linked Queue
Enqueue
1. Create new node.
2. Attach to rear->next.
3. Update rear.
Dequeue
1. Remove node from front.
2. Move front pointer.
3. If front becomes NULL → rear = NULL.
Diagram:
Front → 10 → 20 → 30 → Rear
7. Polynomial Addition Using Linked List
Given:
Only for reference
7
P1 = 5x³ + 4x² + 7x + 3
P2 = 6x² + 5
Step-by-step (5 marks)
• Compare exponent 3 and 2 → take 5x³
• Compare 2 and 2 → add → 10x²
• Take 7x¹
• Add constants: 3 + 5 = 8
Final Polynomial
5x³ + 10x² + 7x + 8
Linked List Representation (2 marks)
(5,3) → (10,2) → (7,1) → (8,0) → NULL
C Function (3 marks)
Write short code version:
typedef struct node {
int coef, exp;
struct node *next;
}NODE;
NODE* addPoly(NODE *p1, NODE *p2) {
NODE *res = NULL, *temp;
while(p1 && p2) {
temp = malloc(sizeof(NODE));
if(p1->exp == p2->exp) {
temp->coef = p1->coef + p2->coef;
temp->exp = p1->exp;
Only for reference
8
p1 = p1->next; p2 = p2->next;
} else if(p1->exp > p2->exp) {
temp->coef = p1->coef; temp->exp = p1->exp;
p1 = p1->next;
} else {
temp->coef = p2->coef; temp->exp = p2->exp;
p2 = p2->next;
}
temp->next = res; res = temp;
}
return res;
}
8. Singly Linked List – Insert End, Delete Front (6 Marks)
Insert at End
Steps:
1. Create new node.
2. If list empty → new node becomes head.
3. Else traverse to last node and attach new node.
Delete at Front
Steps:
1. Check empty.
2. Move head to head->next.
3. Delete old first node.
Only for reference
9
Display (1 mark)
Print from head to last.
9. Queue Using Dynamic Array
Short points:
1. Queue created using malloc() with initial capacity (e.g., 4).
2. front and rear move circularly using modulo operator.
3. When queue becomes full, resize array (usually double size).
4. Copy old elements in correct queue order to new array.
5. Update front = 0 and rear = size-1 after resizing.
6. Advantage: no limit, efficient, flexible.
10. Stack Using Singly Linked List – C Program
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}NODE;
NODE *top = NULL;
void push(int x) {
NODE *temp = malloc(sizeof(NODE));
temp->data = x;
Only for reference
10
temp->next = top;
top = temp;
}
int pop() {
if (top == NULL) return -1;
int x = top->data;
NODE *temp = top;
top = top->next;
free(temp);
return x;
}
void display() {
NODE *p = top;
while(p) {
printf("%d ", p->data);
p = p->next;
}
}
Only for reference
11
Module-3
1. C Functions – Invert (Reverse) SLL & Concatenate SLL
a) Invert / Reverse Singly Linked List
Concept (from pg.1) – Reverse means changing links so the last becomes first. Very important
exam program.
Simple Explanation
We use 3 pointers:
• lead → current node
• mid → previous node
• trail → node before previous
We move through list and reverse links one by one.
C Function
void reverse() {
struct node *trail, *mid = NULL, *lead;
lead = first;
while (lead != NULL) {
trail = mid;
mid = lead;
lead = lead->link;
mid->link = trail; }
first = mid;
}
b) Concatenation of Two Singly Linked Lists
Only for reference
12
(Refer pg.2,3)
Easy Explanation
• If list1 empty → result = list2
• If list2 empty → result = list1
• Otherwise go to last node of list1 and attach list2.
C Function
NODE concatenate() {
NODE temp = first1;
if (first1 == NULL) return first2;
if (first2 == NULL) return first1;
while (temp->link != NULL)
temp = temp->link;
temp->link = first2;
first2 = NULL;
return first1;
}
2. Sparse Matrix Linked Representation (Simple Notes)
Sparse matrix = more zeros, few non-zero elements.
Representation uses Header nodes + Element nodes.
(See pg.10-13 diagrams)
Given Matrix
10000
02000
Only for reference
13
00005
00400
00300
Linked List Representation Format
Each node stores:
(row, column, value)
Non-zero entries list
Row Col Value
0 0 1
1 1 2
2 4 5
3 2 4
4 2 3
Linked List Diagram to Write in Exam
HEAD → (0,0,1) → (1,1,2) → (2,4,5) → (3,2,4) → (4,2,3) → NULL
This matches the linked-list format in pg.10–13
3. DLL Operations – Insert End & Delete Front
a) Insert at End of DLL
(Refer pg.17–18)
void insert_end() {
NODE temp = malloc(sizeof(struct node));
Only for reference
14
scanf("%d", &temp->data);
temp->next = temp->prev = NULL;
if (FIRST == NULL)
FIRST = END = temp;
else {
END->next = temp;
temp->prev = END;
END = temp;
}
}
b) Delete Front of DLL
(Refer pg.19–20)
void delete_front() {
if (FIRST == NULL)
printf("List empty");
else if (FIRST == END) {
free(FIRST);
FIRST = END = NULL;
}
else {
NODE temp = FIRST;
FIRST = FIRST->next;
FIRST->prev = NULL;
free(temp);
Only for reference
15
}
}
4. SLL – Insert at Beginning & Delete at End
a) Insert at Beginning
Very simple: new node → link to first → update first.
void insert_begin() {
NODE temp = malloc(sizeof(struct node));
scanf("%d", &temp->data);
temp->link = first;
first = temp;
}
b) Delete at End
void delete_end() {
if (first == NULL) return;
if (first->link == NULL) {
free(first);
first = NULL;
return;
}
NODE p = first, q = NULL;
while (p->link != NULL) {
q = p;
p = p->link;
Only for reference
16
}
q->link = NULL;
free(p);
}
5. Circular Linked List – Lab Program 6 Operations
Common operations:
• Insert front
• Insert end
• Delete front
• Delete end
Example – Insert at Front
void insert_front() {
NODE newNode = malloc(sizeof(struct node));
scanf("%d", &newNode->data);
if (head == NULL) {
head = newNode;
newNode->next = head;
}
else {
NODE temp = head;
while (temp->next != head)
temp = temp->next;
newNode->next = head;
Only for reference
17
head = newNode;
temp->next = head;
}
}
6. Delete a Node by Value in SLL
void delete_value(int key) {
NODE p = first, q = NULL;
if (p != NULL && p->data == key) {
first = p->link;
free(p);
return;
}
while (p != NULL && p->data != key) {
q = p;
p = p->link;
}
if (p == NULL) return;
q->link = p->link;
free(p);
}
Only for reference
18
7. Tree Definitions + Terminology (Very Easy Notes)
Tree
Finite non-linear structure with a root and subtrees.
a) Complete Binary Tree
All levels are full except last; last filled left to right. (pg.41)
b) Degree of Tree
Maximum degree among all nodes.
c) Full Binary Tree
Every node has 0 or 2 children; if depth = k → nodes = 2ᵏ – 1.
d) Height of Tree
Longest path from root to leaf. Leaves have height 0.
e) Level of Node
Root = level 1; each level increases by 1.
f) Internal Node
Non-leaf nodes.
g) Terminal Node
Leaf node → no children.
h) Ancestor
Any node on path from root to given node.
8. Binary Tree Definition + Representation
Binary Tree
Each node has max 2 children → left and right subtree.
Representations
Only for reference
19
1) Array Representation
• Root → index 1
• Left child → 2*i
• Right child → 2*i+1
Useful for complete trees.
2) Linked Representation
Each node:
LCHILD | DATA | RCHILD
9. Traversals + C Functions
Preorder (Root, Left, Right)
void preorder(node *t) {
if (t) {
printf("%d", t->data);
preorder(t->left);
preorder(t->right);
}
}
Inorder (Left, Root, Right)
void inorder(node *t) {
if (t) {
inorder(t->left);
printf("%d", t->data);
inorder(t->right);
Only for reference
20
}
}
Postorder (Left, Right, Root)
void postorder(node *t) {
if (t) {
postorder(t->left);
postorder(t->right);
printf("%d", t->data);
}
}
10. Construct Binary Tree from In order + Preorder
Given:
Inorder:
1 2 3 5 9 109 21 25
Preorder:
5 2 1 3 19 9 21 25
Steps:
1. First element of preorder = root = 5
2. In inorder, elements left of 5 → left subtree; right → right subtree
3. Recursively build tree.
(This is exactly the same process given in pg.49 diagrams.
You can redraw tree in exam.)
Only for reference
21
11. Types of Binary Trees (Easy Notes)
1. Skewed Tree
o Only left or only right children.
2. Complete Tree
o All levels full except last.
3. Full Tree
o Every node has 0 or 2 children.
12. Threaded Binary Tree
Problem in normal BT
There are many NULL pointers → wasteful.
Threaded BT Solution
Replace NULLs with:
• LeftThread → points to inorder predecessor
• RightThread → points to inorder successor
Node Structure
typedef struct threadTree {
int leftThread;
struct threadTree *leftChild;
char data;
struct threadTree *rightChild;
int rightThread;
} threadTree;
Construction
Only for reference
22
For elements 1–9 → arrange in inorder and thread missing links.
Only for reference