DSA - Data Structure & Algorithms, Past Papers, UOS Main Campus

Data Structure and Algorithm Past Paper 2014 – BS IT 4th Term UOS (CMP-210)

ProgramBS IT
Term4th
Year2014
Course codeCMP-210
AnswersSolved ✓

University of Sargodha BS IT 4th term Data Structure & Algorithm (CMP-210) paper, 2014 exam, session 2012-16 (80 marks, 2:30 hours). The paper is retyped below without the handwritten number, followed by solved answers for all 16 short questions and all five subjective questions: pointer errors in three code fragments, linked list, stack and queue programs in C++, and BST insertion. Programs were compiled and run.

Solved answers

Q1 – Short answers (16 × 2)

i. ADT. Abstract Data Type – a type defined by its data values and operations (what it does), not by implementation (how), e.g. Stack, Queue, List.

ii. Data structures. Ways of organising data in memory for efficient access and modification: arrays, linked lists, stacks, queues, trees, graphs, hash tables.

iii. Structured programming. A programming approach using only sequence, selection (if/switch) and iteration (loops), with functions/modules and no goto, giving clear, top-down, maintainable programs (e.g. C, Pascal).

iv. Operator overloading. Giving an operator a special meaning for user-defined types, e.g. Complex operator+(const Complex&) so that c1 + c2 adds two complex objects.

v. Destructor significance. Called automatically when an object goes out of scope or is deleted; it releases resources (dynamic memory, files) and prevents memory leaks. Name ~ClassName(), no arguments, one per class.

vi. Dangling pointer. A pointer that still points to memory that has been freed or gone out of scope; using it causes undefined behaviour. Fix: set it to nullptr after delete.

vii. int *p=&n; Declares p as a pointer to int and initialises it with the address of n, so p points to n.

viii. Dereferencing operator. * applied to a pointer gives the value stored at the address it holds: if p = &n then *p is n.

ix. Graph. A set of vertices and edges connecting them, G = (V, E); can be directed/undirected, weighted/unweighted.

x. Complete binary tree. All levels are full except possibly the last, which is filled from left to right.

xi. Binary tree vs BST. A binary tree is any tree where each node has at most two children. A BST additionally keeps every key in the left subtree smaller and every key in the right subtree larger than the node, allowing O(log n) search when balanced.

xii. this pointer. An implicit pointer in every non-static member function pointing to the object that called the function; used to access members and return the object itself.

xiii. ++(*p) vs *(++p). ++(*p) increments the value pointed to by p (p unchanged). *(++p) first moves p to the next memory location and then dereferences it.

xiv. Array. A collection of elements of the same type stored in contiguous memory and accessed by index, e.g. int a[10].

xv. new operator. Allocates memory on the heap at run time for an object/array, calls the constructor, and returns a pointer to it, e.g. int *p = new int(5);; memory is released with delete.

xvi. Data type. Specifies the kind of values a variable can hold, the memory size and allowed operations – int, float, char, bool, user-defined classes.

Q2 – What is wrong with each code?

(a) int m=p; is an error: p is an int* (an address) and cannot be assigned to an int without a cast. The first lines are fine (++(*p) makes n = 45). Correct: int m = *p; (m = 45) or int *m = p;.

(b) It compiles, but after delete p; the pointer q still points to the freed memory – it becomes a dangling pointer; using *q or deleting q again gives undefined behaviour (double delete). Fix: set p = q = nullptr; after delete, or delete through only one owner.

(c) int n=new int; is an error: new int returns an int*, which cannot be stored in an int; and the memory would never be freed. Correct: int *n = new int; *n = 44; … delete n;.

Q3 – Linked list implementation

#include <iostream>
using namespace std;

struct Node { int data; Node *next; };

class LinkedList {
    Node *head;
public:
    LinkedList() : head(nullptr) {}
    void insertFront(int x) { head = new Node{x, head}; }
    void insertEnd(int x) {
        Node *n = new Node{x, nullptr};
        if (!head) { head = n; return; }
        Node *p = head; while (p->next) p = p->next;
        p->next = n;
    }
    bool remove(int key) {
        Node *prev = nullptr, *cur = head;
        while (cur && cur->data != key) { prev = cur; cur = cur->next; }
        if (!cur) return false;
        if (prev) prev->next = cur->next; else head = cur->next;
        delete cur; return true;
    }
    bool search(int key) const {
        for (Node *p = head; p; p = p->next) if (p->data == key) return true;
        return false;
    }
    void display() const {
        for (Node *p = head; p; p = p->next) cout << p->data << " -> ";
        cout << "NULL" << endl;
    }
    ~LinkedList() { while (head) { Node *t = head; head = head->next; delete t; } }
};

