Downloadfile
Downloadfile
Downloadfile
LAB MANUAL
Data Structures (629401)
2023-2025
PREPARED BY
(235490694084)
Q1. Create a Structure with following Data Members:
1. Integer Array
2. Size of the Array Sort the Array using various Sorting algorithms such as
(i) Selection Sort (ii) Bubble Sort (iii) Two-way Merge Sort (iv) Quick Sort
(v) Heap Sort. And store the sorted Array in a text file.
Code :
#include <stdio.h>
#include <stdlib.h>
// Structure definition
struct Array {
int *arr;
int size;
};
// Function prototypes
void selectionSort(int arr[], int n);
void bubbleSort(int arr[], int n);
void mergeSort(int arr[], int l, int r);
void merge(int arr[], int l, int m, int r);
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void heapSort(int arr[], int n);
void heapify(int arr[], int n, int i);
void swap(int *a, int *b);
void printArray(int arr[], int n);
void storeSortedArrayToFile(int arr[], int n);
int main() {
struct Array arrStruct;
int n;
arrStruct.size = n;
arrStruct.arr = (int *)malloc(n * sizeof(int));
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arrStruct.arr[i]);
}
free(arrStruct.arr);
return 0;
}
// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
swap(&arr[i], &arr[min_index]);
}
}
// Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
// Merge Sort
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);
}
}
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++;
}
// Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
// Heap Sort
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
// Function prototypes
int linearSearch(struct Array arrStruct, int key);
int binarySearch(struct Array arrStruct, int key);
void displayResultsToFile(int result, char *searchType);
int main() {
struct Array arrStruct;
int n, key, linearResult, binaryResult;
arrStruct.size = n;
arrStruct.arr = (int *)malloc(n * sizeof(int));
free(arrStruct.arr);
return 0;
}
Q 7 . Create a structure with the following Data members: 1. Integer Array 2. Size of the
Array Search an element in a given list using Binary Search by recursion. And Display
result in a file.
Code:
#include<stdio.h>
struct array{
int arr[100],n;
}array1;
int binary(int,int);
void save(int);
int i,num;
void sort(){
int temp,j;
for(i=0;i<array1.n;i++){
for(j=i+1;j<array1.n;j++){
if(array1.arr[i]>array1.arr[j]){
temp=array1.arr[i];
array1.arr[i]=array1.arr[j];
array1.arr[j]=temp;
}
}
}
}
void main()
{
int loc;
//clrscr();
printf("\n Enter the size of Array[MAX=100]:");
scanf("%d",&array1.n);
printf("\n Enter the Elements:\n");
for(i=0;i<array1.n;i++){
scanf("%d",&array1.arr[i]);
}
printf("\n\t ARRAY SEARCHING");
printf("\n\t--------------------------------");
printf("\n Enter the Number to be searched:");
scanf("%d",&num);
sort();
printf("After Sorting Array... \n");
for(i=0;i<array1.n;i++){
printf("%d \n",array1.arr[i]);
}
loc=binary(0,array1.n);
if(loc!=0){
printf("\nElement Found At :- %d\n",loc);
save(loc);
}
else{
printf("\nElement not found...\n");
save(0);
}
}
int binary(f,l){
int m;
m = (f+l)/2;
if (l >= f)
{
if (array1.arr[m] == num) {
return m+1;
}
else if (array1.arr[m] < num){
f = m + 1;
binary(f,l);
}
else if (array1.arr[m] > num){
l = m - 1;
binary(f,l);
}
}
else{
return 0;
}
}
void save(num){
FILE *fptr;
fptr = fopen("binary_search_rec.txt","w");
if(fptr == NULL)
{
printf("Error in file!");
exit(0);
}
if(num==0){
fprintf(fptr,"\n Element found not Found");
}
else{
fprintf(fptr,"\n Element found at position: %d",num);
}
fclose(fptr);
}
Output:
Q 8 . Create a “Queue” structure with following Data members: 1. Integer Array 2. Size of
the Array Perform the following operations on Simple queue using user-defined
functions: 1. Insert an element 2. Remove an element 3. Display 4. Isfull 5. Isempty Create
a file which stores all values of Array.
Code:
#include<stdio.h>
struct queue{
int q[100],n;
}struct1;
int choice,start=-1,rear=-1,x,i;
void insert();
void rem();
void display();
void save();
int main()
{
//clrscr();
start=-1;
printf("\n Enter the size of Queue[MAX=100]:");
scanf("%d",&struct1.n);
do
{
printf("\n\t Queue OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.Insert\n\t 2.Remove\n\t 3.Display\n\t 4.Save in file\n\t 5.EXIT");
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
insert();
break;
}
case 2:
{
rem();
break;
}
case 3:
{
display();
break;
}
case 4:
{
save();
break;
}
case 5:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4/5)");
}
}
}
while(choice!=5);
return 0;
}
void insert()
{
if(rear==struct1.n-1)
{
printf("\n\tQueue is full");
}
else
{
printf(" Enter a value to be inserted:");
scanf("%d",&x);
rear++;
struct1.q[rear]=x;
if(start==-1){
start=0;
}
}
}
void rem()
{
if(start==-1)
{
printf("\n\t Queue is empty");
}
else
{
printf("\n\t The remd element is %d",struct1.q[start]);
if(start==rear){
start=-1;
rear=-1;
}
else{
start++;
}
}
}
void display()
{
if(start>=0)
{
printf("\n The elements in queue \n");
for(i=start;i<=rear;i++){
printf("\n%d",struct1.q[i]);
}
printf("\n Press Next Choice");
}
else
{
printf("\n The queue is empty");
}
}
void save()
{
if(start>=0)
{
FILE *fptr;
fptr = fopen("queue.txt","w");
if(fptr == NULL)
{
printf("Error in file!");
exit(0);
}
for(i=start;i<=rear;i++)
{
fprintf(fptr,"\n%d",struct1.q[i]);
}
printf("\n File is saved");
fclose(fptr);
}
else
{
printf("\n The queue is empty");
}
}
Output
:
Q 9 . Create a “Queue” user-defined structure with the following data members:
1. A Data , 2. A link to the next node Perform the following operations on Simple queue
using user-defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
// Queue structure
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
int main() {
Queue* queue = initializeQueue();
int choice, element;
do {
printf("\n1. Insert an element\n");
printf("2. Remove an element\n");
printf("3. Display\n");
printf("4. Store values in a file\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
enqueue(queue, element);
break;
case 2:
if (!isEmpty(queue))
printf("Removed element: %d\n", dequeue(queue));
break;
case 3:
printf("Queue: ");
display(queue);
break;
case 4:
storeToFile(queue, "queue_values.txt");
printf("Queue values stored in 'queue_values.txt'\n");
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
return 0;
}
Output :
Q 10. Create a “Circular Queue” structure with following Data members:
1. Integer Array
2. Size of the Array Perform the following operations on Circular queue using user-
defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of Array.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int size, choice, element;
do {
printf("\n1. Insert an element\n");
printf("2. Remove an element\n");
printf("3. Display\n");
printf("4. Store values in a file\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
enqueue(cq, element);
break;
case 2:
if (!isEmpty(cq))
printf("Removed element: %d\n", dequeue(cq));
break;
case 3:
printf("Queue: ");
display(cq);
break;
case 4:
storeToFile(cq, "circular_queue_values.txt");
printf("Queue values stored in 'circular_queue_values.txt'\n");
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
free(cq->array);
free(cq);
return 0;
}
Output :
Q 11. Create a “Circular Queue” user-defined structure with the following data members:
1. A Data 2. A link to the next node Perform the following operations on Circular queue
using user-defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
int main() {
int capacity, choice, element;
do {
printf("\n1. Insert an element\n");
printf("2. Remove an element\n");
printf("3. Display\n");
printf("4. Store values in a file\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
if (!isFull(cq)) {
printf("Enter element to insert: ");
scanf("%d", &element);
enqueue(cq, element);
} else {
printf("Queue is full. Cannot enqueue.\n");
}
break;
case 2:
if (!isEmpty(cq))
printf("Removed element: %d\n", dequeue(cq));
else
printf("Queue is empty. Cannot dequeue.\n");
break;
case 3:
printf("Queue: ");
display(cq);
break;
case 4:
storeToFile(cq, "circular_queue_values.txt");
printf("Queue values stored in 'circular_queue_values.txt'\n");
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
// Free memory
Node* current = cq->rear;
if (current != NULL) {
Node* temp = current->next;
while (temp != current) {
Node* toDelete = temp;
temp = temp->next;
free(toDelete);
}
free(current);
}
free(cq);
return 0;
}
Output :
Q 12. Create a user-defined “Linked List” structure with the following data members:
1. A Co-efficient
2. An Exponent
3. A link to the next node Perform the following operations on Singly list using user-
defined functions: 1. Create 2. Display 3. Addition 4. Multiplication Create a file which
stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int coefficient;
int exponent;
struct Node* next;
} Node;
return result;
}
return result;
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
Node* result = NULL;
int coefficient, exponent;
int num_terms1, num_terms2;
// Addition
result = addPolynomials(poly1, poly2);
printf("Addition result: ");
display(result);
// Multiplication
result = multiplyPolynomials(poly1, poly2);
printf("Multiplication result: ");
display(result);
return 0;
}
Output :
Q13. Create a user-defined structure with the following data members: 1. A Data 2. A link
to the next node Perform the following operations on list using user-defined functions:
1. Create a list
2. Traverse the whole list
3. Delete first node
4. Delete last node
5. Delete a node before specified data
6. Insert at first position
7. Insert at last position
8. Insert a node before specified data
9. Insert a node at specified position
10. Count
11. Copy
12. Merge two list
13. Reverse
14. Search
15. Sort Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
int main() {
Node* head = NULL;
// Test operations
insertAtLast(&head, 1);
insertAtLast(&head, 2);
insertAtLast(&head, 3);
// Perform operations
printf("\nAfter operations:\n");
// Count nodes
printf("Number of nodes: %d\n", countNodes(head));
// Copy list
Node* copiedList = copyList(head);
printf("Copied list: ");
traverse(copiedList);
// Merge lists
Node* list1 = NULL;
insertAtLast(&list1, 2);
insertAtLast(&list1, 4);
insertAtLast(&list1, 6);
Node* list2 = NULL;
insertAtLast(&list2, 1);
insertAtLast(&list2, 3);
insertAtLast(&list2, 5);
Node* mergedList = mergeLists(list1, list2);
printf("Merged list: ");
traverse(mergedList);
// Reverse list
head = reverseList(head);
printf("Reversed list: ");
traverse(head);
// Search value
int target = 4;
int position = search(head, target);
if (position != -1) {
printf("%d found at position %d.\n", target, position);
} else {
printf("%d not found in the list.\n", target);
}
// Sort list
head = sortList(head);
printf("Sorted list: ");
traverse(head);
Q14 . Create a user-defined structure with the following data members: 1. A Data 2. A link
to the next node Perform the following operations on Circular list using user-defined
functions:
1. Create a list
2. Traverse the whole list\
3. Delete first node
4. Delete last node
5. Delete a node before specified data
6. Insert at first position
7. Insert at last position
8. Insert a node before specified data
9. Insert a node at specified position
10. Count
11. Copy
12. Merge two list
13. Reverse
14. Search
15. Sort Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
int main() {
Node* head = NULL;
// Test operations
insertAtLast(&head, 1);
insertAtLast(&head, 2);
insertAtLast(&head, 3);
// Perform operations
printf("\nAfter operations:\n");
// Count nodes
printf("Number of nodes: %d\n", countNodes(head));
// Copy list
Node* copiedList = copyList(head);
printf("Copied circular list: ");
traverse(copiedList);
// Merge lists
Node* list1 = NULL;
insertAtLast(&list1, 2);
insertAtLast(&list1, 4);
insertAtLast(&list1, 6);
Node* list2 = NULL;
insertAtLast(&list2, 1);
insertAtLast(&list2, 3);
insertAtLast(&list2, 5);
Node* mergedList = mergeLists(list1, list2);
printf("Merged circular list: ");
traverse(mergedList);
// Reverse list
head = reverseList(head);
printf("Reversed circular list: ");
traverse(head);
// Search value
int target = 4;
int position = search(head, target);
if (position != -1) {
printf("%d found at position %d.\n", target, position);
} else {
printf("%d not found in the list.\n", target);
}
// Sort list
head = sortList(head);
printf("Sorted circular list: ");
traverse(head);
// Store list values in a file
storeListToFile(head, "circular_list_values.txt");
// Delete entire list
deleteList(&head);
return 0;
}
Output:
Q15. Create a user-defined structure with the following data members: 1. A Data 2. A link
to the next node 3. A link to the previous node Perform the following operations on the
doubly-linked list using user-defined functions: 1. Create a list 2. Traverse the whole list\
3. Delete first node 4. Delete last node 5. Delete a node before specified data 6. Insert at
first position 7. Insert at last position 8. Insert a node before specified data 9. Insert a
node at specified position 10. Count 11. Copy 12. Merge two list 13. Reverse 14. Search
15. Sort Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
// Perform operations
printf("\nAfter operations:\n");
Q16. Create a user-defined structure with the following data members: 1. A Data 2. A link
to the next node 3. A link to the previous node Perform the following operations on
doubly-linked Circular list using user defined functions: 1. Create a list 2. Traverse the
whole list\ 3. Delete first node 4. Delete last node 5. Delete a node before specified data 6.
Insert at first position 7. Insert at last position 8. Insert a node before specified data 9.
Insert a node at specified position 10. Count 11. Copy 12. Merge two list 13. Reverse 14.
Search 15. Sort Create a file which stores all values of list.
Code:
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
int main() {
Node* head = NULL;
// Test operations
insertAtLast(&head, 1);
insertAtLast(&head, 2);
insertAtLast(&head, 3);
// Perform operations
printf("\nAfter operations:\n");
// Count nodes
printf("Number of nodes: %d\n", countNodes(head));
// Copy list
Node* copiedList = copyList(head);
printf("Copied doubly linked circular list: ");
traverse(copiedList);
// Merge lists
Node* list1 = NULL;
insertAtLast(&list1, 2);
insertAtLast(&list1, 4);
insertAtLast(&list1, 6);
Node* list2 = NULL;
insertAtLast(&list2, 1);
insertAtLast(&list2, 3);
insertAtLast(&list2, 5);
Node* mergedList = mergeLists(list1, list2);
printf("Merged doubly linked circular list: ");
traverse(mergedList);
// Reverse list
head = reverseList(head);
printf("Reversed doubly linked circular list: ");
traverse(head);
// Search value
int target = 4;
int position = search(head, target);
if (position != -1) {
printf("%d found at position %d.\n", target, position);
} else {
printf("%d not found in the list.\n", target);
}
// Sort list
head = sortList(head);
printf("Sorted doubly linked circular list: ");
traverse(head);
// Store list values in a file
storeListToFile(head, "doubly_linked_circular_list_values.txt");
// Delete entire list
deleteList(&head);
return 0;
}
Output:
Q17. Write a program to represent an undirected graph using the adjacency matrix to
implement the graph and perform following operations, with menu driven options for
following tasks: 1. Create graph 2. Insert an edge 3. Print Adjacency Matrix 4. List all
vertices that are adjacent to a specified vertex. 5. Print out vertices using depth first
search 6. Print out vertices using breadth first search 7. Exit program
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices = 0;
// Function to create a graph
void createGraph() {
printf("Enter the number of vertices in the graph: ");
scanf("%d", &numVertices);
if (numVertices <= 0 || numVertices > MAX_VERTICES) {
printf("Invalid number of vertices.\n");
return;
}
printf("Graph with %d vertices has been created.\n", numVertices);
// Initialize adjacency matrix with zeros
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
adjacencyMatrix[i][j] = 0;
}
}
}
// Function to insert an edge between two vertices
void insertEdge() {
int vertex1, vertex2;
printf("Enter the vertices to connect (separated by space): ");
scanf("%d %d", &vertex1, &vertex2);
if (vertex1 < 0 || vertex1 >= numVertices || vertex2 < 0 || vertex2 >= numVertices) {
printf("Invalid vertices.\n");
return;
}
adjacencyMatrix[vertex1][vertex2] = 1;
adjacencyMatrix[vertex2][vertex1] = 1;
printf("Edge between vertices %d and %d has been inserted.\n", vertex1, vertex2);
}
// Function to print the adjacency matrix
void printAdjacencyMatrix() {
printf("Adjacency Matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
printf("%d ", adjacencyMatrix[i][j]);
}
printf("\n");
}
}
// Function to list all vertices adjacent to a specified vertex
void listAdjacentVertices() {
int vertex;
printf("Enter the vertex to list adjacent vertices: ");
scanf("%d", &vertex);
if (vertex < 0 || vertex >= numVertices) {
printf("Invalid vertex.\n");
return;
}
printf("Vertices adjacent to vertex %d: ", vertex);
for (int i = 0; i < numVertices; i++) {
if (adjacencyMatrix[vertex][i] == 1) {
printf("%d ", i);
}
}
printf("\n");
}
// Depth First Search
void DFSUtil(int v, int visited[]) {
visited[v] = 1;
printf("%d ", v);
for (int i = 0; i < numVertices; i++) {
if (adjacencyMatrix[v][i] == 1 && !visited[i]) {
DFSUtil(i, visited);
}
}
}
void DFS() {
int visited[MAX_VERTICES] = {0};
printf("Depth First Search: ");
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
DFSUtil(i, visited);
}
}
printf("\n");
}
// Breadth First Search
void BFS(int start) {
int visited[MAX_VERTICES] = {0};
int queue[MAX_VERTICES], front = -1, rear = -1;
printf("Breadth First Search: ");
visited[start] = 1;
queue[++rear] = start;
while (front != rear) {
int current = queue[++front];
printf("%d ", current);
for (int i = 0; i < numVertices; i++) {
if (adjacencyMatrix[current][i] == 1 && !visited[i]) {
visited[i] = 1;
queue[++rear] = i;
}
}
}
printf("\n");
}
// Function to display menu
void displayMenu() {
printf("\nMenu:\n");
printf("1. Create graph\n");
printf("2. Insert an edge\n");
printf("3. Print Adjacency Matrix\n");
printf("4. List all vertices that are adjacent to a specified vertex\n");
printf("5. Print out vertices using depth first search\n");
printf("6. Print out vertices using breadth first search\n");
printf("7. Exit program\n");
}
int main() {
int choice;
do {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createGraph();
break;
case 2:
insertEdge();
break;
case 3:
printAdjacencyMatrix();
break;
case 4:
listAdjacentVertices();
break;
case 5:
DFS();
break;
case 6:
printf("Enter the starting vertex for Breadth First Search: ");
int start;
scanf("%d", &start);
BFS(start);
break;
case 7:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a number from 1 to 7.\n");
}
} while (choice != 7);
return 0;
}
Output:
Q18. Create a user-defined structure with the following data members: 1. A Data 2. A link
to the Left child 3. A link to the Right child Perform the following operations on Binary
Search Tree using recursion: 1. Create 2. Traverse (Inorder, Preorder, Postorder) 3. Insert
4. Delete 5. Search 6. Create a file which stores all values of traversal.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = NULL;
// Close file
fclose(file);
return 0;
}
Output: