0% found this document useful (0 votes)
17 views

Assignment of Data Structures

Data structures of computer application assignment of programing with codes

Uploaded by

mgamergirl04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment of Data Structures

Data structures of computer application assignment of programing with codes

Uploaded by

mgamergirl04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

DATA STRUCTURES

USING C

Name: Tarandeep kour


Roll no.: 14
Semester: 4 (2nd year)
Course code: UMJCAT401
Course name: Data structures using c
Index
1. PROGRAM TO CREATE A LINKED LIST AND PRINT ALL ELEMENTS
2. PROGRAM TO CREATE LINKED LIST AND INSERT THE ELEMENT AT FIRST, LAST AND MIDDLE
OF LINKED LIST
3. PROGRAM TO CREATE A LINKED LIST AND DELETE THE ELEMENT FROM FIRST, MIDDLE AND
LAST LOCATION
4. WRITE A PROGRAM TO CREATE THE LINKED LIST AND COUNT THE NUMBERS OF ELEMENTS IN
THE LINKED LIST
5. WAP TO CREATE A LINKED LIST AND FIND THE POSITION OF ITEM ENTERED BY USER
6. PROGRAM TO SORT LINKED LIST IN ASCENDING ORDER
7. PROGRAM TO SORT THE LINKED LIST IN DESCENDING ORDER
8. WAP TO CREATE A CIRCULAR LINKED LIST
9. WAP TO COUNT THE NO OF ELEMENT IN LINKED LIST
10. PROGRAM TO CONCATENATE TWO LISTS
11. PROGRAM TO CREATE A STACK AND APPLY PUSH AND POP OPERATION
12. PROGRAM TO IMPLEMENT FACTORIAL OF A NUMBER USING RECURSION
13. WAP FOR FIBONACCI SERIES THROUGH RECURSION
14. PROGRAM TO CREATE A BINARY SEARCH TREE AND PRINT ALL ELEMENTS
15. PROGRAM TO IMPLEMENT SELECTION SORT
16. PROGRAM TO IMPLEMENT QUICK SORT
17. PROGRAM TO IMPLEMENT BUBBLE SORT
18. PROGRAM FOR LINEAR SEARCH
19. WAP TO BINARY SEARCH IN LINEAR ARRAY
20. WAP TO CREATE DOUBLE LINKED LIST
PROGRAM TO CREATE A LINKED LIST AND PRINT ALL ELEMENTS
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to append a new node to the end of the linked list


void append(struct Node** head_ref, int data) {
struct Node* new_node = createNode(data);
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
struct Node* last_node = *head_ref;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}

// Function to print all elements of the linked list


void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

// Main function
int main() {
struct Node* head = NULL;

// Appending elements to the linked list


append(&head, 1);
append(&head, 2);
append(&head, 3);
// Printing all elements of the linked list
printf("Elements of the linked list are: ");
printList(head);

return 0;
}

PROGRAM TO CREATE A LINKED LIST AND PRINT ALL ELEMENTS


Output:

Elements of the linked list are: 1 2 3


PROGRAM TO CREATE LINKED LIST AND INSERT THE ELEMENT AT FIRST, LAST AND MIDDLE OF
LINKED LIST
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to append a new node to the end of the linked list


void append(struct Node** head_ref, int data) {
struct Node* new_node = createNode(data);
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
struct Node* last_node = *head_ref;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}

// Function to insert a new node at the beginning of the linked list


void insertFirst(struct Node** head_ref, int data) {
struct Node* new_node = createNode(data);
new_node->next = *head_ref;
*head_ref = new_node;
}

// Function to insert a new node at the end of the linked list


void insertLast(struct Node** head_ref, int data) {
append(head_ref, data);
}

// Function to insert a new node at the middle of the linked list


void insertMiddle(struct Node* prev_node, int data) {
if (prev_node == NULL) {
printf("The previous node cannot be NULL.");
return;
}
struct Node* new_node = createNode(data);
new_node->next = prev_node->next;
prev_node->next = new_node;
}

// Function to print all elements of the linked list


void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

