0% found this document useful (0 votes)
7 views

Important- Practice Sheet Final ( Array, Stack, Queue, Linked List)

vff

Uploaded by

bajajkeshav861
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Important- Practice Sheet Final ( Array, Stack, Queue, Linked List)

vff

Uploaded by

bajajkeshav861
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LIST OF PROGRAMS FORM ARRAY, STACK, QUEUE and

LINKED LIST
Array:
1. WAC program to find the largest and second largest element in an
array.
2. WAC program to find the maximum and minimum number in an
array(Or find min and second min element).
3. WAC program to reverse an array.
4. WAC program to rotate an array.
5. WAC to generate all the subarrays that has a certain sum (sum may
be any number, you have to generate such pairs that makes up that
sum).
6. WAC to perform insertion and deletion of an element in array.
7. WAC program for removing duplicates from an array.
8. WAC program to sort an array.
9. WAC program for union and intersection of two arrays.
10. WAC program to merge two given arrays.
11. Write a program in C to count the frequency of each element of
an array.
Test Data:
Input the number of elements to be stored in the array :3
Input 3 elements in the array:
element - 0: 25
element - 1: 12
element - 2: 43
Expected Output:
The frequency of all elements of an array:
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times
12. WAC program for addition of two matrices, transpose of a
matrix.
13. Write a program in C to find the sum of the right diagonals
of a matrix.
Hint: for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &arr1[i][j]);
if (i == j) {
sum = sum + arr1[i][j];
}
}
}
14. WAC program to print the sum of rows and columns of a
given matrix.
Hint: Initialize two matrices first,
Then for calculation sum of rows:
for (i = 0; i < n; i++) {
rsum[i] = 0;
for (j = 0; j < n; j++) {
rsum[i] = rsum[i] + arr1[i][j];
}
}
Calculate sum of columns:
for (i = 0; i < n; i++) {
csum[i] = 0;
for (j = 0; j < n; j++) {
csum[i] = csum[i] + arr1[j][i];
}
}
15. Write a program in C to find the missing number in a given
array. There are no duplicates in the list.
Expected Output :
The given array is : 1 3 4 2 5 6 9 8
The missing number is : 7
Hint: int pickMissNumber(int *arr1, int ar_size) {
int i, sum = 0, n = ar_size + 1;

for (i = 0; i < ar_size; i++) {


sum = sum + arr1[i];
}

return (n * (n + 1)) / 2 - sum;


}
16. Write a program in C to find the ceiling in a sorted array.
Given a sorted array in ascending order and a value x, the ceiling
of x is the smallest element in array greater than or equal to x, and
the floor is the greatest element smaller than or equal to x.

Output:
The given array is: 1 3 4 7 8 9 9 10
The ceiling of 5 is: 7
Hint:

int findCeiling(int arr1[], int low, int high, int x) {


int i;
If 'x' is smaller or equal to the first element, return the index
of the first element (comment)
if (x <= arr1[low])
return low;

Traverse the array(Comment)


for (i = low; i < high; i++) {

If 'x' is found in the array, return its index (comment)


if (arr1[i] == x)
return i;

If 'x' is between two elements, return the index of the next


greater element (comment)
if (arr1[i] < x && arr1[i + 1] >= x)
return i + 1;
}
return -1;

17. Write a program in C to replace every element with the


greatest element on its right side.
Output:
The given array is : 7 5 8 9 6 8 5 7 4 6
After replace the modified array is: 9 9 9 8 8 7 7 6 6 0

void replaceWithNextGreatest(int a[], int size) {


int maximum = a[size - 1]; - Initialize maximum as the last
element
a[size - 1] = 0; - Update the last element to 0 as there is no
greater element

Iterate through the array from right to left


for (int i = size - 2; i >= 0; i--) {
int temp = a[i]; - Store the current element temporarily
a[i] = maximum; - Replace the current element with the
maximum

if (maximum < temp)


maximum = temp; -Update maximum if the current element is
greater
}

printf("After replacing, the modified array is: ");


printArray(a, size); // Print the modified array
}
Part B
STACK
1. WAC Program to implement a stack using an array (done in class
notes).
2. Write a C program to implement a stack using Singly Linked List
(already done and uploaded in sheet 2).
3. Write a C program to reverse a stack( sheet 2).
4. Algorithm to Implement Stack using Queue (already done in class).
5. Write a C program to sort a given stack using another stack.
Hint: write same function of push() and pop() done in class
notes and then for sorting:

void sort_stack() {
int temp;
int sortedTop = -1; - Top of the stack used for sorting

- Sorting logic
while (top != -1) {
temp = pop();
while (sortedTop != -1 && sorted_Stack[sortedTop] < temp) {
push(sorted_Stack[sortedTop]);
sortedTop--;
}
sortedTop++;
sorted_Stack[sortedTop] = temp;
}

- Pushing sorted elements back to the original stack


while (sortedTop != -1) {
push(sorted_Stack[sortedTop]);
sortedTop--;
}
6. Write a C program to print the next greater element for each
element in an array stack.
7. WAC program to get the minimum element from a
stack.
Hint: Design push and pop function first
Then;
int getMin() {
if (min_Top < 0) {
printf("Stack is empty\n");
return INT_MIN;
}

return minStack[min_Top];

8. Write a C program that checks whether a string of


parentheses is balanced or not using stack.
Part 3
QUEUE
1. Write a C program using array for all the operations on a Linear
Queue (done in class notes)
2. WAC program using array for all the operations on a Circular
Queue(done in class notes)
3. Write a C program to count the number of elements in a queue.
Hint: same enqueue and dequeue function of linear queue
and then apply a count variable in the display function:
int count() {
int count = 0;
if (front != -1 && rear != -1) is not empty
for (int i = front; i <= rear; i++) {
count++;
}
}
return count;
}
4. Implement a Queue using 2 stacks (already done in class).
5. Implement a Queue using Linked List (Sheet 2).
6. WAC program to find max element in a queue.
Hint: Same enqueue and dequeue function;
Then apply:
int findMax() {
if (front == -1 || front > rear)
printf("Error: Queue is empty\n");
return -1; //
}
int max = queue[front];

for (int i = front; i <= rear; i++) {


if (queue[i] > max) { max
max = queue[i];
}
}
return max;

7. WAC program to sort the elements in a given queue.


Hint: Write Same function for enqueue, dequeue and display
then apply;
void sort_queue_asc() {
int i, j, temp;
int n = rear - front + 1;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++) {
if (queue[i] > queue[j]) {
temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
}
PART 4
LINKED LIST
1. Write insertion and deletion of a node at front, end, specific for
all the three Linked Lists (Singly, Double and Circular).
Ans: All the 15 programs are discussed in class

Note: For Linked List programs see the Sheet “EXTRA


PROGRAMS ON LINKED LIST”.

2. Write a C program to swap the Nth Node from beg with Nth
node for end.
Hint: Count the number of nodes in Linked List
Then;
if (k > n) return;
if (2 * k - 1 == n) return;
Initialise two pointers or beg and end;
struct Node *a = *head, *b = *head;
for (int i = 1; i < k; i++)
a = a->next;
for (int i = 1; i < n - k + 1; i++)
b = b->next;
Swap :
int t = a->data;
a->data = b->data;
b->data = t;
}

You might also like