DSA Full Final
DSA Full Final
DSA Full Final
Name-
Registration number-
BONAFIDE CERTIFICATE
Registration no.
Certified to be the Bonafide record of work done by _________ of 3th semester 2nd year
B. TECH degree course in SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, NCR Campus
of Department of Computer Science & Engineering, in Data Structure & Algorithm, during
the academic year 2023-24.
Exp.
Title of Experiments Date of
No. Signature
submission
1 Implementation of Structure.
12
AIM:
ALGORITHM:
• Define the structure using the struct keyword, specifying its fields (data members) within
curly braces.
• Access and manipulate the fields of the structure variables using the dot (.) operator.
PROGRAM:
{ char name[50];
int stu_id; float
cgpa; } person1;
int main()
{
}
OUTPUT :
Name:George
student id :2211
student cgpa : 9.68
EXPERIMENT: 2
AIM:
ALGORITHM:
STEP 1: Enter the input for creating a structure of student database. STEP 2: Create a structure
pointer using normal structure variable and pointer variable
STEP 3:Dot (.) operator is used to access the data using normal structure variable and arrow (->) is
used to access data using pointer variable. STEP 4: Student details are printed using normal and
pointer structure variable.
PROGRAM:
struct student
};
0; }
INPUT & OUTPUT
name : Brown
phone_number : : 123443
Second Student
roll_no : 2
name : Sam
phone_number : 1234567822
Third Student
roll_no : 3
name : Addy
phone_number : 1234567844
EXPERIMENT: 3
AIM:
ALGORITHM:
• Allocate memory dynamically for the matrices using the malloc function.
PROGRAM
};
(int*)malloc(columns * sizeof(int));
return matrix;
}
void freeMatrix(struct Matrix* matrix) { for (int i = 0; i < matrix-
>rows; i++) { free(matrix->data[i]);
free(matrix->data); free(matrix);
>columns);
result->data[i][j] = sum;
} }
return result;
>data[i][j]);
printf("\n");
}
int main() { int rows1, columns1, rows2, columns2; printf("Enter the number of rows and
columns of the first matrix: "); scanf("%d %d", &rows1, &columns1); printf("Enter the
number of rows and columns of the second matrix: "); scanf("%d %d", &rows2,
&columns2); if (columns1 != rows2) { printf("Matrix multiplication is not possible. Invalid
dimensions!\n"); return 1;
}
OUTPUT:
Enter the number of rows and columns of the first matrix: 2 2 Enter the number of rows
and columns of the second matrix: 2 2
Resultant matrix:
16 21
18 24
EXPERIMENT: 4
AIM:
ALGORITHM:
• Define the maximum size of the list (array size) and initialize variables for tracking the
number of elements in the list.
• Implement functions for common list operations (e.g., insertion, deletion, searching, and
displaying).
• In each function, handle edge cases (e.g., checking for empty or full list, handling invalid
indices).
PROGRAM
#include <stdio.h> #define MAX_SIZE
100
} list->size--
;}
return
0;
}
OUTPUT:
List: 5 10 15
List: 5 15
EXPERIMENT: 5
AIM:
ALGORITHM:
1. Start
6. If choice = 1 then
14. Else
15. Traverse the list from Head node to node which points to null
16. Stop
PROGRAM
!= 7)
printf("\n*********Main Menu*********\n");
printf("\n=====================================
last\n3.Delete from
case 1:
beginsert(); break; case
2:
void beginsert()
else
printf("\nnode inserted\n");
void lastinsert()
else
printf("\nEnter Data?");
scanf("%d",&item); ptr>data = item;
if(head == NULL) { head = ptr; ptr ->
next = head; } else { temp = head;
while(temp -> next != head) { temp =
temp -> next; } temp -> next = ptr; ptr
-> next = head;
printf("\nnode inserted\n");
void begin_delete()
void last_delete()
else
void search()
printf("\nEmpty List\n");
else
item)
{
printf("item found at location %d",i+1); flag=0;
} else
if(ptr->data == item)
if(flag != 0)
}}}
void display()
printf("\nnothing to print");
else
}
OUTPUT:
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
6.Show
7.Exit
inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
6.Show
choice?
2 Enter Data?
20 node
inserted
*********Main Menu*********
===============================================
1.Insert in beginning
2.Insert at last
6.Show
7.Exit
6 printing values
...
20
10
*********Main Menu*********
1.Insert in beginning
2.Insert at last
6.Show
7.Exit
AIM:
ALGORITHM:
=NewNode Stop
Start
Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
Start
Create a NewNode
NewNode → DATA = DATA
b. NewNode → LPoint=NULL
Else
a. TEMP = START
Stop
v. Backward Traversal
Start
Repeat the step 5 and 6 until (TEMP == NULL ) Display “TEMP → DATA”
PROGRAM
#include <stdio.h>
stnod
e = NULL; en node = NULL; printf("\n\n Doubly Linked List : Create and display a
doubly linked
list :\n");
scanf("%d", &n);
DlListcreation(n); displayDlList();
return 0;
void DlListcreation(int n)
printf(" Input data for node 1 : "); // assigning data in the first node scanf("%d",
&num); stnode->num = num; stnode->preptr = NULL; stnode->nextptr = NULL; ennode = stnode;
scanf("%d", &num); fnNode->num = num; fnNode->preptr = ennode; // new node is linking with
the previous node fnNode->nextptr = NULL;
ennode->nextptr = fnNode; // previous node is linking with the new node ennode = fnNode; //
assign new node as last node
else
}
}
else
void displayDlList()
else
}}}
OUTPUT:
Main Menu
1. Insert at beginning
2. Insert at end
6. Display list
7. Exit
inserted at beginning.
inserted at end.
List: 10 <-> 20
List: 20
7
EXPERIMENT: 7 A
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
push(value) - Inserting value into the stack Step 1 - Check whether stack is FULL.
(top == SIZE-1)
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value). pop() - Delete a value from the Stack
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
display() - Displays the elements of a Stack Step 1 - Check whether stack is EMPTY. (top
== -1)
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function. Step 3 -
If it is NOT EMPTY, then define a variable 'i' and initialize with top.
Display stack[i] value and decrement i value by one (i--). Step 3 - Repeat above step until i value
becomes '0'.
PROGRAM
#include<stdio.h> #include<conio.h>
int stack[10],choice,n,top,x,i; // Declaration of variables void push(); void pop(); void display();
int main()
top = -1; // Initially there is no element in stack printf("\n Enter the size of STACK : ");
scanf("%d",&n); printf("\nSTACK IMPLEMENTATION USING ARRAYS\n");
do {
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
}}}
while(choice!=4); return 0;
void push()
{ if(top >= n - 1)
printf("\nSTACK OVERFLOW\n");
}
else
{
printf(" Enter
a value to be
pushed
: ");
scanf("%d",&x);
}}
void pop()
{
if(top <= -1)
printf("\nSTACK UNDERFLOW\n");
} else
{
printf("\nThe popped element is %d",stack[top]); top--; // Decrement TOP after a pop }} void
display()
If (top >= 0)
printf("\nEMPTY STACK\n");
OUTPUT :
*********Main Menu*********
1. Push
2. Pop
3. Display
4. Exit
10 10 pushed to stack.
20
20 pushed to stack.
Stack elements: 10 20
Popped value: 20
3 Stack elements:
10
4
EXPERIMENT: 7 B
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 4 - Implement the main method by displaying Menu with list of operations and make suitable
function calls in the main method.
push(value) - Inserting an element into the Stack Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL) Step 3 - If it is Empty, then set newNode →
next = NULL.
Step 4 - If it is Not Empty, then set newNode → next = top. Step 5 - Finally, set top = newNode.
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the
function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4 - Then set 'top = top → next'. Step 5 - Finally, delete 'temp'. (free(temp)).
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until temp
reaches to the first node in the stack. (temp → next != NULL).
PROGRAM
Node
{
::\n"); while(1){
printf("\n****** MENU ******\n"); printf("1. Push\n2.
Pop\n3. Display\n4. Exit\n"); printf("Enter your choice: ");
scanf("%d",&choice); switch(choice){ case 1: printf("Enter
the value to be insert: ");
void display()
printf("%d--->NULL",temp->data);
}
OUTPUT :
*********Main Menu*********
1.
Push
2. Pop
3. Display
4. Exit
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1,
rear = -1)
Step 5 - Then implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on queue.
enQueue(value) - Inserting value into the queue Step 1 - Check whether queue is FULL. (rear
== SIZE-1)
Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear] = value.
deQueue() - Deleting a value from the Queue Step 1 - Check whether queue is EMPTY. (front
== rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate
the function.
Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++). Then display
queue[front] as deleted element. Then check whether both front and rear are equal (front == rear),
if it TRUE, then set both front and rear to '1' (front = rear = -1).
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value
reaches to rear (i <= rear)
PROGRAM
#include<stdio.h>
}}
int i;
}
OUTPUT :
*********Main Menu*********
1. Enqueue
2. Dequeue
3. Display
4. Exit
10 enqueued.
20 enqueued.
Queue elements: 10 20
Dequeued value: 10
Queue elements: 20
4
EXPERIMENT: 8 B
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - Implement the main method by displaying Menu of list of operations and make suitable
function calls in the main method to perform user selected operation.
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 3 - If it is Empty then, set front = newNode and rear = newNode. Step 4 - If it is Not Empty
then, set rear → next = newNode and rear = newNode.
Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate
from the function
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to
'front'.
Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)). display() - Displaying the
elements of Queue
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp'
reaches to 'rear' (temp → next != NULL). Step 5 - Finally! Display 'temp → data ---> NULL'.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice, value;
switch (choice) {
case 1:
printf("Enter the value to be
inserted: "); scanf("%d", &value);
insert(value); break; case 2:
delete(); break; case 3:
display();
break; case 4:
exit(0); default:
printf("\nWrong selection!!!
Please try again!!!\n");
}
}
if (front == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("\nInsertion is successful!!!\n");
}
*********Main Menu*********
1. Enqueue
2. Dequeue
3. Display
4. Exit
AIM:
ALGORITHM:
To convert Infix Expression into Postfix Expression using a stack data structure, We can use the
following steps...
• Read all the symbols one by one from left to right in the given Infix Expression.
• If the reading symbol is operand, then directly print it to the result (Output).
• If the reading symbol is left parenthesis '(', then Push it on to the Stack.
• If the reading symbol is right parenthesis ')', then Pop all the contents of stack until
respective left parenthesis is poped and print each poped symbol to the result.
• If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack.first pop the
operators which are already on the stack that have higher or equal precedence than
current operator and print them to the result.
PROGRAM :
#include<stdio.h>
#include<ctype.h> char stack[100]; int top
= -1; void push(char x)
stack[++top] = x;
char pop()
int priority(char x)
{
if(x == '(') return 0; if(x == '+' ||
int main()
if(isalnum(*e)) printf("%c
",*e); else if(*e == '(')
push(*e);
else if(*e ==
')')
", x);
else
} e++;
while(top != -1)
printf("%c ",pop());
return 0;
OUTPUT:
AIM:
ALGORITHM:
START
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1 move disk from source to dest // Step
2
PROGRAM :
#include <stdio.h>
#define MAX_DISK 10
int to = 0;
char sndl, indl, dndl;
stkn[++top] = num;
stksndl[top] = src;
stkindl[top] = dest;
stkdndl[top] = aux;
stkadd[top] = 3;
while (top != -1) { int
add = stkadd[top];
num = stkn[top]; sndl
= stksndl[top]; indl =
stkindl[top];
dndl = stkdndl[top];
top--;
if (add == 3) {
if (num == 1) {
printf("\nMove top disk from needle %c to needle %c.", sndl, indl);
continue;
}
stkn[++top] = num - 1;
stksndl[top] = sndl;
stkindl[top] = dndl;
stkadd[top] = 3;
stkdndl[top] = indl;
stksndl[++top] = indl;
stkindl[top] = dndl;
OUTPUT:
Non-Recursive
Move top disk from needle A to needle C Move top disk from needle
A to needle B Move top disk from needle C to needle B Move top
disk from needle A to needle C Move top disk from needle B to
needle A
Move top disk from needle A to needle B. Move top disk from needle A to needle
C.
Move top disk from needle B to needle C. Move top disk from needle
A to needle B. Move top disk from needle C to needle A. Move top disk
from needle C to needle B.
AIM:
ALGORITHM:
STEP 2: If root exists then compare the data with node.data while until insertion position is located
If
else
(B) SEARCH
If root.data is equal to search.data return root else while data not found
If data is greater than node.data goto right subtree else goto left subtree
end if
PROGRAM
node {
node *right;
} node;
node *create() {
node *p;
int x;
(node*)malloc(sizeof(node)); p->data
create(); return p;
preorder(root);
return 0;
}
OUTPUT:
child of 5:
Enter data (-1 for no data): -1
RESULT:
Thus the C Program for Implementation of Tree has been executed successfully.
EXPERIMENT: 10B
AIM:
ALGORITHM:
structure BTREE
declare
end
end BTREE
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct Node {
Node *right;
};
count = 0;
display(struct Node*);
int main() { int choice, value; clrscr(); // Clear screen (platform-specific, might
while (1) {
printf("\n***** MENU *****\n");
&choice);
switch (choice) {
case 1:
break;
case 2:
display(root);
break; case 3:
exit(0); default:
struct Node* insert(struct Node *root, int value) { struct Node *newNode =
if (root == NULL) {
count++;
return newNode; // Return the new node as the root if the tree is empty
} else {
return root;
display(root->right);
}
OUTPUT:
----- Binary Tree -----
1. Insert
2. Display
3. Exit
1. Insert
2. Display
3. Exit
1. Insert
2. Display
3. Exit
1. Insert
2. Display
3. Exit
10 20 30
1. Insert
2. Display
3. Exit
RESULT: Thus the C Program for Implementation of Binary Tree has been executed successfully.
EXPERIMENT: 11
AIM:
ALGORITHM:
FUNCTION search(data):
PRINT current.data
RETURN current
ELSE:
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
struct node {
node *right;
};
// Function for inorder traversal void
>data); inorder(root-
>right);
int main() {
int n, i;
scanf("%d", &n);
>right = NULL;
if (i == 0) {
} else {
(q->right == NULL) {
q->right = p;
break;
} else {
} else {
if (q->left == NULL) {
q->left = p; break;
} else {
inorder(root); printf("\n");
return 0;
}
OUTPUT:
Binary Search Tree nodes in Inorder Traversal: 8 10 12 15 20
RESULT:
Thus the C Program for Implementation of Binary SearchTree using Linked List has been executed successfully.
EXPERIMENT: 12
AIM:
B-Tree Algorithm
B-TREE-CREATE
1. Function: B-TREE-CREATE(T) o
o Set root[T] = x.
B-TREE-INSERT
1. Function: B-TREE-INSERT(T, k) o
Let r = root[T].
o If n[r] = 2t - 1:
▪ Set root[T] = s.
B-TREE-SEARCH
1. Function: B-TREE-SEARCH(x, k) o
Set i = 1.
▪ Increment i.
B-TREE-DELETION
2. If k is in internal node x:
▪ Find predecessor k' and delete it, replacing k with k'. o Else if
o Otherwise:
Program:
#include <stdio.h>
#include <stdlib.h>
1. Insert
2. Delete
3. Search
4. Display
5. Quit
1. Insert
2. Delete
3. Search
4. Display
5. Quit
1. Insert
2. Delete
3. Search
4. Display
5. Quit
B-tree is:
10 20
RESULT:
Thus the C Program for Implementation of B- Tree using Linked List has been executed successfully.
EXPERIMENT: 13
AIM:
To write a c program to implement Graph using Array to print the adjacent Matrix.
ALGORITHM
Creation of a graph
STEP 3:Read the origin,destination value and weight value from the user
STEP 5:Otherwise read the weight value of the edge from the user
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
**adj_matrix;
char d; int r,
c, nodes;
&nodes);
*)malloc(sizeof(int) * nodes);
in memory allocation!");
return -1;
cautiously; it's undefined in some compilers. scanf(" %c %d %d", &d, &r, &c);
if (r > 0 && r <= nodes && c > 0 && c <= nodes) { adj_matrix[r
} else {
} else {
printf("Invalid node pair. Please enter valid nodes between 1 and %d.\n", nodes);
printf("\nAdjacency Matrix\n");
", c + 1);
printf("\n");
printf("---");
printf("\n");
free(adj_matrix[r]);
}
free(adj_matrix);
return 0;
}
OUTPUT:
Number of Nodes: 4
Adjacency Matrix
1234
------------- 1|
0100
2| 0 0 1 0
3| 0 1 0 1
4| 0 0 0 0
RESULT:
Thus the C Program for Implementation of Graph has been executed successfully.
EXPERIMENT:14
AIM:
ALGORITHM
STEP 1:Assign to every node a distance value. Set it to zero for our initial node and to infinity for all other nodes.
STEP 3:For current node, consider all its unvisited neighbors and calculate their distance (from the initial node).
For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the
distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the
beginning, zero for the initial node), overwrite the distance.
STEP 4:When we are done considering all neighbors of the current node, mark it as visited. A visited node will not
be checked ever again; its distance recorded now is final and minimal.
STEP 5:Set the unvisited node with the smallest distance (from the initial node) as the next "current node" and
continue from step 3 .
(vii)while Q is not empty: // The main loop (viii)u := vertex in Q with smallest dist[]
(xi)remove u from Q
(xii)for each neighbor v of u: // where v has not yet been removed from alt := dist[u] + dist_between(u, v)
(xiiii)previous[v] := u
(xv)return previous []
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define IN INT_MAX
#define N 6
}
}
int distance = dijkstra(cost, source - 1, target - 1); // Adjusting for zero-based indexing
printf("\nThe Shortest Path Distance: %d\n", distance);
return 0;
}
while (selected[target] == 0) {
min = IN;
m = -1; // Reset m to an invalid value for comparison
// Construct path
start = target;
j = 0;
while (start != -1) { path[j++] = start + 65; // Convert
to ASCII (A, B, C...) start = prev[start];
}
path[j] = '\0'; // Null terminate the string
}
Output :