Unit 1 of 1 · B.Sc IT Sem 3

Unit 1: Data structure implementation

Software Lab-V (Data Structures) notes · PTU syllabus (BSIT306/BSBC306)

4 min read7 topics11 exam questions
On this page
  1. Unit summary
  2. Recursion programs
  3. Array traversal, insertion, deletion and merging
  4. Linear and binary search
  5. Insertion, bubble and selection sort programs
  6. Stack PUSH and POP and infix-to-postfix conversion
  7. Queue and circular queue using arrays
  8. Insertion and deletion in singly and doubly linked lists
  9. Key terms
  10. Quick revision
  11. 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
ProcessInfix to postfix with a stack
  1. 1

    Scan the expression left to right

  2. 2

    Operand

    Add straight to output

  3. 3

    Left bracket

    Push onto the stack

  4. 4

    Operator

    Pop higher or equal precedence, then push

  5. 5

    Right bracket

    Pop until the left bracket

  6. 6

    End

    Pop everything left

1

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);
}
2

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++];
3

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;
}
4

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); }
}
5

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.

6

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;
}
ComparisonCommon lab errors
Symptom
Fix

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

7

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 prev pointer; when inserting, update both neighbours' links.
  • Circular list creation: the last node's next points to start; traversal stops when the pointer comes back to start.

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

  1. Q1.What is the base case in the factorial program?
  2. Q2.Why must the array be sorted for binary search?
  3. Q3.How many moves does Tower of Hanoi need for 4 disks?
  4. Q4.How do you detect a full circular queue?
  5. Q5.Convert (A + B) × C to postfix.
  6. Q6.How is a node deleted from a doubly linked list?

Long-answer questions

  1. Q1.Write recursive programs for factorial, Fibonacci and Tower of Hanoi.
  2. Q2.Write programs for linear and binary search.
  3. Q3.Write a program to implement a circular queue using an array.
  4. Q4.Write a program to convert an infix expression to postfix.
  5. 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.

WhatsApp us