Blog
Data Structure and Algorithms Past Paper 2015 – BSCS 3rd Term UOS (CMP-3113)
University of Sargodha BS Computer Science 3rd term Data Structure & Algorithms (CMP-3113) paper, 2015 exam (80 marks, 2:30 hours). The paper is retyped below without the handwritten marks, followed by solved answers for all 16 short questions and all five subjective questions: stack push in C++, deleting a node from a linked list, building a BST, bubble sort pass by pass and the adjacency matrix. Programs were compiled and run, and the BST and sort passes were checked by program.
Solved answers
Q1 – Short answers (16 × 2)
A. Data structure. A way of organising and storing data in memory so it can be used efficiently, e.g. arrays, linked lists, stacks, queues, trees, graphs.
B. Linked list. A linear collection of nodes where each node holds data and a pointer to the next node; size can grow and shrink dynamically.
C. Queue. A linear structure in which insertion is at the rear and deletion at the front – First In First Out (FIFO).
D. Stack. A linear structure where insertion (push) and deletion (pop) happen at one end, the top – Last In First Out (LIFO).
E. FIFO. First In, First Out – the first element inserted is the first removed, as in a queue.
F. VOID. void means “no type/no value”: a function returning void returns nothing (void display()); void* is a generic pointer that can point to any type.
G. PUSH operation. Inserting an element on the top of a stack; if the stack is full it causes overflow.
H. Linear search. Checking each element of a list in sequence until the key is found or the list ends; O(n).
I. Data encapsulation. Bundling data and the functions that operate on it into a class and hiding the data (private) so it is accessed only through public methods.
J. Data structures that use pointers. Linked lists (singly, doubly, circular), linked stacks and queues, trees (binary trees, BST, AVL), graphs (adjacency lists) and hash tables with chaining.
K. Multi-dimensional arrays. Arrays with more than one index, e.g. a 2-D array int a[3][4] stored as rows and columns (matrix).
L. Bubble sort. Repeatedly compares adjacent elements and swaps them if out of order; after each pass the largest element reaches the end. O(n²).
M. Recursion. A function calling itself to solve a smaller instance of the same problem, with a base case to stop, e.g. factorial(n) = n × factorial(n − 1).
N. Algorithm. A finite sequence of well-defined steps to solve a problem, with input, output, definiteness, finiteness and effectiveness.
O. Minimum spanning tree. A spanning tree of a connected weighted graph with the minimum total edge weight (found by Prim’s or Kruskal’s algorithm).
P. Abstract data type. A data type defined by its values and operations, independent of implementation, e.g. Stack ADT with push, pop, top, isEmpty.
Q2 – Insert a value in 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;
}push() checks for overflow, increments top and stores the value. Output: 30 20 10, Popped: 30, Top: 20, then overflow when the array is full. (A linked-list stack can grow without limit – see the push function in a linked stack: top = new Node{x, top};.)
Q3 – Delete a node from a linked list
#include <iostream>
using namespace std;
struct Node { int data; Node *next; };
void insertEnd(Node *&head, 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 deleteNode(Node *&head, int key) {
if (!head) return false;
if (head->data == key) { // deleting the first node
Node *t = head; head = head->next; delete t; return true;
}
Node *prev = head, *cur = head->next;
while (cur && cur->data != key) { prev = cur; cur = cur->next; }
if (!cur) return false; // key not found
prev->next = cur->next; // bypass the node
delete cur;
return true;
}
void print(Node *head) {
for (Node *p = head; p; p = p->next) cout << p->data << " ";
cout << endl;
}
int main() {
Node *head = nullptr;
for (int x : {10, 20, 30, 40}) insertEnd(head, x);
print(head); // 10 20 30 40
deleteNode(head, 30); print(head); // 10 20 40
deleteNode(head, 10); print(head); // 20 40
cout << (deleteNode(head, 99) ? "deleted" : "99 not found") << endl;
return 0;
}Output: 10 20 30 40 → delete 30 → 10 20 40 → delete 10 (head) → 20 40 → 99 not found. Cases handled: empty list, deleting the head, deleting a middle/last node, key not present.
Q4 – BST insertion: 10, 5, 90, 65, 50, 35, 100, 30, 85, 15, 70, 75, 55, 40, 20, 80
Each key goes left if smaller, right if larger than the node it is compared with:
10
├─L: 5
└─R: 90
├─L: 65
│ ├─L: 50
│ │ ├─L: 35
│ │ │ ├─L: 30
│ │ │ │ └─L: 15
│ │ │ │ └─R: 20
│ │ │ └─R: 40
│ │ └─R: 55
│ └─R: 85
│ └─L: 70
│ └─R: 75
│ └─R: 80
└─R: 100Pre-order: 10, 5, 90, 65, 50, 35, 30, 15, 20, 40, 55, 85, 70, 75, 80, 100. In-order gives the sorted list 5, 10, 15, …, 100. Height = 8 levels (the tree is unbalanced). (Built and checked with a Python program.)
Q5 – Bubble sort on 10, 5, 90, 65, 50, 35, 100, 30, 85, 15, 70, 75, 55, 40, 20, 80
| Pass | Array after the pass |
|---|---|
| 1 | 5 10 65 50 35 90 30 85 15 70 75 55 40 20 80 100 |
| 2 | 5 10 50 35 65 30 85 15 70 75 55 40 20 80 90 100 |
| 3 | 5 10 35 50 30 65 15 70 75 55 40 20 80 85 90 100 |
| 4 | 5 10 35 30 50 15 65 70 55 40 20 75 80 85 90 100 |
| 5 | 5 10 30 35 15 50 65 55 40 20 70 75 … |
| 6 | 5 10 30 15 35 50 55 40 20 65 70 … |
| 7 | 5 10 15 30 35 50 40 20 55 65 … |
| 8 | 5 10 15 30 35 40 20 50 55 … |
| 9 | 5 10 15 30 35 20 40 50 … |
| 10 | 5 10 15 30 20 35 40 … |
| 11 | 5 10 15 20 30 35 40 50 55 65 70 75 80 85 90 100 |
| 12 | no swaps – stop |
Sorted after 11 passes (pass 12 confirms no swaps). (Passes generated by a program.)
Q6 – Graph and adjacency matrix
A graph G = (V, E) is a set of vertices and a set of edges connecting pairs of vertices; graphs may be directed or undirected, weighted or unweighted. Uses: road maps, networks, social networks.
An adjacency matrix is a |V| × |V| matrix A where A[i][j] = 1 (or the weight) if there is an edge from i to j, otherwise 0. For undirected graphs it is symmetric. Edge lookup is O(1) but space is O(V²), so it suits dense graphs.
Example: undirected graph with edges A–B, A–C, B–C, C–D:
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 1 | 1 | 0 |
| B | 1 | 0 | 1 | 0 |
| C | 1 | 1 | 0 | 1 |
| D | 0 | 0 | 1 | 0 |