// Main function
int main() {
struct Node* head = NULL;

// Inserting elements at different positions


insertFirst(&head, 1);
insertLast(&head, 3);
insertMiddle(head, 2);

// Printing all elements of the linked list


printf("Elements of the linked list are: ");
printList(head);

return 0;
}

PROGRAM TO CREATE LINKED LIST AND INSERT THE ELEMENT AT FIRST, LAST AND MIDDLE OF
LINKED LIST
Output:

Elements of the linked list are: 1 2 3


PROGRAM TO CREATE A LINKED LIST AND DELETE THE ELEMENT FROM FIRST, MIDDLE AND LAST
LOCATION
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to append a new node to the end of the linked list


void append(struct Node** head_ref, int data) {
struct Node* new_node = createNode(data);
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
struct Node* last_node = *head_ref;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}

// Function to delete the first node from the linked list


void deleteFirst(struct Node** head_ref) {
if (*head_ref == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct Node* temp = *head_ref;
*head_ref = (*head_ref)->next;
free(temp);
}

// Function to delete the last node from the linked list


void deleteLast(struct Node** head_ref) {
if (*head_ref == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct Node* temp = *head_ref;
struct Node* prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
if (prev == NULL) {
*head_ref = NULL;
} else {
prev->next = NULL;
}
free(temp);
}

// Function to delete the middle node from the linked list


void deleteMiddle(struct Node* prev_node) {
if (prev_node == NULL || prev_node->next == NULL) {
printf("Cannot delete. Previous node is NULL or it is the last node.\n");
return;
}
struct Node* temp = prev_node->next;
prev_node->next = temp->next;
free(temp);
}

// Function to print all elements of the linked list


void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

// Main function
int main() {
struct Node* head = NULL;

// Appending elements to the linked list


append(&head, 1);
append(&head, 2);
append(&head, 3);
append(&head, 4);

// Printing all elements of the linked list


printf("Initial elements of the linked list are: ");
printList(head);

// Deleting elements from different positions


deleteFirst(&head);
deleteMiddle(head->next);
deleteLast(&head);
// Printing all elements of the linked list after deletion
printf("Elements of the linked list after deletion are: ");
printList(head);

return 0;
}

PROGRAM TO CREATE A LINKED LIST AND DELETE THE ELEMENT FROM FIRST, MIDDLE AND LAST
LOCATION
Output:

Initial elements of the linked list are: 1 2 3 4


Elements of the linked list after deletion are: 2
WRITE A PROGRAM TO CREATE THE LINKED LIST AND COUNT THE NUMBERS OF ELEMENTS IN THE
LINKED LIST
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a structure for a node


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to append a new node to the end of the linked list


void append(struct Node** head_ref, int data) {
struct Node* new_node = createNode(data);
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
struct Node* last_node = *head_ref;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}

// Function to count the number of elements in the linked list


int countElements(struct Node* head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}

// Main function
int main() {
struct Node* head = NULL;

// Appending elements to the linked list


append(&head, 1);
append(&head, 2);
append(&head, 3);
append(&head, 4);

// Counting the number of elements in the linked list


int count = countElements(head);

// Printing the count


printf("Number of elements in the linked list: %d\n", count);

return 0;
}

WRITE A PROGRAM TO CREATE A LINKED LIST AND COUNT THE NUMBER OF ELEMENTS IN THE
LINKED LIST
Output:

Number of elements in linked list: 4


WAP TO CREATE A LINKED LIST AND FIND THE POSITION OF ITEM ENTERED BY USER
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

// Function to find the position of an item in the linked list


int findPosition(struct Node* head, int item) {
int position = 1;
struct Node* current = head;

while (current != NULL) {


if (current->data == item)
return position;
position++;
current = current->next;
}

return -1; // Item not found


}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
int item, position;

// Inserting nodes into the linked list


insertNode(&head, 10);
insertNode(&head, 20);
insertNode(&head, 30);
insertNode(&head, 40);
insertNode(&head, 50);

// Display the linked list


printf("Linked List: ");
displayList(head);

// Asking user for the item to find


printf("Enter the item to find its position: ");
scanf("%d", &item);

// Finding the position of the item


position = findPosition(head, item);

if (position == -1)
printf("Item not found in the list.\n");
else
printf("Position of %d in the list: %d\n", item, position);

return 0;
}

WAP TO CREATE A LINKED LIST AND FIND THE POSITION OF ITEM ENTERED BY USER
Output:

Linked List: 10 20 30 40 50
Enter the item to find its position: 30
Position of 30 in the list: 3
PROGRAM TO SORT LINKED LIST IN ASCENDING ORDER
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

// Function to sort the linked list in ascending order


void sortLinkedList(struct Node** head) {
struct Node *current, *index;
int temp;

if (*head == NULL)
return;

for (current = *head; current != NULL; current = current->next) {


for (index = current->next; index != NULL; index = index->next) {
if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
}
}
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;

// Inserting nodes into the linked list


insertNode(&head, 30);
insertNode(&head, 20);
insertNode(&head, 40);
insertNode(&head, 10);
insertNode(&head, 50);

// Display the original linked list


printf("Original Linked List: ");
displayList(head);

// Sort the linked list


sortLinkedList(&head);

// Display the sorted linked list


printf("Sorted Linked List: ");
displayList(head);

return 0;
}

PROGRAM TO SORT LINKED LIST IN ASCENDING ORDER


Output:

Original Linked List: 30 20 40 10 50


Sorted Linked List: 10 20 30 40 50
PROGRAM TO SORT THE LINKED LIST IN DESCENDING ORDER
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

// Function to reverse the linked list


void reverseLinkedList(struct Node** head) {
struct Node *prev = NULL, *current = *head, *next;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}

*head = prev;
}