int main() {
    LinkedList L;
    L.insertEnd(20); L.insertEnd(30); L.insertFront(10);
    L.display();                                   // 10 -> 20 -> 30 -> NULL
    L.remove(20); L.display();                     // 10 -> 30 -> NULL
    cout << "Search 30: " << (L.search(30) ? "found" : "not found") << endl;
    return 0;
}

Output: 10 -> 20 -> 30 -> NULL, 10 -> 30 -> NULL, Search 30: found.

Q4 – Push and pop of a stack

#include <iostream>
using namespace std;

const int MAX = 5;

class Stack {
    int a[MAX];
    int top;
public:
    Stack() : top(-1) {}
    bool isEmpty() const { return top == -1; }
    bool isFull()  const { return top == MAX - 1; }
    void push(int x) {
        if (isFull()) { cout << "Stack overflow\n"; return; }
        a[++top] = x;
    }
    int pop() {
        if (isEmpty()) { cout << "Stack underflow\n"; return -1; }
        return a[top--];
    }
    int peek() const { return isEmpty() ? -1 : a[top]; }
    void display() const {
        for (int i = top; i >= 0; i--) cout << a[i] << " ";
        cout << endl;
    }
};

int main() {
    Stack s;
    s.push(10); s.push(20); s.push(30);
    s.display();                               // 30 20 10
    cout << "Popped: " << s.pop() << endl;     // 30
    cout << "Top: " << s.peek() << endl;       // 20
    s.push(40); s.push(50); s.push(60); s.push(70);   // last push overflows
    s.display();
    return 0;
}

Output: 30 20 10, Popped: 30, Top: 20, then overflow handling. Push and pop are O(1).

Q5 – Queue implementation

#include <iostream>
using namespace std;

const int MAX = 5;

class Queue {                // circular array queue
    int a[MAX];
    int front, rear, count;
public:
    Queue() : front(0), rear(-1), count(0) {}
    bool isEmpty() const { return count == 0; }
    bool isFull()  const { return count == MAX; }
    void enqueue(int x) {
        if (isFull()) { cout << "Queue overflow\n"; return; }
        rear = (rear + 1) % MAX; a[rear] = x; count++;
    }
    int dequeue() {
        if (isEmpty()) { cout << "Queue underflow\n"; return -1; }
        int x = a[front]; front = (front + 1) % MAX; count--; return x;
    }
    void display() const {
        for (int i = 0, j = front; i < count; i++, j = (j + 1) % MAX) cout << a[j] << " ";
        cout << endl;
    }
};

int main() {
    Queue q;
    q.enqueue(1); q.enqueue(2); q.enqueue(3);
    q.display();                                   // 1 2 3
    cout << "Dequeued: " << q.dequeue() << endl;   // 1
    q.enqueue(4); q.enqueue(5); q.enqueue(6);      // wraps around
    q.display();                                   // 2 3 4 5 6
    q.enqueue(7);                                  // overflow
    return 0;
}

A circular array queue: output 1 2 3, Dequeued: 1, 2 3 4 5 6 (indices wrap around), then Queue overflow. (All programs compiled with g++ and run.)

Q6 – BST: 50, 55, 60, 105, 77, 80, 40, 99, 101, 200, 68, 87

50
├─L: 40
└─R: 55
     └─R: 60
          └─R: 105
               ├─L: 77
               │    ├─L: 68
               │    └─R: 80
               │         └─R: 99
               │              ├─L: 87
               │              └─R: 101
               └─R: 200

Steps: 50 root; 55 > 50 → right; 60 → right of 55; 105 → right of 60; 77 < 105 → left of 105; 80 > 77 → right of 77; 40 < 50 → left of 50; 99 → right of 80; 101 → right of 99; 200 → right of 105; 68 → left of 77; 87 → left of 99.

In-order: 40, 50, 55, 60, 68, 77, 80, 87, 99, 101, 105, 200 (sorted). Pre-order: 50, 40, 55, 60, 105, 77, 68, 80, 99, 87, 101, 200. (Checked with a Python program.)

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.