simvik2.0
simvik2.0
#include <stdio.h>
int arr[100], size = 0;
void sort_array() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void insert_sorted(int val) {
sort_array();
int i;
for (i = size - 1; i >= 0 && arr[i] > val; i--)
arr[i + 1] = arr[i];
arr[i + 1] = val;
size++;
}
void insert_unsorted(int pos, int val) {
for (int i = size; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = val;
size++;
}
void delete_sorted(int val) {
sort_array();
int pos = -1;
for (int i = 0; i < size; i++) {
if (arr[i] == val) {
pos = i;
break;
}
1
}
if (pos == -1) return;
for (int i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
}
void delete_unsorted(int val) {
int pos = -1;
for (int i = 0; i < size; i++) {
if (arr[i] == val) {
pos = i;
break;
}
}
if (pos == -1) return;
arr[pos] = arr[size - 1]; // Replace with last element
size--;
}
void traverse() {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void reverse() {
for (int i = 0, j = size - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
int choice, val, pos;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter elements: ");
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
while (1) {
printf("\n1. Insert in Sorted\n2. Insert in Unsorted\n3. Delete from Sorted\n4. Delete from
Unsorted\n5. Traverse\n6. Reverse\n7. Exit\nChoose: ");
scanf("%d", &choice);
switch (choice) {
2
case 1:
sort_array();
printf("Enter value to insert: ");
scanf("%d", &val);
insert_sorted(val);
break;
case 2:
printf("Enter position and value: ");
scanf("%d %d", &pos, &val);
insert_unsorted(pos, val);
break;
case 3:
sort_array();
printf("Enter value to delete: ");
scanf("%d", &val);
delete_sorted(val);
break;
case 4:
printf("Enter value to delete: ");
scanf("%d", &val);
delete_unsorted(val);
break;
case 5:
traverse();
break;
case 6:
reverse();
break;
case 7:
return 0;
default:
printf("Invalid choice\n");
}
}
}
3
Output:-
4
5
PROGRAM-2
/*WAP to perform following string related function (user defined) on an
array
i. Finding string length ii. Concatenation of two strings iii. Comparing two
string iv. Copy one string to another*/
#include <stdio.h>
char str1[50], str2[50], str3[100];
int get_length(char str[]) {
int i = 0;
while (str[i] != '\0' && str[i] != '\n') {
i++;
}
return i;
}
6
void copy_string(char s1[], char s2[]) {
int i = 0;
while (s1[i] != '\0' && s1[i] != '\n') {
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
}
int main() {
int choice;
printf("Choose an operation:\n");
printf("1. Get String Length\n");
printf("2. Concatenate Two Strings\n");
printf("3. Compare Two Strings\n");
printf("4. Copy a String\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
printf("Enter the string: ");
fgets(str1, sizeof(str1), stdin);
printf("Length of string: %d\n", get_length(str1));
break;
case 2:
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
concat_strings(str1, str2);
printf("Concatenated String: %s\n", str3);
break;
case 3:
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
if (compare_strings(str1, str2))
printf("Strings are the same.\n");
else
printf("Strings are different.\n");
7
break;
case 4:
printf("Enter the string: ");
fgets(str1, sizeof(str1), stdin);
copy_string(str1, str2);
printf("Copied String: %s\n", str2);
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output:-
8
PROGRAM-3
/*WAP to perform sorting in array(Menu Driven) :-
i. Bubble sort ii. Selection sort iii. Insertion sort iv. merge sort v. Merging
two sorted array*/
#include <stdio.h>
int main() {
int arr[100], n, choice;
printf("Choose an operation:\n");
printf("1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Merge Sort\n5. Merge Two Sorted
Arrays\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
bubble_sort(arr, n);
printf("Sorted Array (Bubble Sort):\n");
display_array(arr, n);
break;
case 2:
selection_sort(arr, n);
printf("Sorted Array (Selection Sort):\n");
display_array(arr, n);
break;
case 3:
insertion_sort(arr, n);
printf("Sorted Array (Insertion Sort):\n");
display_array(arr, n);
break;
case 4:
merge_sort(arr, 0, n - 1);
printf("Sorted Array (Merge Sort):\n");
display_array(arr, n);
9
break;
case 5: {
int a[50], b[50], c[100], n1, n2;
printf("Enter first sorted array:\n");
input_array(a, &n1);
printf("Enter second sorted array:\n");
input_array(b, &n2);
merge_arrays(a, n1, b, n2, c);
printf("Merged Sorted Array:\n");
display_array(c, n1 + n2);
break;
}
default:
printf("Invalid choice!\n");
}
return 0;
}
10
arr[i] = arr[min];
arr[min] = temp;
}
}
void merge_arrays(int a[], int n1, int b[], int n2, int res[]) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
res[k++] = (a[i] < b[j]) ? a[i++] : b[j++];
}
while (i < n1) res[k++] = a[i++];
while (j < n2) res[k++] = b[j++];
}
11
Output:-
12
PROGRAM-4
/*WAP to perform searching in array(Menu Driven) :-
i. Linear search ii. Binary Search*/
#include <stdio.h>
void input();
int search();
void linear();
void binary();
int arr[100], n;
int main() {
int choice;
printf("1. Linear Search\n2. Binary Search\nEnter choice: ");
scanf("%d", &choice);
input();
if (choice == 1) linear();
else if (choice == 2) binary();
else printf("Invalid choice\n");
return 0;
}
void input() {
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
}
int search() {
int ele;
13
printf("Enter element to search: ");
scanf("%d", &ele);
return ele;
}
void linear() {
int ele = search(), found = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == ele) {
found = 1;
printf("Element found at index %d\n", i);
break;
}
}
if (!found) printf("Element not found\n");
}
void binary() {
int ele = search(), low = 0, high = n - 1, mid, found = 0;
Output:-
14
PROGRAM-5
/* WAP to accept a matrix from user and find out whether matrix is sparse
or not and convert into triplex matrix or tuple form */
#include <stdio.h>
int main() {
int r, c, nz = 0;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int mat[r][c];
printf("Enter elements:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &mat[i][j]);
if (mat[i][j] != 0) nz++;
}
}
int total = r * c;
if (total - nz >= total / 2) {
printf("\nThe matrix is a sparse matrix.\n");
int tuple[nz + 1][3];
to_tuple(r, c, nz, mat, tuple);
printf("Tuple form:\n");
printf("row\tcol\tval\n");
for (int i = 0; i <= nz; i++)
printf("%d\t %d\t %d\n", tuple[i][0], tuple[i][1], tuple[i][2]);
} else {
printf("The matrix is not a sparse matrix.\n");
}
return 0;
}
15
tuple[0][2] = nz;
int k = 1;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (mat[i][j] != 0) {
tuple[k][0] = i;
tuple[k][1] = j;
tuple[k][2] = mat[i][j];
k++;
}
}
}
}
Output:-
16
PROGRAM-6
/* WAP to display the sparse matrix in the following way(Menu Driven) :-
i. Upper triangular matrix ii. Lower triangular matrix iii. Diagonal
matrix*/
#include <stdio.h>
int main() {
int r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int mat[r][c];
printf("Enter elements:\n");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &mat[i][j]);
int choice;
printf("\n1 - Lower Triangular\n2 - Upper Triangular\n3 - Diagonal\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: lower(r, c, mat); break;
case 2: upper(r, c, mat); break;
case 3: diagonal(r, c, mat); break;
default: printf("Invalid choice\n");
}
return 0;
}
17
printf("\n");
}
}
Output:-
18
PROGRAM-7
/* WAP to perform the following operations on sparse matrix(Menu
Driven:
i. Transpose of sparse matrix ii. Add two sparse matrices iii. Multiply two
sparse matrices*/
#include <stdio.h>
#define MAX 10
int main() {
int rows, cols, choice;
if (choice == 1) {
transpose(tuple);
}
19
else if (choice == 2) {
printf("Enter second matrix:\n");
inputNormalMatrix(normal, rows, cols);
convertToTuple(normal, rows, cols, mat2);
add(tuple, mat2, res);
}
else if (choice == 3) {
int rows2, cols2;
printf("Enter rows and columns of the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols != rows2) {
printf("Multiplication not possible.\n");
return 0;
}
return 0;
}
20
for (int j = 0; j < cols; j++) {
if (normal[i][j] != 0) {
tuple[k][0] = i;
tuple[k][1] = j;
tuple[k][2] = normal[i][j];
k++;
}
}
}
tuple[0][2] = k - 1;
}
printf("Transpose:\n");
printTuple(res);
}
21
printf("Addition not possible.\n");
return;
}
int i = 1, j = 1, k = 1;
res[0][0] = mat1[0][0];
res[0][1] = mat1[0][1];
res[0][2] = k - 1;
22
printf("Resultant Matrix (Addition):\n");
printTuple(res);
}
int k = 1;
res[0][0] = mat1[0][0];
res[0][1] = mat2[0][1];
res[0][2] = k - 1;
printf("Resultant Matrix (Multiplication):\n");
printTuple(res);
}
23
Output:-
24
PROGRAM-8
/* WAP to do the following operations on Singly linked list-
i. Create a list ii. Traverse a list(forward/backward) iii. Reversal of a list*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
} *first = NULL;
void createList(int n) {
struct node *newNode, *last = NULL;
for (int i = 1; i <= n; i++) {
newNode = (struct node*)malloc(sizeof(struct node));
printf("Enter Node %d info: ", i);
scanf("%d", &newNode->info);
newNode->next = NULL;
if (first == NULL)
first = last = newNode;
else {
last->next = newNode;
last = newNode;
}
}
}
void displayForward() {
struct node *temp = first;
if (!temp) {
printf("List is empty.\n");
return;
}
printf("Forward List: \n");
while (temp) {
printf("%d -> ", temp->info);
temp = temp->next;
}
25
printf("NULL\n");
}
void reverseList() {
struct node *prev = NULL, *current = first, *next = NULL;
while (current) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
first = prev;
}
int main() {
int choice, n;
while (1) {
printf("\nMenu:\n");
printf("1. Create List\n");
printf("2. Display List (Forward)\n");
printf("3. Display List (Backward)\n");
printf("4. Reverse List\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (first != NULL) {
printf("List already created.\n");
} else {
printf("Enter number of nodes: ");
scanf("%d", &n);
createList(n);
}
break;
case 2:
26
displayForward();
break;
case 3:
printf("Backward List: \n");
displayBackward(first);
printf("NULL\n");
break;
case 4:
reverseList();
printf("List reversed successfully.\n");
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
}
Output:-
27
PROGRAM-9
/*WAP to do the following operations on Singly linked list
i. Insertion in a list(sorted/unsorted list) :-
a. At the beginning b. At the end c. Anywhere in the middle*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* next;
} *first = NULL;
newNode->next = temp->next;
temp->next = newNode;
}
void createList() {
int n, info;
printf("Enter the number of nodes: ");
scanf("%d", &n);
28
printf("Enter info for node %d: ", i + 1);
scanf("%d", &info);
if (isSorted)
insertSorted(info);
else {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->info = info;
newNode->next = NULL;
if (first == NULL)
first = newNode;
else {
struct node* temp = first;
while (temp->next)
temp = temp->next;
temp->next = newNode;
}
}
}
}
if (first == NULL)
first = newNode;
else {
struct node* temp = first;
while (temp->next)
temp = temp->next;
temp->next = newNode;
}
}
29
void insertMiddle(int info, int pos) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->info = info;
if (pos == 1) {
newNode->next = first;
first = newNode;
return;
}
if (temp == NULL) {
printf("Position out of range!\n");
free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
void displayList() {
struct node* temp = first;
if (!temp) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (temp) {
printf("%d -> ", temp->info);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice, info, pos;
30
printf("Enter choice: ");
scanf("%d", &isSorted);
isSorted = (isSorted == 1) ? 1 : 0;
createList();
displayList();
while (1) {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Any Position\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter info: ");
scanf("%d", &info);
if (isSorted)
insertSorted(info);
else
insertBeginning(info);
break;
case 2:
printf("Enter info: ");
scanf("%d", &info);
if (isSorted)
insertSorted(info);
else
insertEnd(info);
break;
case 3:
if (isSorted) {
printf("For sorted list, elements are always inserted in order.\n");
break;
}
printf("Enter info: ");
scanf("%d", &info);
printf("Enter position: ");
scanf("%d", &pos);
31
insertMiddle(info, pos);
break;
case 4:
displayList();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
}
Output:-
32
33
PROGRAM-10
/*WAP to do the following operations on Singly linked list:-
struct node {
int info;
struct node* next;
} *first = NULL;
void create_node();
void display_nodes();
void delete_from_sorted();
void delete_from_unsorted();
void main() {
printf("Select an operation:\n");
printf("1. Delete from a sorted linked list\n");
printf("2. Delete from an unsorted linked list\n");
printf("Enter your choice: ");
int user_choice;
scanf("%d", &user_choice);
switch (user_choice) {
case 1:
create_node();
printf("Current linked list:\n");
display_nodes();
delete_from_sorted();
printf("\nUpdated linked list:\n");
display_nodes();
break;
case 2:
34
create_node();
printf("Current linked list:\n");
display_nodes();
delete_from_unsorted();
printf("\nUpdated linked list:\n");
display_nodes();
break;
default:
printf("Invalid choice! Please select 1 or 2.\n");
break;
}
}
void create_node() {
NODE* last = NULL;
int total_nodes;
if (first == NULL) {
first = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
}
void display_nodes() {
NODE* ptr = first;
if (ptr == NULL) {
35
printf("The list is empty.\n");
return;
}
while (ptr != NULL) {
printf("%d -> ", ptr->info);
ptr = ptr->next;
}
printf("NULL\n");
}
void delete_from_sorted() {
int value;
printf("\nEnter the element to delete: ");
scanf("%d", &value);
if (first == NULL) {
printf("The list is empty.\n");
return;
}
if (first->info == value) {
NODE* temp = first;
first = first->next;
free(temp);
printf("Element deleted successfully.\n");
return;
}
36
void delete_from_unsorted() {
int position;
printf("\nEnter the position to delete: ");
scanf("%d", &position);
if (first == NULL) {
printf("The list is empty.\n");
return;
}
if (position == 1) {
NODE* temp = first;
first = first->next;
free(temp);
printf("Node deleted successfully.\n");
return;
}
37
Output:-
38
PROGRAM-11
/* WAP to do the following operations on linked list:-
struct node {
int info;
struct node* next;
} *first = NULL;
void createNode();
void displayNodes();
void searchNode();
void bubbleSort();
int main() {
printf("Choose from the following operations:\n");
printf("1 -- Search in the linked list\n");
printf("2 -- Sort the linked list\n");
printf("Enter your choice: ");
int userChoice;
scanf("%d", &userChoice);
switch (userChoice) {
case 1:
createNode();
printf("Current linked list:\n");
displayNodes();
searchNode();
break;
case 2:
createNode();
printf("Current linked list:\n");
displayNodes();
bubbleSort();
printf("\nSorted linked list:\n");
39
displayNodes();
break;
default:
printf("Invalid choice\n");
break;
}
return 0;
}
void createNode() {
NODE *last = NULL;
int n;
printf("Enter the number of nodes: ");
scanf("%d", &n);
if (first == NULL) {
first = last = newNode;
} else {
last->next = newNode;
last = newNode;
}
}
}
void displayNodes() {
NODE *current = first;
while (current != NULL) {
printf("%d -> ", current->info);
current = current->next;
}
printf("NULL\n");
}
40
void searchNode() {
int element, found = 0, position = 0;
printf("\nEnter the element to search: ");
scanf("%d", &element);
if (found) {
printf("Element found at node %d\n", position);
} else {
printf("Element not found\n");
}
}
void bubbleSort() {
int count = 0, temp;
NODE *current = first, *nextNode;
41
}
}
Output:-
42
PROGRAM-12
/*WAP to do the following operations on Doubly linked list-
1. Create a list
2. Traverse a list(forward/backward)*/
#include <stdio.h>
#include <stdlib.h>
struct dnode {
int info;
struct dnode *prev, *next;
} *first = NULL;
void createNode();
void displayForward();
void displayBackward();
int main() {
createNode();
printf("Forward traversal:\n");
displayForward();
printf("\nBackward traversal:\n");
displayBackward();
return 0;
}
void createNode() {
NODE *last = NULL;
int n;
printf("Enter the number of nodes: ");
scanf("%d", &n);
43
}
printf("Enter Node %d value: ", i);
scanf("%d", &newNode->info);
newNode->prev = newNode->next = NULL;
if (first == NULL) {
first = last = newNode;
} else {
last->next = newNode;
newNode->prev = last;
last = newNode;
}
}
}
void displayForward() {
NODE *current = first;
printf("NULL -> ");
while (current != NULL) {
printf("%d -> ", current->info);
current = current->next;
}
printf("NULL\n");
}
void displayBackward() {
NODE *current = first;
if (current == NULL) {
printf("List is empty\n");
return;
}
44
Output:-
45
PROGRAM-13
/* WAP to do the following operations on Doubly linked list
i. Insertion in a list(sorted/unsorted list)
a. At the beginning
b. At the end
c. Anywhere in the middle*/
#include <stdio.h>
#include <stdlib.h>
struct dnode {
int info;
struct dnode *prev, *next;
} *first = NULL;
void createNode();
void displayForward();
void insertSorted();
void insertUnsorted();
int main() {
printf("Choose from the following operations:\n");
printf("1 -- Insertion in sorted doubly linked list\n");
printf("2 -- Insertion in unsorted doubly linked list\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
createNode();
printf("\nCurrent linked list:\n");
displayForward();
insertSorted();
printf("\nUpdated linked list:\n");
displayForward();
break;
46
case 2:
createNode();
printf("\nCurrent linked list:\n");
displayForward();
insertUnsorted();
printf("\nUpdated linked list:\n");
displayForward();
break;
default:
printf("Invalid operation\n");
break;
}
return 0;
}
void createNode() {
NODE *last = NULL;
int n;
printf("Enter the number of nodes: ");
scanf("%d", &n);
if (first == NULL) {
first = last = newNode;
} else {
last->next = newNode;
newNode->prev = last;
last = newNode;
}
}
}
void displayForward() {
NODE *current = first;
47
printf("NULL -> ");
while (current != NULL) {
printf("%d -> ", current->info);
current = current->next;
}
printf("NULL\n");
}
void insertSorted() {
int element;
printf("\nEnter element to insert: ");
scanf("%d", &element);
if (current->next == NULL) {
current->next = newNode;
newNode->prev = current;
} else {
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
48
}
}
void insertUnsorted() {
int position;
printf("\nEnter element to insert: ");
if (position <= 0) {
printf("Invalid position\n");
free(newNode);
return;
}
if (position == 1) {
newNode->next = first;
if (first != NULL) {
first->prev = newNode;
}
first = newNode;
return;
}
if (current->next == NULL) {
current->next = newNode;
49
newNode->prev = current;
} else {
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
}
}
Output:-
50
PROGRAM-14
/*WAP to do the following operations on Doubly linked list:-
i. Deletion in a list(sorted/unsorted list)
a.At the beginning
b.At the end
c.Anywhere in the middle
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *prev, *next;
} *first = NULL;
void createNode();
void displayForward();
void deleteSorted();
void deleteUnsorted();
int main() {
printf("Choose from the following operations:\n");
printf("1 -- Deletion from sorted doubly linked list\n");
printf("2 -- Deletion from unsorted doubly linked list\n");
printf("Enter your operation: ");
int userOperation;
scanf("%d", &userOperation);
switch (userOperation) {
case 1:
createNode();
printf("\nCurrent Linked List:\n");
displayForward();
deleteSorted();
printf("\nUpdated Linked List:\n");
displayForward();
break;
51
case 2:
createNode();
printf("\nCurrent Linked List:\n");
displayForward();
deleteUnsorted();
printf("\nUpdated Linked List:\n");
displayForward();
break;
default:
printf("Oops! You entered an invalid operation\n");
break;
}
return 0;
}
void createNode() {
NODE *last = NULL;
int totalNodes;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &totalNodes);
if (first == NULL) {
first = newNode;
last = newNode;
} else {
last->next = newNode;
newNode->prev = last;
last = newNode;
}
}
}
void displayForward() {
NODE *ptr = first;
printf("NULL -> ");
while (ptr != NULL) {
52
printf("%d -> ", ptr->info);
ptr = ptr->next;
}
printf("NULL\n");
}
void deleteSorted() {
if (first == NULL) {
printf("\nList is empty!\n");
return;
}
int element;
printf("\nEnter the element to delete: ");
scanf("%d", &element);
53
printf("Element does not exist in linked list\n");
}
void deleteUnsorted() {
if (first == NULL) {
printf("\nList is empty!\n");
return;
}
int pos;
printf("\nEnter the position to delete: ");
scanf("%d", &pos);
if (pos == 1) {
first = ptr->next;
if (first != NULL) {
first->prev = NULL;
}
free(ptr);
printf("Node deleted successfully\n");
return;
}
if (ptr == NULL) {
printf("Node does not exist in linked list\n");
return;
}
if (ptr->next != NULL) {
ptr->next->prev = ptr->prev;
}
if (ptr->prev != NULL) {
ptr->prev->next = ptr->next;
54
}
free(ptr);
printf("Node deleted successfully\n");
}
Output:-
55
PROGRAM-15
/* WAP to do the following operations on Circular linked list-
i. Create a list
ii. Traverse a list(forward/backward)*/
#include <stdio.h>
#include <stdlib.h>
struct cnode {
int info;
struct cnode *prev, *next;
} *first = NULL;
void createNode();
void displayForward();
void displayBackward();
int main() {
createNode();
printf("\nForward traversal:\n");
displayForward();
printf("\n\nBackward traversal:\n");
displayBackward();
return 0;
}
void createNode() {
NODE *last = NULL;
int totalNodes;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &totalNodes);
56
if (first == NULL) {
first = newNode;
first->next = first;
first->prev = first;
last = first;
} else {
newNode->next = first;
newNode->prev = last;
last->next = newNode;
first->prev = newNode;
last = newNode;
}
}
}
void displayForward() {
if (first == NULL) {
printf("List is empty\n");
return;
}
void displayBackward() {
if (first == NULL) {
printf("List is empty\n");
return;
}
57
Output:-
58
PROGRAM-16
/*WAP to do the following operations on Circular linked list
i. Insertion in a list(sorted/unsorted list)
a.At the beginning
b.At the end
c.Anywhere in the middle*/
#include <stdio.h>
#include <stdlib.h>
struct cnode {
int info;
struct cnode *prev, *next;
} *first = NULL;
typedef struct cnode NODE;
void createNode();
void displayList();
void insertSorted();
void insertUnsorted();
int main() {
printf("Choose from the following operations:\n");
printf("1 -- Insertion in sorted double circular linked list\n");
printf("2 -- Insertion in unsorted double circular linked list\n");
printf("Enter your operation: ");
int userOption;
scanf("%d", &userOption);
switch (userOption) {
case 1:
createNode();
printf("Current linked list:\n");
displayList();
insertSorted();
printf("\nUpdated linked list:\n");
displayList();
break;
59
case 2:
createNode();
printf("Current linked list:\n");
displayList();
insertUnsorted();
printf("\nUpdated linked list:\n");
displayList();
break;
default:
printf("You entered an invalid operation\n");
break;
}
return 0;
}
void createNode() {
NODE *last = NULL;
int totalNodes;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &totalNodes);
if (first == NULL) {
first = newNode;
last = newNode;
first->next = first;
first->prev = first;
} else {
last->next = newNode;
last = newNode;
first->prev = last;
}
}
}
60
void displayList() {
if (first == NULL) {
printf("List is empty\n");
return;
}
void insertSorted() {
int element;
printf("\nEnter the element to insert: ");
scanf("%d", &element);
if (first == NULL) {
newNode->next = newNode;
newNode->prev = newNode;
first = newNode;
return;
}
61
}
newNode->next = ptr->next;
newNode->prev = ptr;
ptr->next->prev = newNode;
ptr->next = newNode;
}
}
void insertUnsorted() {
NODE *newNode = (NODE*)malloc(sizeof(NODE));
int position;
printf("\nEnter the element to insert: ");
scanf("%d", &newNode->info);
printf("Enter the position to insert at: ");
scanf("%d", &position);
if (position < 1) {
printf("Invalid position\n");
return;
}
if (first == NULL) {
newNode->next = newNode;
newNode->prev = newNode;
first = newNode;
return;
}
if (position == 1) {
newNode->next = first;
newNode->prev = first->prev;
first->prev->next = newNode;
first->prev = newNode;
first = newNode;
return;
}
62
ptr = ptr->next;
}
newNode->next = ptr->next;
newNode->prev = ptr;
ptr->next->prev = newNode;
ptr->next = newNode;
}
Output:-
63
PROGRAM-17
/* WAP to do the following operations on Circular linked list:-
i. Deletion in a list(sorted/unsorted list)
a.At the beginning
b.At the end
c.Anywhere in the middle.*/
#include <stdio.h>
#include <stdlib.h>
struct cnode {
int info;
struct cnode *prev, *next;
}*first = NULL;
void create_node();
void forward();
void delete_at_beginning();
void delete_at_end();
void delete_at_position();
int main() {
int choice;
create_node();
printf("\nCurrent Linked List:\n");
forward();
switch (choice) {
case 1:
delete_at_beginning();
break;
case 2:
64
delete_at_end();
break;
case 3:
delete_at_position();
break;
default:
printf("Invalid choice!\n");
return 1;
}
return 0;
}
void create_node() {
NODE *last = NULL;
int total_nodes;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &total_nodes);
if (first == NULL) {
first = new_node;
first->next = first;
first->prev = first;
last = first;
} else {
new_node->next = first;
new_node->prev = last;
last->next = new_node;
first->prev = new_node;
last = new_node;
65
}
}
}
void forward() {
if (first == NULL) {
printf("List is empty.\n");
return;
}
void delete_at_beginning() {
if (first == NULL) {
printf("List is empty.\n");
return;
}
void delete_at_end() {
if (first == NULL) {
printf("List is empty.\n");
return;
}
66
if (first->next == first) { // Only one node in the list
free(first);
first = NULL;
} else {
last->prev->next = first;
first->prev = last->prev;
free(last);
}
printf("Last node deleted successfully.\n");
}
void delete_at_position() {
if (first == NULL) {
printf("List is empty.\n");
return;
}
if (pos == 1) {
delete_at_beginning();
return;
}
do {
if (count == pos) {
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
free(ptr);
printf("Node at position %d deleted successfully.\n", pos);
return;
}
count++;
ptr = ptr->next;
} while (ptr != first);
printf("Invalid position.\n");
}
67
Output:-
68
PROGRAM-18
/*WAP to merge two singly sorted linked lists.*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *next;
};
typedef struct node NODE;
int main() {
NODE *list1 = NULL, *list2 = NULL;
printf("Creating 1st linked list:\n");
create_node(&list1);
printf("Linked list 1:\n");
NODE *ptr = list1;
while (ptr != NULL) {
printf("%d -> ", ptr->info);
ptr = ptr->next;
}
printf("NULL\n");
69
merge(list1, list2, &merge_list);
ptr = merge_list;
while (ptr != NULL) {
printf("%d -> ", ptr->info);
ptr = ptr->next;
}
printf("NULL\n");
return 0;
}
if (*first == NULL) {
*first = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
70
}
new_node->next = NULL;
if (*merge_list == NULL) {
*merge_list = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
71
if (new_node == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
new_node->info = ptr1->info;
new_node->next = NULL;
if (last == NULL) {
*merge_list = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
ptr1 = ptr1->next;
}
72
}
}
Output:-
73
PROGRAM-19
/*WAP to implement Polynomial addition operation using Singly linked
list.*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int coeff;
int exp;
struct node *next;
};
typedef struct node NODE;
int main() {
NODE *poly1 = NULL, *poly2 = NULL;
printf("Creating Polynomial 1:\n");
create_poly(&poly1);
printf("Polynomial 1:\n");
NODE *current = poly1;
while (current != NULL) {
printf("%dx(%d) ", current->coeff, current->exp);
current = current->next;
}
printf("\n");
74
NODE *result = NULL;
add_poly(&poly1, &poly2, &result);
printf("Resultant Polynomial:\n");
current = result;
while (current != NULL) {
printf("%dx(%d) ", current->coeff, current->exp);
current = current->next;
}
printf("\n");
free_list(poly1);
free_list(poly2);
free_list(result);
return 0;
}
if (*first == NULL) {
*first = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
75
}
}
}
if (*result == NULL) {
*result = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
76
new_node = (NODE *)malloc(sizeof(NODE));
if (new_node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
new_node->coeff = ptr1->coeff;
new_node->exp = ptr1->exp;
new_node->next = NULL;
ptr1 = ptr1->next;
if (last == NULL) {
*result = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
77
temp = first;
first = first->next;
free(temp);
}
}
Output:-
78
PROGRAM-20
/* Write a C program to create two linked lists from a given list in following
way:-
INPUT List: - 1 2 3 4 5 6 7 8 9 10
struct node {
int data;
struct node *next;
};
typedef struct node NODE;
int main() {
NODE *first = NULL;
create_list(&first);
printf("Original linked list:\n");
NODE *current = first;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
79
printf("Even linked list:\n");
current = even_first;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
free_list(first);
free_list(odd_first);
free_list(even_first);
return 0;
}
if (*first == NULL) {
*first = new_node;
last = new_node;
} else {
last->next = new_node;
last = new_node;
}
}
}
80
void separate(NODE **first, NODE **odd_first, NODE **even_first) {
NODE *ptr = *first;
NODE *new_node, *odd = NULL, *even = NULL;
while (ptr != NULL) {
new_node = (NODE *)malloc(sizeof(NODE));
if (new_node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
new_node->data = ptr->data;
new_node->next = NULL;
if (ptr->data % 2 == 0) {
if (*even_first == NULL) {
*even_first = new_node;
even = new_node;
} else {
even->next = new_node;
even = new_node;
}
} else {
if (*odd_first == NULL) {
*odd_first = new_node;
odd = new_node;
} else {
odd->next = new_node;
odd = new_node;
}
}
ptr = ptr->next;
}
}
81
Output:-
82
PROGRAM-21
/* WAP to implement Student Database using Linked List with the
following structure :-
i. Name
ii. Rollno
iii. Marks of 5 subjects
iv. Average
v. Result, If the average < 50, then print ‘Fail’, otherwise ‘Pass’*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char name[50];
int roll;
int marks[5];
float avg;
char result[10];
struct node *next;
} *first = NULL;
int main() {
NODE *last = NULL;
int total;
83
}
printf("Name: ");
fgets(student->name, sizeof(student->name), stdin);
student->name[strcspn(student->name, "\n")] = '\0';
int sum = 0;
for (int j = 0; j < 5; j++) {
printf("Marks in subject %d: ", j + 1);
if (scanf("%d", &student->marks[j]) != 1 || student->marks[j] < 0 || student->marks[j] > 100) {
printf("Invalid marks. Enter values between 0 and 100.\n");
free(student);
return 1;
}
sum += student->marks[j];
}
student->avg = (float)sum / 5;
strcpy(student->result, (student->avg < 50) ? "Fail" : "Pass");
student->next = NULL;
if (first == NULL) {
first = student;
last = student;
} else {
last->next = student;
last = student;
}
}
84
printf("Name: %s\n", ptr->name);
printf("Roll No: %d\n", ptr->roll);
for (int j = 0; j < 5; j++) {
printf("Subject %d: %d ", j + 1, ptr->marks[j]);
}
printf("\nAverage: %.2f\n", ptr->avg);
printf("Result: %s\n", ptr->result);
ptr = ptr->next;
}
// Free memory
ptr = first;
while (ptr != NULL) {
NODE *temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}
Output:-
85