// Function to sort the linked list in descending order


void sortLinkedList(struct Node** head) {
struct Node *current, *index;
int temp;

if (*head == NULL)
return;
for (current = *head; current != NULL; current = current->next) {
for (index = current->next; index != NULL; index = index->next) {
if (current->data < index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
}
}
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;

// Inserting nodes into the linked list


insertNode(&head, 30);
insertNode(&head, 20);
insertNode(&head, 40);
insertNode(&head, 10);
insertNode(&head, 50);

// Display the original linked list


printf("Original Linked List: ");
displayList(head);

// Sort the linked list in descending order


sortLinkedList(&head);

// Display the sorted linked list


printf("Sorted Linked List (Descending Order): ");
displayList(head);

return 0;
}

PROGRAM TO SORT THE LINKED LIST IN DESCENDING ORDER


Output:

Original Linked List: 30 20 40 10 50


Sorted Linked List (Descending Order): 50 40 30 20 10
WAP TO CREATE A CIRCULAR LINKED LIST
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the circular linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = *head;

newNode->data = data;
newNode->next = *head;

if (*head != NULL) {
while (temp->next != *head)
temp = temp->next;
temp->next = newNode;
} else
newNode->next = newNode; // For the first node

*head = newNode;
}

// Function to display the circular linked list


void displayList(struct Node* head) {
struct Node* current = head;

if (head != NULL) {
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
}
printf("\n");
}

int main() {
struct Node* head = NULL;

// Inserting nodes into the circular linked list


insertNode(&head, 10);
insertNode(&head, 20);
insertNode(&head, 30);
insertNode(&head, 40);
insertNode(&head, 50);
// Display the circular linked list
printf("Circular Linked List: ");
displayList(head);

return 0;
}

WAP TO CREATE A CIRCULAR LINKED LIST


Output:

Circular Linked List: 50 40 30 20 10


WAP TO COUNT THE NO OF ELEMENT IN LINKED LIST
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

// Function to count the number of elements in the linked list


int countElements(struct Node* head) {
int count = 0;
struct Node* current = head;

while (current != NULL) {


count++;
current = current->next;
}

return count;
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;

// Inserting nodes into the linked list


insertNode(&head, 10);
insertNode(&head, 20);
insertNode(&head, 30);
insertNode(&head, 40);
insertNode(&head, 50);

// Display the linked list


printf("Linked List: ");
displayList(head);

// Count the number of elements in the linked list


int count = countElements(head);
printf("Number of elements in the linked list: %d\n", count);

return 0;
}

WAP TO COUNT THE NO OF ELEMENT IN LINKED LIST


Output:

