Data Structure Lab Manual(2024-25)
Data Structure Lab Manual(2024-25)
#include <stdio.h>
void bubbleSort(int arr[], int size) {
int i, j, temp;
for (i = 0; i< size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] >arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main () {
int arr[20];
int i, n;
printf("Enter the number of items in the array: ");
scanf("%d", &n);
printf("Enter the data in the array:\n");
for (i = 0; i< n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("\nSorted Array:\n");
for (i = 0; i< n; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
Output:
Experiment No: 1.B
Aim: To implement Insertion Sorting.
Program:
#include<stdio.h>
#include<conio.h>
void insert(int [],int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array: ");
scanf("%d",&n);
printf("Enter the data in the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insert(a,n);
getch();
}
void insert(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=temp;
}
printf("Data After Insertion Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
Output:
Experiment No: 1.C
Aim: To implement Selection Sorting.
Program:
#include<stdio.h>
#include<conio.h>
void select(int [],int);
int min(int [],int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array: ");
scanf("%d",&n);
printf("Enter the data in the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
select(a,n);
getch();
}
void select(int a[],int n)
{
int i,loc,temp;
loc=0;
temp=0;
for(i=0;i<n;i++)
{
loc=min(a,i,n);
temp=a[loc];
a[loc]=a[i]; a[i]=temp;
}
printf("\nData After Selection Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int min(int a[],int lb,int ub)
{
int m=lb;
while(lb<ub)
{ if(a[lb]<a[m])
{
m=lb;
}
lb++;
}
return m;
}
Output:
Experiment No: 1.D
Aim: To implement Quick Sorting.
Program:
#include <stdio.h>
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i< n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
return 0;
}
Output:
Experiment No: 2.A
Aim: To implement Linear Search.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10], i, item, flag = 0;
clrscr();
printf("Enter the data in the array");
for (i = 0; i< 10; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the element to be searched");
scanf("%d", &item);
for (i = 0; i< 10; i++)
{
if (item == a[i])
{
flag = 1;
break;
}
}
if (flag == 0)
printf("Element Not Found");
else
printf("Element Found at Position =%d", i);
getch();
}
Output:
Experiment No: 2.B
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,mid,beg,i,end,item,loc=-1;
clrscr();
printf("Enter the number of elements to be entered: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&item);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2; if(item==a[mid])
{
loc=mid;
break;
}
else if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
}
if(loc==-1)
Output:
Experiment No: 3.A
Aim: To implement Stack using Array.
Program:
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
printf("\n\n*****MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value)
{
if (top == SIZE - 1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else
{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop()
{
if (top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
if (top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("\nStack elements are:\n");
for (i = top; i>= 0; i--)
printf("%d\n", stack[i]);
}
}
Output:
Experiment No: 3.B
Aim: To implement Stack using Linked List.
Program:
#include <stdio.h>
#include <conio.h>
struct Node
{
int data;
struct Node * next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\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: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
Void push(int value)
{
struct Node * newNode;
newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->data = value;
if (top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
Void pop()
{
if (top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
Void display()
{
if (top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while (temp->next != NULL)
{
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL", temp->data);
}
}
Output:
Experiment No: 3.C
Aim: To implement the conversion of Infix to Postfix expression.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char infix[100];
printf("Enter an infix expression: ");
scanf("%s", infix);
printf("Postfix expression: ");
infixToPostfix(infix);
printf("\n");
return 0;
}
Output:
Experiment No: 4.A
Aim: To implement the Queue using Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int data;
struct Node* next;
};
// Queue structure
struct Queue {
struct Node* front; // Pointer to the front of the queue
struct Node* rear; // Pointer to the rear of the queue
};
// If the queue is empty, set the new node as both front and rear
if (isEmpty(queue)) {
queue->front = queue->rear = newNode;
return;
}
// Add the new node at the end and update the rear pointer
queue->rear->next = newNode;
queue->rear = newNode;
// Store the front node and move the front pointer to the next node
struct Node* temp = queue->front;
queue->front = queue->front->next;
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Example usage with user input
int main() {
struct Queue* queue = createQueue();
int choice, data;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to enqueue: ");
scanf("%d", &data);
enqueue(queue, data);
break;
case 2:
printf("Dequeued element: %d\n", dequeue(queue));
break;
case 3:
display(queue);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Output:
Experiment No: 4.B
Aim: To implement the circular Queue using Array.
Program:
#include <stdio.h>
#define SIZE 5 /* Size of Circular Queue */
int CQ[SIZE], f = -1, r = -1; /* Global declarations */
deletedElem = CQ[f];
if (f == r) {
f = r = -1;
} else if (f == SIZE - 1) {
f = 0;
} else {
f++;
}
return deletedElem;
}
int main() { /* Main Program */
int opn, elem;
do {
printf("\n ### Circular Queue Operations ### \n\n");
printf(" Press 1-Insert, 2-Delete, 3-Exit\n");
return 0;
}
Output:
Experiment No: 4.C
Aim: To implement the Priority Queue using Linked List.
#include <stdio.h>
#include <stdlib.h>
// Function to remove and return the element with the highest priority
int dequeue(struct Node** head) {
if (*head == NULL) {
printf("Priority queue is empty.\n");
exit(1);
}
int main() {
struct Node* pq = NULL;
int choice, data, priority;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data and priority to enqueue: ");
scanf("%d %d", &data, &priority);
enqueue(&pq, data, priority);
display(pq);
break;
case 2:
if (!isEmpty(pq)) {
printf("Dequeued element: %d\n", dequeue(&pq));
display(pq);
} else {
printf("Priority queue is empty.\n");
}
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Experiment No: 5.A
Aim: To implement the Singly Linked List.
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a single linked list node
struct Node {
int data;
struct Node* next;
};
if (*head == NULL) {
*head = newNode;
return;
}
if (position == 0) {
insertAtBeginning(head, data);
return;
}
if (current == NULL) {
printf("Invalid position. Position exceeds the length of the list.\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
if (position < 0) {
printf("Invalid position. Position must be non-negative.\n");
return;
}
if (position == 0) {
deleteAtBeginning(head);
return;
}
int currentPosition = 0;
while (current != NULL &¤tPosition< position) {
prev = current;
current = current->next;
currentPosition++;
}
if (current == NULL) {
printf("Invalid position. Position exceeds the length of the list.\n");
return;
}
prev->next = current->next;
free(current);
}
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Insert at a specific position\n");
printf("4. Delete at the beginning\n");
printf("5. Delete at the end\n");
printf("6. Delete at a specific position\n");
printf("7. Display\n");
printf("8. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position to insert at: ");
scanf("%d", &position);
insertAtPosition(&head, data, position);
break;
case 4:
deleteAtBeginning(&head);
break;
case 5:
deleteAtEnd(&head);
break;
case 6:
printf("Enter position to delete at: ");
scanf("%d", &position);
deleteAtPosition(&head, position);
break;
case 7:
display(head);
break;
case 8:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Experiment No: 5.B
Aim: To implement the Circular Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
struct Node* current = *head;
while (current->next != *head) {
current = current->next;
}
current->next = newNode;
newNode->next = *head;
*head = newNode;
}
}
// Function to delete a node with a given value from the circular list
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("The circular list is empty.\n");
return;
}
do {
if (current->data == key) {
if (prev == NULL) {
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
last->next = current->next;
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("Node with data %d deleted from the circular list.\n", key);
return;
}
prev = current;
current = current->next;
} while (current != *head);
int main() {
struct Node* head = NULL;
int choice, data, key;
while (1) {
printf("1. Insert at the beginning\n");
printf("2. Delete a node\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &key);
deleteNode(&head, key);
break;
case 3:
printf("Circular Linked List:\n");
display(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Experiment No: 5.C
Aim: To implement the Doubly Linked List.
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a doubly linked list node
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to insert a new node at the beginning of the doubly linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
if (*head == NULL) {
newNode->next = NULL;
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
// Function to insert a new node at the end of the doubly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// Function to delete a node with a given value from the doubly linked list
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("The doubly linked list is empty.\n");
return;
}
struct Node* current = *head;
while (current != NULL) {
if (current->data == key) {
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
printf("Node with data %d deleted from the doubly linked list.\n", key);
return;
}
current = current->next;
}
printf("Node with data %d not found in the doubly linked list.\n", key);
}
while (1) {
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Delete a node\n");
printf("4. Display\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to delete: ");
scanf("%d", &key);
deleteNode(&head, key);
break;
case 4:
printf("Doubly Linked List:\n");
display(head);
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Experiment No: 5.D
Aim: To implement the Doubly Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
if (*poly == NULL) {
*poly = newTerm;
} else {
struct Term* current = *poly;
struct Term* prev = NULL;
if (poly1 != NULL) {
coef1 = poly1->coefficient;
exp1 = poly1->exponent;
poly1 = poly1->next;
}
if (poly2 != NULL) {
coef2 = poly2->coefficient;
exp2 = poly2->exponent;
poly2 = poly2->next;
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
struct Term* result = NULL;
// Free memory
struct Term* temp;
while (poly1 != NULL) {
temp = poly1;
poly1 = poly1->next;
free(temp);
}
while (poly2 != NULL) {
temp = poly2;
poly2 = poly2->next;
free(temp);
}
while (result != NULL) {
temp = result;
result = result->next;
free(temp);
}
return 0;
}
Output:
Experiment No: 6.A
Aim: To implement Binary search tree using Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
return root;
}
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("In-order traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
Experiment No: 6.B
Aim: To implement recursive traversal: Preorder, Postorder, Inorder.
Program:
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
if (data == -1) {
return NULL;
}
return root;
}
int main() {
struct Node* root = NULL;
printf("Enter the elements of the binary tree:\n");
root = buildTree();
printf("Binary Tree Traversals:\n");
printf("Pre-order traversal: ");
preOrder(root);
printf("\n");
printf("In-order traversal: ");
inOrder(root);
printf("\n");
printf("Post-order traversal: ");
postOrder(root);
printf("\n");
Program:
#include<stdio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
main() {
int n,i,a[50];
system("clear");
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nThe Sorted Elements Are:\n");
for (i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
void heapsort(int a[],int n) {
int i,t;
heapify(a,n);
for (i=n-1;i>0;i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}
void heapify(int a[],int n) {
int k,i,j,item;
for (k=1;k<n;k++) {
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) {
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}
void adjust(int a[],int n) {
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1) {
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i]) {
a[j] = a[i];
j = i;
i = 2*j+1;
} else
break;
}
a[j] = item;
}
Output:
Experiment No: 7.A
Program:
#include <stdio.h>
#define MAX_NODES 100
// Define the graph structure
struct Graph {
int vertices;
int adjacencyMatrix[MAX_NODES][MAX_NODES];
};
Output:
Experiment No: 7.B
Aim: To implement BFS using Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int vertices;
struct Node** adjList;
};
while (!isEmpty(queue)) {
startVertex = dequeue(&queue);
printf("%d ", startVertex);
int main() {
int vertices, edges, src, dest;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int startVertex;
printf("Enter the starting vertex for BFS: ");
scanf("%d", &startVertex);
bfs(graph, startVertex);
return 0;
}
Output:
Experiment No: 7.C
Aim: To implement any minimum spanning tree algorithm.
Program:
#include <stdio.h>
#include <stdbool.h>
#define V 5 // Number of vertices in the graph
int minKey(int key[], bool mstSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (!mstSet[v] && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
void printMST(int parent[], int graph[V][V]) {
printf("Edge Weight\n");
for (int i = 1; i< V; i++) {
printf("%d - %d %d\n", parent[i], i, graph[i][parent[i]]);
}
}
void primMST(int graph[V][V]) {
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i< V; i++) {
key[i] = INT_MAX;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printMST(parent, graph);
}
int main() {
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
printf("Minimum Spanning Tree using Prim's algorithm:\n");
primMST(graph);
return 0;
}
Output:
Content Beyond Syllabus
Experiment No: 1
Aim: To implement count sort.
Program:
#include <stdio.h>
#include <stdlib.h>
free(count);
free(output);
}
int main() {
int arr[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
countingSort(arr, n);
printf("\n");
return 0;
}
Output: