Unit 1: Data structure implementation
Software Lab-V (Data Structures) notes · PTU syllabus (BSIT306/BSBC306)
On this page
- Unit summary
- Recursion programs
- Array traversal, insertion, deletion and merging
- Linear and binary search
- Insertion, bubble and selection sort programs
- Stack PUSH and POP and infix-to-postfix conversion
- Queue and circular queue using arrays
- Insertion and deletion in singly and doubly linked lists
- Key terms
- Quick revision
- Important questions
Unit summary
This lab implements Data Structures in C: recursion, array traversal, insertion, deletion and merging, linear and binary search, insertion, bubble and selection sort, stack PUSH and POP, array implementation of queues and circular queues, infix-to-postfix conversion, and insertion and deletion in singly and doubly linked lists.
After this unit you can
- Write recursive programs and array operations
- Implement searching and sorting programs
- Implement stacks, queues, circular queues and infix-to-postfix conversion
- Implement singly and doubly linked lists
PTU syllabus topics
- Programs using recursion
- traversing/inserting/deleting/merging array elements
- linear and binary search
- insertion/bubble/selection sort
- PUSH and POP stack operations
- array implementation of queue and circular queue
- infix-to-postfix conversion
- insertion and deletion in single and double linked lists
- 1
Scan the expression left to right
- 2
Operand
Add straight to output
- 3
Left bracket
Push onto the stack
- 4
Operator
Pop higher or equal precedence, then push
- 5
Right bracket
Pop until the left bracket
- 6
End
Pop everything left
Topic 1
Recursion programs
cint fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
int fib(int n) { return n <= 1 ? n : fib(n - 1) + fib(n - 2); }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
void hanoi(int n, char from, char to, char via) {
if (n == 0) return;
hanoi(n - 1, from, via, to);
printf("Move disk %d from %c to %c\n", n, from, to);
hanoi(n - 1, via, to, from);
}Topic 2
Array traversal, insertion, deletion and merging
- Insert at position pos: shift elements right from the end, then place the item and increase n.
- Delete at position pos: shift elements left from pos + 1 and decrease n.
- Matrix multiplication: possible only if columns of A = rows of B.
cfor (i = 0; i < m; i++)
for (j = 0; j < p; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}c/* Merge two sorted arrays a[m] and b[n] into c[] */
int i = 0, j = 0, k = 0;
while (i < m && j < n) c[k++] = (a[i] < b[j]) ? a[i++] : b[j++];
while (i < m) c[k++] = a[i++];
while (j < n) c[k++] = b[j++];Topic 3
Linear and binary search
cint linear(int a[], int n, int key) {
for (int i = 0; i < n; i++) if (a[i] == key) return i;
return -1;
}
int binary(int a[], int n, int key) { /* a[] must be sorted */
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == key) return mid;
else if (a[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}Topic 4
Insertion, bubble and selection sort programs
cvoid bubble(int a[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
void insertion(int a[], int n) {
for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; }
a[j + 1] = key;
}
}
int partition(int a[], int lo, int hi) {
int pivot = a[hi], i = lo - 1;
for (int j = lo; j < hi; j++)
if (a[j] < pivot) { i++; int t = a[i]; a[i] = a[j]; a[j] = t; }
int t = a[i + 1]; a[i + 1] = a[hi]; a[hi] = t;
return i + 1;
}
void quick(int a[], int lo, int hi) {
if (lo < hi) { int p = partition(a, lo, hi); quick(a, lo, p - 1); quick(a, p + 1, hi); }
}Topic 5
Stack PUSH and POP and infix-to-postfix conversion
c#define MAX 50
int stack[MAX], top = -1;
void push(int x) { if (top == MAX - 1) printf("Overflow"); else stack[++top] = x; }
int pop() { if (top == -1) { printf("Underflow"); return -1; } return stack[top--]; }Postfix evaluation: for each symbol, if it is a digit push it; if it is an operator, pop b then a, compute a op b and push the result.
Topic 6
Queue and circular queue using arrays
- Recursive factorial:
long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); } - Circular queue with modulo arithmetic:
cint q[MAX], front = -1, rear = -1;
void enqueue(int x) {
if ((rear + 1) % MAX == front) { printf("Queue full"); return; }
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
q[rear] = x;
}Segmentation fault
Dereferencing a NULL pointer
Check for NULL before using ->next
Memory leak
Deleted node never freed
Call free() on removed nodes
Wrong queue full
Simple queue after deletions
Use a circular queue
Topic 7
Insertion and deletion in singly and doubly linked lists
cstruct node { int data; struct node *next; };
struct node *start = NULL;
void insert_begin(int x) {
struct node *t = malloc(sizeof(struct node));
t->data = x;
t->next = start;
start = t;
}
void delete_begin() {
if (start == NULL) { printf("Underflow"); return; }
struct node *t = start;
start = start->next;
free(t);
}- Doubly linked list nodes add a
prevpointer; when inserting, update both neighbours' links. - Circular list creation: the last node's
nextpoints tostart; traversal stops when the pointer comes back tostart.
Key terms
- Recursion
- Function calling itself with a base case
- Merging
- Combining two sorted lists into one sorted list
- Stack overflow
- PUSH on a full stack
- Circular queue
- Queue reusing freed front positions
- Node
- Linked list element with data and pointer
Quick revision
- Factorial, Fibonacci, GCD, Tower of Hanoi.
- Array insertion shifts right; deletion shifts left; merging two sorted arrays.
- Linear O(n), binary O(log n) on sorted data.
- Bubble, insertion, selection sort.
- Stack and queue with arrays; rear = (rear + 1) % size; infix to postfix; SLL and DLL insert and delete.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is the base case in the factorial program?
- Q2.Why must the array be sorted for binary search?
- Q3.How many moves does Tower of Hanoi need for 4 disks?
- Q4.How do you detect a full circular queue?
- Q5.Convert (A + B) × C to postfix.
- Q6.How is a node deleted from a doubly linked list?
Long-answer questions
- Q1.Write recursive programs for factorial, Fibonacci and Tower of Hanoi.
- Q2.Write programs for linear and binary search.
- Q3.Write a program to implement a circular queue using an array.
- Q4.Write a program to convert an infix expression to postfix.
- Q5.Write a program for insertion and deletion in a doubly linked list.
Stuck on this unit?
Message SBS on WhatsApp for help with Software Lab-V (Data Structures), or to ask about studying B.Sc IT at Synetic.