Linked List: 10 20 30 40 50
Number of elements in the linked list: 5
PROGRAM TO CONCATENATE TWO LISTS
Input:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

while (last->next != NULL)


last = last->next;

last->next = newNode;
}

// Function to concatenate two linked lists


void concatenateLists(struct Node** list1, struct Node* list2) {
if (*list1 == NULL) {
*list1 = list2;
return;
}

struct Node* current = *list1;


while (current->next != NULL)
current = current->next;

current->next = list2;
}

// Function to display the linked list


void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;

// Inserting nodes into the first list


insertNode(&list1, 10);
insertNode(&list1, 20);
insertNode(&list1, 30);

// Inserting nodes into the second list


insertNode(&list2, 40);
insertNode(&list2, 50);

// Display the first list


printf("First List: ");
displayList(list1);

// Display the second list


printf("Second List: ");
displayList(list2);

// Concatenate the two lists


concatenateLists(&list1, list2);

// Display the concatenated list


printf("Concatenated List: ");
displayList(list1);

return 0;
}

PROGRAM TO CONCATENATE TWO LISTS


Output:

First List: 10 20 30
Second List: 40 50
Concatenated List: 10 20 30 40 50
PROGRAM TO CREATE A STACK AND APPLY PUSH AND POP OPERATION
Input:

#include <stdio.h>
#include <stdlib.h>

// Define the maximum size of the stack


#define MAX_SIZE 10

// Define a stack structure


struct Stack {
int items[MAX_SIZE];
int top;
};

// Function to initialize the stack


void initializeStack(struct Stack* stack) {
stack->top = -1;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == -1;
}

// Function to check if the stack is full


int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}

// Function to push an element onto the stack


void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->top++;
stack->items[stack->top] = value;
}

// Function to pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
int poppedItem = stack->items[stack->top];
stack->top--;
return poppedItem;
}

// Function to display the elements of the stack


void displayStack(struct Stack* stack) {
printf("Stack: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->items[i]);
}
printf("\n");
}

int main() {
struct Stack stack;
initializeStack(&stack);

// Pushing elements onto the stack


push(&stack, 10);
push(&stack, 20);
push(&stack, 30);

// Displaying the stack


displayStack(&stack);

// Popping an element from the stack


int poppedValue = pop(&stack);
if (poppedValue != -1) {
printf("Popped value: %d\n", poppedValue);
}

// Displaying the stack after popping


displayStack(&stack);

return 0;
}

PROGRAM TO CREATE A STACK AND APPLY PUSH AND POP OPERATION


Output:

Stack: 10 20 30
Popped value: 30
Stack: 10 20
PROGRAM TO IMPLEMENT FACTORIAL OF A NUMBER USING RECURSION
Input:

#include <stdio.h>

// Function prototype
int factorial(int n);

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

// Call the factorial function and print the result


printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}

// Recursive function to calculate factorial


int factorial(int n) {
// Base case: if n is 0 or 1, return 1
if (n == 0 || n == 1) {
return 1;
} else {
// Recursive case: multiply n by factorial of (n-1)
return n * factorial(n - 1);
}
}

PROGRAM TO IMPLEMENT FACTORIAL OF A NUMBER USING RECURSION


Output:

Enter a number: 5
Factorial of 5 = 120
WAP FOR FIBONACCI SERIES THROUGH RECURSION
Input:

#include <stdio.h>

// Function prototype
int fibonacci(int n);

int main() {
int terms;
printf("Enter the number of terms: ");
scanf("%d", &terms);

// Print Fibonacci series up to the given number of terms


printf("Fibonacci Series:\n");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}

// Recursive function to calculate Fibonacci number


