May 2023 Examination
Total Marks
30
Duration
1 hour
Semester
semester3
Year/Branch
SE IT
Attempt any THREE questions from the following.
Define a linked list. What are the advantages of linked lists over arrays?
A linked list is a linear data structure where elements are stored in nodes. Each node contains data and a reference (pointer) to the next node in the sequence.
Advantages over arrays:
Write a C program to reverse a singly linked list.
Program to reverse a singly linked list:
struct Node* reverse(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// Time Complexity: O(n)
// Space Complexity: O(1)Attempt any one
Choose one of the following questions
Explain binary search tree with its properties. Write an algorithm to search for an element in BST.
A Binary Search Tree is a binary tree with the following properties:
struct Node* search(struct Node* root, int key) {
// Base cases: root is null or key is present at root
if (root == NULL || root->data == key)
return root;
// Key is greater than root's key
if (root->data < key)
return search(root->right, key);
// Key is smaller than root's key
return search(root->left, key);
}
// Time Complexity: O(h) where h is height
// Best case: O(log n) for balanced BST
// Worst case: O(n) for skewed BSTWhat is a stack? Explain its applications with examples.
Stack: A stack is a linear data structure that follows the Last In First Out (LIFO) principle. Elements can only be inserted and removed from one end called the top.
Applications:
Implement stack using array in C with push and pop operations.
#define MAX 100
struct Stack {
int arr[MAX];
int top;
};
void initialize(struct Stack* s) {
s->top = -1;
}
int isFull(struct Stack* s) {
return s->top == MAX - 1;
}
int isEmpty(struct Stack* s) {
return s->top == -1;
}
void push(struct Stack* s, int value) {
if (isFull(s)) {
printf("Stack Overflow\n");
return;
}
s->arr[++s->top] = value;
printf("%d pushed to stack\n", value);
}
int pop(struct Stack* s) {
if (isEmpty(s)) {
printf("Stack Underflow\n");
return -1;
}
return s->arr[s->top--];
}
int peek(struct Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
}
return s->arr[s->top];
}Write a program to implement Bubble Sort. Explain its time complexity for best and worst cases with suitable examples.
void bubbleSort(int arr[], int n) {
int i, j, temp;
int swapped;
for (i = 0; i < n - 1; i++) {
swapped = 0;
// Last i elements are already sorted
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}
// If no swap happened, array is sorted
if (swapped == 0)
break;
}
}Best Case: O(n)
Example: Array [1, 2, 3, 4, 5] - Already sorted
Only one pass is needed, no swaps occur, loop breaks early.
Worst Case: O(n²)
Example: Array [5, 4, 3, 2, 1] - Reverse sorted
Every comparison results in a swap. For n elements, total comparisons = n(n-1)/2 ≈ O(n²)
Average Case: O(n²)
Space Complexity: O(1) - In-place sorting algorithm
SPPU | Last updated: 11/30/2025