DS Lab Solve
DS Lab Solve
Let LA is linear array with lower bound LB and upper bound UB. Algorithm traverses LA applying an
operation PROCESS to each element of LA.
1. Set K :=LB
4. Set K :=K+1.
5. Exit.
#include <stdio.h>
int main() {
}
2.ALGORITHM: INSERT (LA, N, K, ITEM)
Here LA is linear array with N elements and K is positive integer such that K<=N. This algorithm inserts an
element ITEM into Kth position in LA.
1. Set J :=N.
4. Set J :=J-1.
6. Set N := N + 1.
7. Exit
#include <stdio.h>
int main() {
N = N + 1; // Step 6: Set N := N + 1
printf("\n");
return 0;}
3.ALGORITHM: PUSH (STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a Stack.
4. Return.
#include <stdio.h>
if (TOP == MAXSTK) {
printf("OVERFLOW\n");
return;
STACK[TOP] = ITEM;
TOP = TOP + 1;
int main() {
push(10);
push(20);
push(30);
push(40);
printf("\n");
return 0;}
4. ALGORITHM: POP (STACK, TOP, ITEM)
This procedure deletes the top element of STACK and assigns it to the variable ITEM.
4. Return.
#include <stdio.h>
if (TOP == MAXSTK) {
printf("OVERFLOW\n");
return;
STACK[TOP] = ITEM;
TOP = TOP + 1;
int pop() {
if (TOP == 0) {
printf("UNDERFLOW\n");
TOP = TOP - 1;
return STACK[TOP];
}
int main() {
push(10);
push(20);
push(30);
printf("\n");
if (poppedItem != -1) {
printf("\n");
return 0;
}
5.ALGORITHM: QINSERT (QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
Set REAR:=1.
Else:
4. Return.
#include <stdio.h>
int FRONT = -1, REAR = -1; // Initialize FRONT and REAR to -1, representing an empty queue
printf("OVERFLOW\n");
return;
if (FRONT == -1) {
FRONT = 0;
REAR = 0;
} else if (REAR == N - 1) {
REAR = 0;
}
else {
REAR++;
QUEUE[REAR] = ITEM;
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
} else {
printf("\n");
return 0;}
6.AGORITHM: QDELETE (QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assign it
Set FRONT:= 1.
4. Return.
#include <stdio.h>
int FRONT = -1, REAR = -1; // Initialize FRONT and REAR to -1, representing an empty queue
int qdelete() {
if (FRONT == -1) {
printf("UNDERFLOW\n");
if (FRONT == REAR) {
FRONT = -1;
REAR = -1;
else if (FRONT == N - 1) {
FRONT = 0;
} else {
FRONT = FRONT + 1;
return item;
int main() {
if (deletedItem != -1) {
FRONT = 0;
QUEUE[REAR] = 10;
QUEUE[++REAR] = 20;
QUEUE[++REAR] = 30;
deletedItem = qdelete();
if (deletedItem != -1) {
if (FRONT == -1) {
printf("EMPTY");
} else {
printf("\n");
return 0;
}
7.Algorithm: (Deleting from a Linear Array) DELETE(LA, N, K, ITEM)
Here LA is a linear array with N elements and K'is a positive integer such that
[End of loop.].
4. Exit.
#include <stdio.h>
return;
LA[J - 1] = LA[J];
(*N)--;
int main() {
int LA[] = {10, 20, 30, 40, 50}; // Example linear array
scanf("%d", &K);
printf("\n");
return 0;
}
8.Algorithm : (Linear Search) LINEAR(DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of
information. This algorithm finds the location LOC of ITEM in DATA, or sets
[End of loop.]-
5. Exit.
#include <stdio.h>
if (LOC == N + 1) {
return LOC;
}
int main() {
int N;
scanf("%d", &N);
scanf("%d", &DATA[i]);
int ITEM;
scanf("%d", &ITEM);
if (LOC == 0) {
} else {
return 0;
}
9.Algorithm : (Binary Search) BINARY(DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and
ITEM is a given item of information. The variables BEG, END and MID
elements of DATA. This algorithm finds the location LOC of ITEM in DATA
Else:
[End of If structure.]
Else:
[End of If structure.]
6. Exit.
#include <stdio.h>
int MID;
while (BEG <= END) {
if (DATA[MID] == ITEM) {
} else {
int main() {
int N;
scanf("%d", &N);
int DATA[N];
scanf("%d", &DATA[i]);
int ITEM;
if (LOC != -1) {
} else {
return 0;
}
10. Algorithm:A text T and patterns P and Q are in memory. This
algorithm replaces every occurrence of P in T by Q.
1. [Find index of P.] Set K := INDEX(T, P).
2. Repeat while K != 0:
[End of loop:]
3. Write: T.
4. Exit.
1. Write a program which reads a line of text in array TEXT and then replace every occurrence
of the given word stored in PREV with the new word stored in NEW.
#include<bits/stdc++.h>
int main() {
string text;
string prev;
string NEW;
getline(cin, text);
getline(cin, prev);
getline(cin, NEW);
int r = text.size();
int s = prev.size();
int max = r - s + 1;
found = 1;
if (text.substr(i, s) == prev) {
continue;
} else {
found = 0;
if (found == 1) {
text.replace(i, s, NEW);
match = 1;
if (match == 0) {
} else {
return 0;
}
11.Algorithm:
Write a procedure which counts the number NUM of times the word "the" appears in the
short story S. (We do not count "the" in "mother," and we assume no sentence ends with
MID := 'D'//WORD//'D’
[End of If structure.]
7. Return.
2. Write a program which reads a line of text in array TEXT and counts the number of
Sample Output: 2
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
string prev;
getline(cin, text);
getline(cin, prev);
int r = text.size();
int s = prev.size();
int max = r - s + 1;
found = 1;
if (text.substr(i, s) == prev) {
continue;
} else {
found = 0;
if (found == 1) {
count++;
match = 1;
}
}
if (match == 0) {
} else {
return 0;
}
12. Algorithm to Find the Adjacency Matrix:
1. Initialize an MxM matrix (adjacency matrix) with all elements set to 0.
#include <iostream>
#include <vector>
int main() {
cin >> M;
cin >> N;
int u, v;
adj[u][v] = 1;
}
// Print the adjacency matrix
return 0;
}
13. Algorithm to Find the Location of an Item in a BST:
1. **Input**:
2. Initialize variables:
3. **Search Loop**:
- Otherwise:
4. **Output**:
- If the loop terminates without finding the item (i.e., `index` is NULL_INDEX), return NULL_INDEX (item
not found).
#include <stdio.h>
#define SIZE 9
#define NULL_INDEX -1
if (key[index] == item) {
} else {
int main() {
int item = 6;
if (location != NULL_INDEX) {
} else {
return 0;
}
14. Algorithm for Preorder Traversal of Binary Tree (Parallel Arrays):
1. **Input**:
3. **Main Program**:
- Print a newline.
4. **Output**:
- The program will print the keys of the nodes in preorder traversal order.
#include <stdio.h>
#define SIZE 9
#define NULL_INDEX -1
// Parallel arrays to store the binary tree
if (index != NULL_INDEX) {
int main() {
printf("\n");
return 0;
}
15. ALGORITHM: INSERT END (INFO, LINK, START, AVAIL, ITEM)
This algorithm inserts ITEM as the END node in the List.
7. Exit.
#include <stdio.h>
#include <stdlib.h>
int LINK[MAX];
int INFO[MAX];
int AVAIL;
int START = -1; // Assume START is initially NULL (-1 in our case)
void initializeAvail() {
LINK[i] = i + 1;
AVAIL = 0;
printf("OVERFLOW\n");
return;
// Step 2
AVAIL = LINK[AVAIL];
// Step 3
INFO[newNode] = ITEM;
// Step 4
// Step 5
START = newNode;
LINK[newNode] = -1;
} else {
PTR = LINK[PTR];
// Step 6
LINK[PTR] = newNode;
LINK[newNode] = -1;
// Step 7
return;
void printList() {
PTR = LINK[PTR];
printf("NULL\n");
int main() {
initializeAvail();
addItem(10);
addItem(20);
addItem(30);
printList();
return 0;
}
16. (Warshall's Algorithm)
A directed graph G with M nodes is maintained in
memory by its adjacency matrix A. This algorithm finds the (Boolean) path
[End of loop.]
[End of loop.]
5. Exit.
#include <iostream>
int P[MAX_NODES][MAX_NODES];
// Warshall's Algorithm
int main() {
cin >> M;
warshallAlgorithm(A, M);
return 0;
}
17. QUICK(A, N, BEG, END, LOC)
Here A is an array with N elements. Parameters BEG and END contain the
keeps track of the position of the first element A[BEG] of the sublist during
the procedure. The local variables LEFT and RIGHT will contain the boundary
[End of loup.]
A[RIGHT] := TEMP.
(iii) Go to Step.3.
[End of If structure.]
LEFT := LEFT+1.
[End of loop.]
A[LEFT] := TEMP.
(ii) Set LOC := LEFT.
(iii) Go to Step. 2.
#include <iostream>
#include <vector>
std::swap(arr[++i], arr[j]);
// QUICK procedure
void QUICK(std::vector<int>& arr, int N, int BEG, int END, int& LOC) {
LOC = BEG;
// Scan from right to left
RIGHT--;
if (LOC == RIGHT) {
std::swap(arr[LOC], arr[RIGHT]);
LOC = RIGHT;
LEFT++;
if (LOC == LEFT) {
std::swap(arr[LEFT], arr[LOC]);
LOC = LEFT;
}
int main() {
int n;
std::cin >> n;
std::vector<int> arr(n);
int loc;
QUICK(arr, n, 0, n - 1, loc);
}
std::cout << std::endl;
return 0;