int fibonacci(int n) {
// Base case: if n is 0 or 1, return n
if (n == 0 || n == 1) {
return n;
} else {
// Recursive case: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

WAP FOR FIBONACCI SERIES THROUGH RECURSION


Output:

Enter the number of terms: 10


Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
PROGRAM TO CREATE A BINARY SEARCH TREE AND PRINT ALL ELEMENTS
Input:

#include <stdio.h>
#include <stdlib.h>

// Definition of a node in BST


struct Node {
int data;
struct Node *left;
struct Node *right;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to insert a new node in BST


struct Node* insertNode(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}

if (value < root->data) {


root->left = insertNode(root->left, value);
} else if (value > root->data) {
root->right = insertNode(root->right, value);
}

return root;
}

// Function to print elements of BST in inorder traversal


void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}

int main() {
struct Node* root = NULL;
int n, value;

printf("Enter the number of elements in BST: ");


scanf("%d", &n);
printf("Enter the elements of BST: ");
for (int i = 0; i < n; i++) {
scanf("%d", &value);
root = insertNode(root, value);
}

printf("Elements of BST in inorder traversal: ");


inorderTraversal(root);
printf("\n");

return 0;
}

PROGRAM TO CREATE A BINARY SEARCH TREE AND PRINT ALL ELEMENTS


Output:

Enter the number of elements in BST: 7


Enter the elements of BST: 50 30 70 20 40 60 80
Elements of BST in inorder traversal: 20 30 40 50 60 70 80
PROGRAM TO IMPLEMENT SELECTION SORT
Input:

#include <stdio.h>

// Function to perform selection sort


void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
// Traverse through the array
for (i = 0; i < n - 1; i++) {
// Find the index of the minimum element in the unsorted part of the array
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[ j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the first element of the unsorted part
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Perform selection sort


selectionSort(arr, n);

// Print the sorted array


printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

PROGRAM TO IMPLEMENT SELECTION SORT


Output:

Enter the number of elements in the array: 6


Enter 6 elements: 12 45 23 6 18 3
Sorted array: 3 6 12 18 23 45
PROGRAM TO IMPLEMENT QUICK SORT
Input:

#include <stdio.h>

// Function to swap two elements


void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Function to partition the array and return the pivot index


int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choosing the last element as pivot
int i = low - 1; // Index of smaller element

for (int j = low; j < high; j++) {


// If current element is smaller than or equal to pivot
if (arr[ j] <= pivot) {
i++; // Increment index of smaller element
swap(&arr[i], &arr[ j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

// Function to perform quick sort


void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);

// Separately sort elements before and after partition


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");


printArray(arr, n);
return 0;
}

PROGRAM TO IMPLEMENT QUICK SORT


Output:

Unsorted array:
10 7 8 9 1 5
Sorted array:
1 5 7 8 9 10
PROGRAM TO IMPLEMENT BUBBLE SORT
Input:

#include <stdio.h>

// Function to perform bubble sort


void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[ j] > arr[ j + 1]) {
temp = arr[ j];
arr[ j] = arr[ j + 1];
arr[ j + 1] = temp;
}
}
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: ");
printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);
return 0;
}

PROGRAM TO IMPLEMENT BUBBLE SORT


Output:

Unsorted array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
PROGRAM FOR LINEAR SEARCH
Input:

#include <stdio.h>

// Function to perform linear search


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index of the element if found
}
}
return -1; // Return -1 if the element is not found
}

int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;

// Perform linear search


int result = linearSearch(arr, n, 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;
}

PROGRAM FOR LINEAR SEARCH


Output:

Element 30 found at index 2


WAP TO BINARY SEARCH IN LINEAR ARRAY
Input:

#include <stdio.h>

// Function to perform linear search


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index of the element if found
}
}
return -1; // Return -1 if the element is not found
}

int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;

// Perform linear search


int result = linearSearch(arr, n, 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;
}

WAP TO BINARY SEARCH IN LINEAR ARRAY


Output:

Element 30 found at index 2


WAP TO CREATE DOUBLE LINKED LIST
Input:

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

void insertAtEnd(struct Node** head, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;

newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}

while (last->next != NULL) {


last = last->next;
}

last->next = newNode;
newNode->prev = last;
}

void displayList(struct Node* node) {


struct Node* last;
printf("Traversal in forward direction: ");
while (node != NULL) {
printf("%d ", node->data);
last = node;
node = node->next;
}

printf("\nTraversal in reverse direction: ");


while (last != NULL) {
printf("%d ", last->data);
last = last->prev;
}
}

int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);

displayList(head);

return 0;
}

WAP TO CREATE DOUBLE LINKED LIST


Output:

Traversal in forward direction: 1 2 3


Traversal in reverse direction: 3 2 1

You might also like