Abhiroop dsa file
Abhiroop dsa file
AND MANAGEMENT
Affiliated to MDU
2
3 Write a program to 20-22
demonstrate the use of
stack(implemented using
linear array in converting
expression from infix
notation to postfix notation.
4 Program to 23-25
demonstrate
the
implementation
of various
operations on a
linear queue
represented
using a linear
array.
4
Program 1
Aim:
Write a menu driven program that implements following
operations using separate functions for each using a linear array:
insert a new element at end as well as at a given position
delete an element from a given whose value is given or
whose position is given
linear array.
Code:
value; do { printf("\nMenu:\n");
{ case 1:
scanf("%d", &element);
break; case 2:
6
position, element); break; case
3:
break; case 4:
position);
break; case 6:
show(arr, size);
break; case 7:
default:
{ arr[*size] = element;
end.\n", element);
8
{ printf("Invalid position for insertion.\n");
return;
}
for (int i = *size; i > position; i--)
arr[position] = element;
(*size)--;
found = 1;
printf("Element
%d deleted from
the array.\n",
value);
break;
arr[i + 1];
(*size)--;
printf("Element at position %d deleted from the array.\n",
10
(int i = 0; i < size; i++) { if (arr[i] == element)
{ return i;
{ printf("Array elements:
arr[i]);
printf("\n");}
Program 2
Aim:
Write a menu driven programme that maintains a linear linked
list whose elements are stored in a ascending order and
implements the following operations using seperate functions
insert a new element
delete an existing element
12
struct Node { int
data; struct Node
*next;
}; struct Node* createNode(int data); struct Node*
ins_element(struct Node* head, int element); struct Node*
del_element(struct Node* head, int element); int
search(struct Node* head, int element); void show(struct
Node* head); int main() { struct Node *head = NULL;
int choice, element, result;
do {
printf("\nWhat would you like to do?:\n");
printf("1. Insert an element in the list\n");
printf("2. Delete an element from the list\n");
printf("3. Search for an element\n");
printf("4. Display the linked list\n");
printf("5. Quit\n"); printf("Enter your
choice: "); scanf("%d", &choice);
switch (choice) { case 1:
printf("Enter element to insert in the linked list:
"); scanf("%d", &element); head =
ins_element(head, element); break; case 2:
printf("Enter element to delete from the list:
"); scanf("%d", &element); head =
del_element(head, element); break;
case 3:
printf("Enter element to search in the list: ");
scanf("%d", &element);
result = search(head, element); if
(result) printf("Element found in the
list.\n"); else printf("Element
not found in the list.\n"); break;
case 4: show(head); break;
case 5:
printf("Exiting the
program\n"); break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5); return
0; } struct Node* createNode(int
data) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node)); if (newNode ==
14
NULL) { printf("Memory allocation failed.\n");
exit(1);
} newNode->data =
data; newNode->next =
NULL; return newNode;
} struct Node* ins_element(struct Node* head, int
element) { struct Node *newNode =
createNode(element); struct Node *current = head,
*prev = NULL; while (current != NULL && current-
>data < element) { prev = current; current =
current->next;
}
if (prev == NULL)
{ newNode->next = head;
head = newNode;
} else { newNode-
>next = current; prev-
>next = newNode;
}
printf("Element %d inserted into the linked list.\n", element);
return head;
} struct Node* del_element(struct Node* head, int
element) { struct Node *current = head, *prev = NULL;
while (current != NULL && current->data != element)
{ prev = current; current = current->next;
}
if (current != NULL)
{ if (prev == NULL)
{ head = current-
>next;
} else { prev->next =
current->next;
} free(current); printf("Element %d deleted
from the list.\n", element);
} else { printf("Element %d not found in the
list.\n", element);
} return
head;
} int search(struct Node* head, int element) { struct
Node* current = head; while (current != NULL &&
current->data < element) { current = current->next;
}
return (current != NULL && current->data == element);
} void show(struct Node* head)
{ printf("List elements: ");
struct Node* current = head;
while (current != NULL)
16
{ printf("%d ", current-
>data); current = current-
>next;
}
printf("\n");
}
Program 3
Aim:
Write a program to demonstrate the use of stack(implemented
using linear array in converting expression from infix notation to
postfix notation.
Code:
#include <stdio.h>
#include <ctype.h>
char stack[100]; int
top = -1; void
push(char x)
{ stack[++top] =
x;
} char pop() { if (top
== -1) { return -
1; } else
{ return stack[top-
-];
}
} int priority(char
x) { if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1; if (x == '*' || x == '/') return 2;
return 0; } int main() { char exp[100]; char *e, x;
printf("Enter the expression: "); scanf("%s", exp);
printf("The postfix notation for the given infix expression
is:\n"); e = exp; while (*e != '\0') { if
18
(isalnum(*e)) { printf("%c ", *e); } else if (*e
== '(') { push(*e); } else if (*e == ')')
{ while ((x = pop()) != '(') { printf("%c ",
x);
}
} else {
while (priority(stack[top]) >= priority(*e)) {
printf("%c ", pop());
}
push(*e);
}
e++;
}
while (top != -1) {
printf("%c ", pop());
}
printf("\n");
return 0;
}
20
Program 4
Aim:
Program to demonstrate the implementation of various
operations on a linear queue represented using a linear array.
Code:
#include <stdio.h> #define N 5 int main() { int
queue[N], ch = 1, front = 0, rear = 0, i, j = 1;
printf("Queue using Array\n"); printf("1.
Insertion\n"); printf("2. Deletion\n");
printf("3. Display\n"); printf("4. Exit\n");
while (ch) { printf("\nEnter your choice: ");
scanf("%d", &ch); switch (ch) { case
1:
if (rear == N)
{ printf("\nQueue is Full\n");
} else { printf("\nEnter
number %d: ", j++); scanf("%d",
&queue[rear++]);
}
break;
case 2:
if (front == rear)
{ printf("\nQueue is Empty\n");
} else { printf("\nDeleted
Element is %d\n", queue[front++]);
}
break;
case 3:
printf("\nQueue Elements are:\n");
if (front == rear)
{ printf("Queue is Empty\n");
} else { for (i =
front; i < rear; i++)
{ printf("%d ", queue[i]);
}
printf("\n");
}
break;
case 4:
printf("Exiting the program\n");
return 0;
default:
printf("Wrong Choice. Please see the options.\n");
break;
} }
return 0;
}
22
Program 5 Aim:
Program to demonstrate the implementation of various
operations on a circular queue represented using a linear array.
Code :
#include <stdio.h> #define MAX 6 int queue[MAX]; int
front = -1; int rear = -1; void enqueue(int element) { if
((rear + 1) % MAX == front) { // Condition to check if
queue is full printf("Queue is overflow.\n"); } else if
(front == -1 && rear == -1) { front = 0; rear =
0; queue[rear] = element;
} else { rear = (rear +
1) % MAX; queue[rear]
= element;
}
}
24
Program 6
Aim:
Program to illustrate implementation of different operations on a
BST.
Code:
#include <stdio.h>
#include <stdlib.h> struct
node { int data; struct
node *right_child;
struct node *left_child;
}; struct node* new_node(int x)
{ struct node *temp; temp =
malloc(sizeof(struct node)); temp-
>data = x; temp->left_child =
NULL; temp->right_child =
NULL; return temp;
} struct node* search(struct node *root, int
x) { if (root == NULL || root->data == x)
return root;
else if (x > root->data) return
search(root->right_child, x); else
return search(root->left_child, x);
} struct node* insert(struct node *root, int x) { if
(root == NULL) return new_node(x); else if
(x > root->data) root->right_child =
insert(root->right_child, x); else root-
>left_child = insert(root->left_child, x); return
root;
} struct node* find_minimum(struct node
*root) { if (root == NULL) return
NULL; else if (root->left_child != NULL)
return find_minimum(root->left_child);
return root;
} struct node* delete(struct node *root, int
x) { if (root == NULL)
return NULL; if (x > root->data) root-
>right_child = delete(root->right_child, x); else if
(x < root->data) root->left_child = delete(root-
>left_child, x); else {
if (root->left_child == NULL && root->right_child ==
NULL) {
free(root);
return NULL;
26
} else if (root->left_child == NULL || root->right_child ==
NULL) {
struct node *temp; if
(root->left_child == NULL)
temp = root->right_child; else
temp = root->left_child;
free(root); return temp;
} else { struct node *temp = find_minimum(root-
>right_child); root->data = temp->data; root-
>right_child = delete(root->right_child, temp>data);
} } return root; } void
inorder(struct node *root) { if
(root != NULL)
{ inorder(root->left_child);
printf("%d ", root->data);
inorder(root->right_child);
}
} int main() { struct node *root =
new_node(20); insert(root, 52);
insert(root, 13); insert(root, 17);
insert(root, 14); insert(root, 7);
insert(root, 12); insert(root, 3);
insert(root, 5); insert(root, 20);
insert(root, 65);
insert(root, 92); printf("The BST in inorder
traversal is: \n"); inorder(root);
printf("\n"); root = delete(root, 12); root =
delete(root, 65); root = delete(root, 52);
root = delete(root, 3); printf("After deletion,
the BST becomes: \n"); inorder(root);
printf("\n"); return 0; } void display() { int
i = front; if (front == -1 && rear == -1)
{ printf("Queue is empty.\n");
} else { printf("Elements in the
Queue are: "); while (i != rear)
{ printf("%d ", queue[i]); i
= (i + 1) % MAX;
}
printf("%d\n", queue[rear]);
}
} int main() { int choice = 1, x; while
(choice < 4 && choice != 0)
{ printf("\nPress 1: Insert an
element"); printf("\nPress 2: Delete
an element"); printf("\nPress 3:
Display the queue"); printf("\nEnter
28
your choice: "); scanf("%d",
&choice); switch (choice)
{ case 1:
printf("Enter the element to be inserted:
"); scanf("%d", &x);
enqueue(x); break; case 2:
dequeue(); break; case 3:
display(); break;
default:
printf("Invalid choice. Please try again.\n");
} }
return 0;
}
Program 7
Aim:
Program to illustrate the traversal of graph using BFS.
Code:
#include <stdio.h>
int n, i, j; int visited[10], queue[10], front = -1,
rear = -1; int adj[10][10]; void bfs(int v)
{ visited[v] = 1; queue[++rear] = v;
while (front < rear) { int current =
queue[++front]; for (i = 1; i <= n; i++)
{ if (adj[current][i] && !visited[i])
{ queue[++rear] = i;
visited[i] = 1;
}
}
30
}
} int
main()
{ int v;
printf("Ent
er the
number of
vertices: ");
scanf("%d"
, &n);
for (i = 1; i
<= n; i++)
{ que
ue[i] = 0;
visited[i] =
0;
}
printf("Enter graph data in matrix form:\n");
for (i = 1; i <= n; i++) for (j = 1; j <= n;
j++) scanf("%d", &adj[i][j]);
printf("Enter the starting vertex: ");
scanf("%d", &v); bfs(v); printf("The
nodes that are reachable are:\n"); int
reachable = 0; for (i = 1; i <= n; i++)
{ if (visited[i]) { printf("%d\t", i);
reachable = 1;
} } if
(!reachable)
{ printf("BF
S is not possible.
Not all nodes are
reachable.");
}
printf("\n");
return 0;
}
32
Program 8
Aim:
Program to illustrate the traversal of graph using DFS.
Code:
#include <stdio.h>
#include <stdlib.h>
vertex; struct
node* next;
totalVertices; int*
adjLists;
}; struct node* createNode(int v) { struct node*
>vertex = v;
newNode->next = NULL;
return newNode;
i++) { graph->adjLists[i] =
NULL; graph->visited[i] =
0;
} return
graph;
34
newNode->next = graph->adjLists[src]; graph-
>visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
temp = temp->next;
}
} void displayGraph(struct Graph* graph)
temp = temp->next;
printf("NULL");
printf("\n");
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
DFS(graph, 1);
printf("NULL\n");
return 0;
36
}
Program 9
Aim :
Program to sort an array of integers in ascending using bubble
sort.
Code
#include <stdio.h> void
bubble_sort(int arr[], int n) {
int i, j; for (i = 0; i < n - 1;
i++) { for (j = 0; j < n - i -
1; j++) { if (arr[j] > arr[j
+ 1]) { int temp =
arr[j]; arr[j] = arr[j +
1]; arr[j + 1] = temp;
}
}
}
} int main() { int arr[] = {48, 21, 67,
12, 25, 9, 87}; int n = sizeof(arr) /
sizeof(arr[0]); bubble_sort(arr, n);
return 0;
}
38
Program 10
Aim:
Code:
#include <stdio.h>
*a = *b;
*b = temp;
min_idx = i;
swap(&array[min_idx], &array[step]);
40
for (int i = 0; i < size; ++i) {
}
printf("\n");
int main() {
selectionSort(data, size);
printArray(data, size);
return 0;
}
Program 11
Aim:
42
int i; for (i = 0;
i < n; i++)
printf("%d ", a[i]);
printf("\n");
} int main() { int a[] = { 21, 32, 45, 18, 22,
1 }; int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n); insert(a, n);
printf("After sorting array elements are - \n");
printArr(a, n); return 0;
}
Program:12
Aim:
Program to sort an array of integers in ascending using radix
sort.
Code
#include <stdio.h> int
getMax(int array[], int n)
{ int max = array[0];
for (int i = 1; i < n; i++)
{ if (array[i] > max)
max = array[i];
} return
max;
} void countingSort(int array[], int size, int
place) { int output[size]; int count[10] =
{0}; for (int i = 0; i < size; i++)
{ count[(array[i] / place) % 10]++;
} for (int i = 1; i < 10;
i++) { count[i] +=
count[i - 1];
} for (int i = size - 1; i >= 0; i--)
{ output[count[(array[i] / place) % 10] - 1] =
array[i];
46
count[(array[i] / place) % 10]--;
} for (int i = 0; i < size;
i++) { array[i] =
output[i];
} } void radixsort(int array[], int size) { int
max = getMax(array, size); for (int place = 1;
max / place > 0; place *= 10)
{ countingSort(array, size, place);
} } void printArray(int array[], int
size) { for (int i = 0; i < size; i++)
{ printf("%d ", array[i]);
}
printf("\n");
} int main() { int array[] = {127, 532,
264, 13, 18, 5, 78}; int n = sizeof(array) /
sizeof(array[0]); radixsort(array, n);
printArray(array, n); return 0;
}
Program 13
Aim:
Program to sort an array of integers in ascending using merge
sort.
Code
48
#include <stdio.h> #include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{ int i, j, k; int n1 = m - l + 1;
int n2 = r - m; int L[n1], R[n2];
for (i = 0; i < n1; i++) L[i] = arr[l
+ i]; for (j = 0; j < n2; j++) R[j]
= arr[m + 1 + j]; i = 0; j = 0; k
= l;
while (i < n1 && j < n2)
{ if (L[i] <= R[j])
{ arr[k] = L[i];
i++;
} else
{ arr[k] =
R[j];
j++; }
k++;
} while (i <
n1) { arr[k]
= L[i]; i++;
k++;
} while (j <
n2) { arr[k]
= R[j]; j++;
k++;
}
} void mergeSort(int arr[], int l, int r)
{ if (l < r) { int m = l + (r -
l) / 2; mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
} void printArray(int A[], int size)
{ for (int i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
} int main() { int arr[] = { 22, 14, 63, 5,
16, 2 }; int arr_size = sizeof(arr) /
sizeof(arr[0]); printf("Given array is
\n"); printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size); return 0;
}
50
Program 14
Aim:
Program to sort an array of integers in ascending using quick sort.
Code
#include <stdio.h> void
swap(int *a, int *b)
{ int t = *a; *a = *b;
*b = t; } int partition(int array[], int
low, int high) { int pivot = array[high];
int i = (low - 1); for (int j = low; j <
high; j++) { if (array[j] <= pivot)
{ i++; swap(&array[i],
&array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
} void quickSort(int array[], int low, int high)
{ if (low < high) { int pi =
partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
} void printArray(int array[], int size)
{ for (int i = 0; i < size; ++i)
{ printf("%d ", array[i]);
}
printf("\n");
} int main() { int data[] = {7, 2, 4, 3, 8, 1,
5}; int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array:\n");
printArray(data, n); quickSort(data, 0, n -
1); printf("Sorted array in ascending
order:\n"); printArray(data, n); return 0;
}
Program 15
Aim :
Program to demonstrate the use of linear search to search a
given element in an array.
52
Code:
#include <stdio.h> int main() { int
array[100], search, c, n; printf("Enter number
of elements in array\n"); scanf("%d", &n);
printf("Enter %d integer(s)\n", n); for (c = 0;
c < n; c++) { scanf("%d", &array[c]);
}
printf("Enter a number to search\n"); scanf("%d",
&search); for (c = 0; c < n; c++) { if (array[c] ==
search) { printf("%d is present at location %d.\n",
search, c + 1); break;
} }
if (c == n)
{ printf("
%d isn't
present in the
array.\n",
search);
}
return 0;
}
Program 16
Aim :
Program to demonstrate the use of binary search to search a
given element in an array of ascending order.
54
Code:
#include <stdio.h> int binarySearch(int a[], int
beg, int end, int val) { int mid; if (end >=
beg) { mid = (beg + end) / 2; if (a[mid]
== val) { return mid + 1; // return 1-based
index
} else if (a[mid] > val)
{ return binarySearch(a, beg, mid - 1,
val);
} else { return binarySearch(a,
mid + 1, end, val);
} }
return -1;
} int
main()
{ int a[]
= {17, 24,
35, 40, 55,
61, 72, 87,
90}; //
given array
int val =
40; // value
to be
searched
int n =
sizeof(a) /
sizeof(a[0]
); // size of
array int
res =
binarySear
ch(a, 0, n -
1, val); //
Store result
printf("The
elements of
the array
are - ");
for (int i =
0; i < n;
i++)
printf("%d
", a[i]);
printf("\nEl
ement to
56
be
searched is
- %d", val);
if (res == -
1)
printf("\nEl
ement is
not present
in the
array");
else
printf("\nEl
ement is
present
at %d
position of
array",
res);
return 0;
}
58