Rescued document 2
Rescued document 2
SESSION: 2023-2027
AMITY SCHOOL OF ENGINEERING &
TECHNOLOGY
DATA STRUCTURE USING C
[CSIT124]
PRACTICAL FILE
Submitted by Submitted to
NAME: - KUSHAGRA KAPOOR Ms. NEHA
TYAGI
ENROLLMENT NUMBER: - A411119823002
BRANCH: - BTECH AI (3rd Semester)
SECTION: - SECTION E
INDEX
Write a program to insert a new element in the given unsorted array at kth position.
Write a program to delete an element from a given sorted array.
Write a program to merge two given sorted arrays.
Write a program to implement Stack using array, also show overflow and
underflow in respective push and pop operations.
Write a program to implement Queue using array, which shows insertion and
deletion operations.
Write a program to implement Circular Queue using an array, which shows
insertion and deletion operations.
Write a program to implement Linear Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
Write a program to implement Stack, using Linked List. Implement Push, Pop and
display operations.
Write a program to implement Queue, using Linked List. Implement Insertion,
deletion and display operations.
Write a program to count the number of times an item is present in a linked list.
Write a program to increment the data part of every node present in a linked list by
10. Display the data both before incrimination and after.
Write a program to implement Doubly Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
Write a program to search for an element using Linear Search.
Write a program to search for an element using Binary Search.
Write a program to sort the given array using Bubble Sort.
Question 1: - Write a program to insert a new element in the given unsorted array at kth
position.
Code: -
#include <stdio.h>
void insertElement(int arr[], int n, int k, int element) {
// Move elements to the right from the k-th position
for (int i = n; i > k; i--) {
arr[i] = arr[i - 1];
}
// Insert the new element at the k-th position
arr[k] = element;
}
int main() {
int n, k, element;
// Input: size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n+1]; // Declare an array of size n+1 to hold the new element
// Input: elements of the array
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input: position (k) and the element to be inserted
printf("Enter the position (index) where the element needs to be inserted: ");
scanf("%d", &k);
// Ensure valid position
if (k < 0 || k > n) {
printf("Invalid position! Please enter a position between 0 and %d.\n", n);
return 1;
}
printf("Enter the element to be inserted: ");
scanf("%d", &element);
// Call function to insert the element
insertElement(arr, n, k, element);
// Output: array after insertion
printf("Array after insertion: ");
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT: -
Enter the number of elements in the array: 5
Enter the elements of the array: 1 2 3 4 5
Enter the position (index) where the element needs to be inserted: 2
Enter the element to be inserted: 6
Array after insertion: 1 2 6 3 4 5
Code: -
#include <stdio.h>
void deleteElement(int arr[], int *n, int element) {
int pos = -1;
// Find the position of the element to delete
for (int i = 0; i < *n; i++) {
if (arr[i] == element) {
pos = i;
break;
}
}
// If element not found
if (pos == -1) {
printf("Element not found in the array.\n");
return;
}
// Shift elements to the left to fill the gap
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
// Decrease the size of the array
(*n)--;
}
int main() {
int n, element;
// Input: size of the array
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);
Output: -
Enter the number of elements in the sorted array: 5
Enter the elements of the sorted array: 10 20 30 40 50
Enter the element to be deleted: 20
Array after deletion: 10 30 40 50
Code: -
#include <stdio.h>
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int mergedArr[]) {
int i = 0, j = 0, k = 0;
int main() {
int n1, n2;
return 0;
}
Output: -
Enter the number of elements in the first sorted array: 5
Enter the elements of the first sorted array: 1 3 5 7 9
Enter the number of elements in the second sorted array: 5
Enter the elements of the second sorted array: 2 4 6 8 10
Merged sorted array: 1 2 3 4 5 6 7 8 9 10
Question 4: - Write a program to implement Stack using array, also show overflow and
underflow in respective push and pop operations.
Code: -
#include <stdio.h>
#define MAX 5
// Push operation
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
stack[++top] = value;
printf("Pushed %d into the stack.\n", value);
}
}
// Pop operation
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
printf("Popped %d from the stack.\n", stack[top--]);
}
}
// Display stack
void display() {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Stack elements are: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
push(60); // Overflow case
display();
pop();
pop();
pop();
pop();
pop();
pop(); // Underflow case
return 0;
}
Output: -
Pushed 10 into the stack.
Pushed 20 into the stack.
Pushed 30 into the stack.
Pushed 40 into the stack.
Pushed 50 into the stack.
Stack Overflow!
Stack elements are: 50 40 30 20 10
Popped 50 from the stack.
Popped 40 from the stack.
Popped 30 from the stack.
Popped 20 from the stack.
Popped 10 from the stack.
Stack Underflow!
Question 5: - Write a program to implement Queue using array, which shows insertion and
deletion operations.
Code: -
#include <stdio.h>
#define MAX 5
// Insertion (Enqueue)
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow!\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Inserted %d into the queue.\n", value);
}
}
// Deletion (Dequeue)
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow!\n");
} else {
printf("Deleted %d from the queue.\n", queue[front++]);
}
}
// Display Queue
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty!\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60); // Overflow case
display();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue(); // Underflow case
return 0;
}
Output: -
Inserted 10 into the queue.
Inserted 20 into the queue.
Inserted 30 into the queue.
Inserted 40 into the queue.
Inserted 50 into the queue.
Queue Overflow!
Queue elements are: 10 20 30 40 50
Deleted 10 from the queue.
Deleted 20 from the queue.
Deleted 30 from the queue.
Deleted 40 from the queue.
Deleted 50 from the queue.
Queue Underflow!
Question 6: - Write a program to implement Circular Queue using an array, which shows
insertion and deletion operations.
Code: -
#include <stdio.h>
#define MAX 5
// Insertion (Enqueue)
void enqueue(int value) {
if ((front == 0 && rear == MAX - 1) || (rear == (front - 1) % (MAX - 1))) {
printf("Circular Queue Overflow!\n");
} else {
if (front == -1) front = rear = 0;
else if (rear == MAX - 1 && front != 0) rear = 0;
else rear++;
circularQueue[rear] = value;
printf("Inserted %d into the circular queue.\n", value);
}
}
// Deletion (Dequeue)
void dequeue() {
if (front == -1) {
printf("Circular Queue Underflow!\n");
} else {
printf("Deleted %d from the circular queue.\n", circularQueue[front]);
if (front == rear) front = rear = -1;
else if (front == MAX - 1) front = 0;
else front++;
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60); // Overflow case
display();
dequeue();
dequeue();
display();
return 0;
}
Output: -
Inserted 10 into the circular queue.
Inserted 20 into the circular queue.
Inserted 30 into the circular queue.
Inserted 40 into the circular queue.
Inserted 50 into the circular queue.
Circular Queue Overflow!
Circular Queue elements are: 10 20 30 40 50
Deleted 10 from the circular queue.
Deleted 20 from the circular queue.
Circular Queue elements are: 30 40 50
Question 7: - Write a program to implement Linear Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.
Code: -
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
insertAtEnd(&head, 30);
displayList(head);
deleteNode(&head, 20);
displayList(head);
searchNode(head, 10);
searchNode(head, 40);
return 0;
}
Output: -
Inserted 10 at the end.
Inserted 20 at the end.
Inserted 5 at the beginning.
Inserted 30 at the end.
Linked list: 5 -> 10 -> 20 -> 30 -> NULL
Deleted node with value 20.
Linked list: 5 -> 10 -> 30 -> NULL
Node with value 10 found.
Node with value 40 not found.
Question 8: - Write a program to implement Stack using Linked List. Implement Push,
Pop, and display operations.
Code: -
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* top = NULL;
push(&top, 10);
push(&top, 20);
push(&top, 30);
displayStack(top);
pop(&top);
displayStack(top);
return 0;
}
Output: -
Pushed 10 onto the stack.
Pushed 20 onto the stack.
Pushed 30 onto the stack.
Stack elements: 30 -> 20 -> 10 -> NULL
Popped 30 from the stack.
Stack elements: 20 -> 10 -> NULL
// Enqueue (Insertion)
void enqueue(struct Node** front, struct Node** rear, int value) {
struct Node* newNode = createNode(value);
if (*rear == NULL) {
*front = *rear = newNode;
printf("Inserted %d into the queue.\n", value);
return;
}
(*rear)->next = newNode;
*rear = newNode;
printf("Inserted %d into the queue.\n", value);
}
// Dequeue (Deletion)
void dequeue(struct Node** front, struct Node** rear) {
if (*front == NULL) {
printf("Queue Underflow! Queue is empty.\n");
return;
}
struct Node* temp = *front;
*front = (*front)->next;
printf("Deleted %d from the queue.\n", temp->data);
free(temp);
if (*front == NULL) {
*rear = NULL;
}
}
// Display Queue
void displayQueue(struct Node* front) {
if (front == NULL) {
printf("Queue is empty!\n");
return;
}
struct Node* temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* front = NULL;
struct Node* rear = NULL;
displayQueue(front);
dequeue(&front, &rear);
displayQueue(front);
return 0;
}
Output: -
Inserted 10 into the queue.
Inserted 20 into the queue.
Inserted 30 into the queue.
Queue elements: 10 -> 20 -> 30 -> NULL
Deleted 10 from the queue.
Queue elements: 20 -> 30 -> NULL
Question 10: - Write a program to count the number of times an item is present in a linked
list.
Code: -
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 10);
insertAtEnd(&head, 30);
insertAtEnd(&head, 10);
displayList(head);
return 0;
}
Output: -
10 -> 20 -> 10 -> 30 -> 10 -> NULL
Element 10 occurs 3 times in the list.
Question 11: - Write a program to increment the data part of every node present in a
linked list by 10. Display the data both before incrementation and after.
Code: -
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
printf("Before incrementation:\n");
displayList(head);
incrementData(head);
printf("After incrementation:\n");
displayList(head);
return 0;
}
Output: -
Before incrementation:
10 -> 20 -> 30 -> NULL
After incrementation:
20 -> 30 -> 40 -> NULL
Question 12: - Write a program to implement Doubly Linked List, showing all the
operations, like creation, display, insertion, deletion, and searching.
Code: -
#include <stdio.h>
#include <stdlib.h>
Output: -
Doubly Linked List: 10 <-> 20 <-> 30 <-> NULL
Deleted node with value 20.
Doubly Linked List: 10 <-> 30 <-> NULL
Node with value 30 found.
Node with value 40 not found!
Question 13: - Write a program to search for an element using Linear Search.
Code: -
#include <stdio.h>
// Function for Linear Search
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int result = linearSearch(arr, size, key);
if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output: -
Element 30 found at index 2.
Question 14: - Write a program to search for an element using Binary Search.
Code: -
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 40;
return 0;
}
Output: -
Element 40 found at index 3.
Question 15: - Write a program to sort the given array using Bubble Sort.
Code: -
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
return 0;
}
Output: -
Array before sorting:
64 34 25 12 22 11 90
Array after sorting:
11 12 22 25 34 64 90