Shivank Practicals
Shivank Practicals
Shivank Practicals
Practical File
on
Data Structures
Branch – Computer Science / 3rd Semester
Session: 2023 - 2024
––––––
1
Table of Contents
S.no Content
1. Array Operations
2. Stack Operations
3. Queue Operations
7. Bubble Sort
8. Quick Sort
9. Insertion Sort
2
Array Operations
1. WAP to perform the following operations on Array.
a. Traversing
b. Insertion
c. Deletion
#include <stdio.h>
#define MAX_SIZE 100
arr[position] = element;
return size + 1;
}
int main() {
int arr[MAX_SIZE];
int size = 0;
int choice, element, position;
traverseArray(arr, size);
printf("Array Operations:\n");
printf("1. Traverse\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Exit\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
traverseArray(arr, size);
break;
case 2:
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position to insert: scanf("%d",
&position);
size = insertElement(arr, size, element, position);
break;
case 3:
printf("Enter the position to delete: ");
scanf("%d", &position);
size = deleteElement(arr, size, position);
break;
case 4:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
if (choice!=1 && choice!= 4){
traverseArray(arr, size);
}
return 0;
}
4
Output:
Array elements:
Array Operations:
1. Traverse
2. Insert
3. Delete
4. Exit
Enter your choice: 2
Enter the element to insert: 12
Enter the position to insert: 0
Array elements: 12
5
Stack Operations
2. WAP to perform the following operations on Stack.
a. Push
b. Pop
Program Source Code:
#include <stdio.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
int value = stack[top];
top--;
printf("Popped %d from the stack\n\n", value);
}
}
int main() {
int choice, value;
printf("Stack Operations\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Exit\n");
do {
switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
6
pop();
break;
case 3:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
} while (choice != 3);
return 0;
}
7
Output:
Stack Operations
1. Push
2. Pop
3. Exit
Enter your choice: 1
Enter the value to push: 1
Pushed 1 onto the stack
8
Queue Operations
3. WAP to perform the following operations on Queue.
a. Enqueue (Insertion)
b. Dequeue (Deletion)
Program Source Code:
#include <stdio.h>
#define MAX_SIZE 4
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
} else if (front == rear) {
int value = queue[front];
front = rear = -1;
printf("Dequeued %d from the queue\n\n", value);
} else if (front == MAX_SIZE - 1) {
int value = queue[front];
front = 0;
printf("Dequeued %d from the queue\n\n", value);
} else {
int value = queue[front];
front++;
printf("Dequeued %d from the queue\n\n", value);
}
}
9
int main() {
int choice, value;
printf("Queue Operations\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Exit\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
} while (choice != 3);
return 0;
}
10
Output:
Queue Operations
1. Enqueue
2. Dequeue
3. Exit
Enter your choice: 1
Enter the value to enqueue: 2
Enqueued 2 into the queue
11
Linked List Insertion Operation
4. WAP to perform the Insertion operation on Linked List.
a. At the Beginning
b. At the End
c. After a specified node (value)
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, value;
printf("Linked List Insertion Operations\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert after a specified node\n");
printf("4. Exit\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert at the beginning:
");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter the value to insert: ");
scanf("%d", &value);
printf("Enter the value of the node after which to
insert: ");
int prevValue;
scanf("%d", &prevValue);
struct Node* temp = head;
while (temp != NULL && temp->data != prevValue) {
temp = temp->next;
}
if (temp == NULL) {
13
printf("Node with value %d not found\n",
prevValue);
} else {
insertAfterNode(temp, value);
}
break;
case 4:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
if (choice!= 4){
printLinkedList(head);
}
} while (choice != 4);
return 0;
}
14
Output:
Linked List Insertion Operations
1. Insert at Beginning
2. Insert at End
3. Insert after a specified node
4. Exit
Enter your choice: 1
Enter the value to insert at the beginning: 23
Linked List Elements: 23
Enter your choice: 1
Enter the value to insert at the beginning: 34
Linked List Elements: 34 23
Enter your choice: 2
Enter the value to insert at the end: 45
Linked List Elements: 34 23 45
Enter your choice: 3
Enter the value to insert: 12
Enter the value of the node after which to insert: 18
Node with value 18 not found
Linked List Elements: 34 23 45
Enter your choice: 3
Enter the value to insert: 22
Enter the value of the node after which to insert: 23
Linked List Elements: 34 23 22 45
Enter your choice: 4
Bye
15
Linked List Deletion Operation
5. WAP to perform the Deletion operation on Linked List.
a. At the Beginning
b. At the End
c. After a specified node (value)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, value;
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
17
insertAtBeginning(&head, 40);
insertAtBeginning(&head, 40);
insertAtBeginning(&head, 40);
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
deleteAtBeginning(&head);
break;
case 2:
deleteAtEnd(&head);
break;
case 3:
printf("Enter the value of the node to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 4:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
if (choice != 4) {
printLinkedList(head);
}
} while (choice != 5);
return 0;
}
18
Output:
19
Searching
6. WAP to perform the Searching operation using following algorithms.
a. Linear Search
b. Binary Search
#include <stdio.h>
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = 10;
int choice, element;
20
traverseArray(arr, size);
printf("Array Operations:\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("3. Exit\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the key to search: "); scanf("%d",
&element);
int linearSearchResult = linearSearch(arr, size,
element);
if (linearSearchResult == -1) {
printf("Element not found.\n");
} else {
printf("Element found at index %d.\n",
linearSearchResult);
}
break;
case 2:
printf("Enter the key to search: "); scanf("%d",
&element);
int binarySearchResult = binarySearch(arr, size,
element);
if (binarySearchResult == -1) {
printf("Element not found.\n");
} else {
printf("Element found at index %d.\n",
binarySearchResult);
}
break;
case 3:
printf("Bye\n");
break;
default:
printf("That choice isn’t available\n");
}
return 0;
}
21
Output:
Array elements: 1 2 3 4 5 6 7 8 9 10
Array Operations:
1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 1
Enter the key to search: 5
Element found at index 4.
Enter your choice: 2
Enter the key to search: 9
Element found at index 8.
Enter your choice: 1
Enter the key to search: 11
Element not found.
Enter your choice: 2
Enter the key to search: 34
Element not found.
Enter your choice: 3
Bye
22
Sorting
7. WAP to perform sorting using the Bubble Sort Technique.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
bubbleSort(arr, n);
return 0;
}
23
Output:
24
8. WAP to perform sorting using the Quick Sort Technique.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
25
quickSort(arr, 0, n - 1);
return 0;
}
26
Output:
27
9. WAP to perform sorting using the Insertion Sort Technique.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
insertionSort(arr, n);
return 0;
}
28
Output:
29
10.WAP to perform sorting using the Selection Sort Technique.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
return 0;
}
30
Output:
31