DSA List of Experiments
DSA List of Experiments
Session :2024-25
Practical :1
Write a program to search an element from array using linear search.
Code:
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &search);
if(arr[i] == search) {
found = 1;
break;
if(!found) {
}
return 0;
Output:
Practical: 2
Write a program to search an element from array using Binary Search
Code:
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &search);
low = 0;
high = n - 1;
if(arr[mid] == search) {
return 0;
low = mid + 1;
} else {
high = mid - 1;
return 0;
Output:
Practical: 3
Write a program to implement Stack using array.
Code:
#include <stdio.h>
int stack[SIZE];
if(top == SIZE - 1) {
} else {
top++;
stack[top] = value;
void pop() {
if(top == -1) {
} else {
top--;
void peek() {
if(top == -1) {
printf("Stack is empty.\n");
} else {
printf("Top element is: %d\n", stack[top]);
void display() {
if(top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements:\n");
printf("%d\n", stack[i]);
int main() {
while(1) {
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
return 0;
Output:
Practical: 4
#include <stdio.h>
int queue[SIZE];
if (rear == SIZE - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
void dequeue() {
printf("Queue Underflow\n");
} else {
front++;
}
void peek() {
printf("Queue is empty\n");
} else {
void display() {
printf("Queue is empty\n");
} else {
printf("\n");
int main() {
while (1) {
printf("\nQueue Operations:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
}
Output:
Practical :5
Code:
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
} else {
queue[rear] = value;
void dequeue() {
if (front == -1) {
} else {
if (front == rear) {
front = rear = -1;
} else {
void peek() {
if (front == -1) {
printf("Queue is empty\n");
} else {
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
int i = front;
while (1) {
if (i == rear)
break;
i = (i + 1) % SIZE;
}
printf("\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
Output:
Practical: 6
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (!newNode) {
printf("Heap Overflow\n");
return;
newNode->data = value;
newNode->next = top;
top = newNode;
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
top = top->next;
free(temp);
void peek() {
if (top == NULL) {
printf("Stack is empty\n");
} else {
void display() {
if (top == NULL) {
printf("Stack is empty\n");
} else {
temp = temp->next;
}
printf("\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
Output:
Practical: 7
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (!newNode) {
return;
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
} else {
rear->next = newNode;
rear = newNode;
}
void dequeue() {
if (front == NULL) {
return;
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
void peek() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
void display() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
temp = temp->next;
printf("\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
}
Practical:8
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (!newNode) {
return;
newNode->data = value;
newNode->next = head;
head = newNode;
if (!newNode) {
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
if (!newNode) {
return;
newNode->data = value;
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
temp = temp->next;
if (temp == NULL) {
} else {
newNode->next = temp->next;
temp->next = newNode;
void deleteAtBeginning() {
if (head == NULL) {
printf("List is empty\n");
return;
head = head->next;
free(temp);
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (temp->next == NULL) {
head = NULL;
free(temp);
return;
temp = temp->next;
temp->next = NULL;
free(lastNode);
if (head == NULL) {
printf("List is empty\n");
return;
if (position == 1) {
head = head->next;
free(temp);
return;
}
temp = temp->next;
} else {
temp->next = temp->next->next;
free(nodeToDelete);
void display() {
if (head == NULL) {
printf("List is empty\n");
return;
printf("List: ");
temp = temp->next;
printf("\n");
}
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
scanf("%d", &value);
scanf("%d", &position);
insertAtPosition(value, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
scanf("%d", &position);
deleteAtPosition(position);
break;
case 7:
display();
break;
case 8:
return 0;
default:
printf("Invalid choice\n");
return 0;
}
Output:
Practical:9
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
return NULL;
newNode->data = data;
newNode->next = newNode;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
*head = newNode;
if (*head == NULL) {
printf("List is empty\n");
return;
if (*head == (*head)->next) {
free(*head);
*head = NULL;
} else {
last = last->next;
*head = (*head)->next;
last->next = *head;
free(temp);
if (*head == NULL) {
printf("List is empty\n");
return;
if (*head == (*head)->next) {
free(*head);
*head = NULL;
} else {
prev = temp;
temp = temp->next;
prev->next = *head;
free(temp);
if (head == NULL) {
printf("List is empty\n");
return;
do {
temp = temp->next;
printf("(head)\n");
}
int main() {
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
display(head);
insertBegin(&head, 5);
display(head);
deleteBegin(&head);
display(head);
deleteEnd(&head);
display(head);
return 0;
Output:
Practical:10
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (newNode == NULL) {
return NULL;
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
void deleteBegin(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
*head = NULL;
} else {
*head = (*head)->next;
(*head)->prev = NULL;
free(temp);
if (*head == NULL) {
printf("List is empty\n");
return;
*head = NULL;
free(temp);
} else {
temp = temp->next;
temp->prev->next = NULL;
free(temp);
if (head == NULL) {
printf("List is empty\n");
return;
temp = temp->next;
printf("NULL\n");
int main() {
insertEnd(&head, 10);
insertEnd(&head, 20);
insertEnd(&head, 30);
insertEnd(&head, 40);
display(head);
insertBegin(&head, 5);
display(head);
deleteBegin(&head);
display(head);
deleteEnd(&head);
display(head);
return 0;
}
Output:
Practical:10
Write a program to sort elements using Bubble Sort.
Code:
#include <stdio.h>
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
printf("\n");
int main() {
printArray(arr, n);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
Output:
Practical:12
Write a program to sort elements using Merge Sort.
Code:
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
k++;
arr[k] = leftArr[i];
i++;
k++;
arr[k] = rightArr[j];
j++;
k++;
printf("\n");
int main() {
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Output:
Practical:13
Write a program to sort elements using Heap Sort.
Code:
#include <stdio.h>
*a = *b;
*b = temp;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
printf("\n");
int main() {
printArray(arr, n);
heapSort(arr, n);
printArray(arr, n);
return 0;
Output:
Practical:14
Write a program to sort elements using Quick Sort.
Code:
#include <stdio.h>
*a = *b;
*b = temp;
i++;
swap(&arr[i], &arr[j]);
}
}
printf("\n");
int main() {
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Output:
Practical:15
. Write a program to sort elements using Selection Sort.
Code:
#include <stdio.h>
*a = *b;
*b = temp;
swap(&arr[minIndex], &arr[i]);
printf("\n");
int main() {
printArray(arr, n);
selectionSort(arr, n);
printArray(arr, n);
return
0;
Output: