Data Structures Lab
Data Structures Lab
Data Structures Lab
MUKTHAGANGOTHRI, MYSURU-06
// Initialize sum to 0
int sum = 0;
OUTPUT
Sum of elements in the array: 15
2. Write a C Program to implement an array and perform basic operations (insertion, deletion,
traversal).
#include <stdio.h>
int main()
{
// Declare an array
int myArray[10] = {1, 2, 3, 4, 5};
int arraySize = 5; // Initial size of the array
// Display the original array
displayArray(myArray, arraySize);
return 0;
}
OUTPUT
Array elements: 1 2 3 4 5
Array elements: 1 2 10 3 4 5
Array elements: 1 2 10 4 5
3. Write a C Program to implement Singly linked list
#include <stdio.h>
#include <stdlib.h>
temp->next = newNode;
}
// Function to delete a node with a given value from the singly linked list
void deleteNode(struct Node** head, int key)
{
if (*head == NULL)
{
printf("List is empty. Deletion failed.\n");
return;
}
int main()
{
struct Node* head = NULL;
int data, choice;
do {
// Display menu
printf("\nSingly Linked List Operations:\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Delete a node\n");
printf("4. Display the list\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
// Input: Get the data to insert at the beginning
printf("Enter the data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
// Input: Get the data to insert at the end
printf("Enter the data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
// Input: Get the data to delete
printf("Enter the data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
displayList(head);
break;
case 5:
printf("Quitting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
return 0;
}
OUTPUT
Singly Linked List Operations:
1. Insert at the beginning
2. Insert at the end
3. Delete a node
4. Display the list
5. Quit
Enter your choice: 1
Enter the data to insert at the beginning: 16
// 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("List is empty. Deletion failed.\n");
return;
}
int main()
{
struct Node* head = NULL;
int data, choice;
do {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Delete a node by value\n");
printf("4. Display the list\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 the value of the node to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
displayList(head);
break;
case 5:
printf("Quitting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int size, key;
int arr[size];
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; ++i)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &key);
return 0;
}
OUTPUT
Enter the size of the array: 5
Enter 5 elements:
23
45
67
89
34
Enter the number to search: 23
Number 23 found at index 0.
#define MAX_SIZE 5
int main()
{
// Create a stack
struct Stack myStack;
// Initialize the stack
initialize(&myStack);
do {
// Display menu
printf("\nStack Operations:\n");
printf("1. Push an element\n");
printf("2. Pop an element\n");
printf("3. Display the stack\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
// Input: Get the element to push
printf("Enter the element to push: ");
scanf("%d", &element);
push(&myStack, element);
break;
case 2:
pop(&myStack);
break;
case 3:
display(&myStack);
break;
case 4:
printf("Quitting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
}
while (choice != 4);
return 0;
}
OUTPUT
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 3
Stack is empty.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 1
Enter the element to push: 12
Pushed 12 onto the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 1
Enter the element to push: 13
Pushed 13 onto the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 1
Enter the element to push: 14
Pushed 14 onto the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 1
Enter the element to push: 15
Pushed 15 onto the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 1
Enter the element to push: 16
Pushed 16 onto the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice:
1
Enter the element to push: 17
Stack Overflow: Cannot push 17, stack is full.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 3
Stack elements: 12 13 14 15 16
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice: 2
Popped 16 from the stack.
Stack Operations:
1. Push an element
2. Pop an element
3. Display the stack
4. Quit
Enter your choice:
9. Write a C Program to implement Queue operations
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
return dequeuedValue;
}
int main()
{
// Create a queue using structure variable
struct Queue myQueue;
do {
// Display menu
printf("\nQueue Operations:\n");
printf("1. Enqueue an element\n");
printf("2. Dequeue an element\n");
printf("3. Display the queue\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
// Input: Get the element to enqueue
printf("Enter the element to enqueue: ");
scanf("%d", &element);
enqueue(&myQueue, element);
break;
case 2:
dequeue(&myQueue);
break;
case 3:
display(&myQueue);
break;
case 4:
printf("Quit the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
OUTPUT
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue: 12
Enqueued 12 into the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue: 13
Enqueued 13 into the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue: 14
Enqueued 14 into the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue: 15
Enqueued 15 into the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue: 16
Enqueued 16 into the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 3
Queue elements: 12 13 14 15 16
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 1
Enter the element to enqueue:
17
Queue Overflow: Cannot enqueue 17, queue is full.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 2
Dequeued 12 from the queue.
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice: 3
Queue elements: 13 14 15 16
Queue Operations:
1. Enqueue an element
2. Dequeue an element
3. Display the queue
4. Quit
Enter your choice:
10. Write a C Program to implement bubble sort algorithm
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[]=n;
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT
// Swap arr[i + 1] and arr[high] (put the pivot in its correct position)
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
int main()
{
int n;
int arr[n];
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT
Enter the number of elements: 5
Enter 5 elements:
12
67
10
28
121
Sorted Array: 10 12 28 67 121
12. Write a C Program to implement depth-first search (DFS) ) traversal on a graph
#include <stdio.h>
#define MAX_VERTICES 100
int main()
{
int vertices, edges;
graph[start][end] = 1;
// For undirected graph, uncomment the following line:
// graph[end][start] = 1;
}
return 0;
}
OUTPUT
#include <stdio.h>
#include <stdlib.h>
int main()
{
int vertices, edges;
return 0;
}
int main()
{
printf("Preorder:\n");
preorder(1);
printf("\nPostorder:\n");
postorder(1);
printf("\nInorder:\n");
inorder(1);
printf("\n");
return 0;
}
OUTPUT
Preorder:
D A E G Q B F R V T J L
Postorder:
G Q E B A V R J L T F D
Inorder:
G E Q A B D V R F J T L
15. write a C Program to find the height of a binary tree
#include <stdio.h>
#include <stdlib.h>
return root;
}
// Return the maximum height of the left and right subtrees, plus 1 for the current node
return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1);
}
int main() {
struct TreeNode* root = NULL;
int nodeValue;
char choice;
if (nodeValue != 0) {
root = insertNode(root, nodeValue);
}
} while (nodeValue != 0);
return 0;
}
OUTPUT
Input a value to insert into the binary tree (enter 0 to stop): 21
Input a value to insert into the binary tree (enter 0 to stop): 23
Input a value to insert into the binary tree (enter 0 to stop): 45
Input a value to insert into the binary tree (enter 0 to stop): 66
Input a value to insert into the binary tree (enter 0 to stop): 0
// Function to delete a node with a given value from the binary search tree
struct TreeNode* deleteNode(struct TreeNode* root, int value)
{
if (root == NULL)
{
return root; // Value not found, return as is
}
// Node with two children: Get the inorder successor (smallest in the right subtree)
struct TreeNode* temp = findMinValueNode(root->right);
return root;
}
int main()
{
struct TreeNode* root = NULL;
int nodeValue;
char choice;
// Insert nodes into the binary search tree
do {
printf("Input a value to insert into the binary search tree (enter 0 to stop): ");
scanf("%d", &nodeValue);
if (nodeValue != 0)
{
root = insertNode(root, nodeValue);
}
if (nodeValue != 0)
{
root = deleteNode(root, nodeValue);
printf("In-order Traversal after deleting %d: ", nodeValue);
inOrderTraversal(root);
printf("\n");
}
return 0;
}
OUTPUT
Input a value to insert into the binary search tree (enter 0 to stop): 75
Input a value to insert into the binary search tree (enter 0 to stop): 42
Input a value to insert into the binary search tree (enter 0 to stop): 35
Input a value to insert into the binary search tree (enter 0 to stop): 12
Input a value to insert into the binary search tree (enter 0 to stop): 10
Input a value to insert into the binary search tree (enter 0 to stop): 0