Queue\circular_array.
1 // WAP to implement Circular queue using Array:
2
3 #include <stdio.h>
4 #define SIZE 5
5
6 int queue[SIZE];
7 int front = -1;
8 int rear = -1;
9
10 void insert(int value) {
11 if ((rear + 1) % SIZE == front) {
12 printf("Queue is Full\n");
13 return;
14 }
15 if (front == -1) { // Initial condition
16 front = 0;
17 }
18 rear = (rear + 1) % SIZE; // Move rear in a circular manner
19 queue[rear] = value;
20 printf("Inserted %d\n", value);
21 }
22
23 void delete() {
24 if (front == -1) {
25 printf("Queue is Empty\n");
26 return;
27 }
28 printf("Deleted %d\n", queue[front]);
29 if (front == rear) { // Queue becomes empty
30 front = rear = -1;
31 } else {
32 front = (front + 1) % SIZE; // Move front in a circular manner
33 }
34 }
35
36 void display() {
37 if (front == -1) {
38 printf("Queue is Empty\n");
39 return;
40 }
41 printf("Queue elements are: ");
42 int i = front;
43 while (1) {
44 printf("%d ", queue[i]);
45 if (i == rear) {
46 break;
47 }
48 i = (i + 1) % SIZE;
49 }
50 printf("\n");
51 }
52
53 int main() {
54 int choice, value;
55
56 do {
57 printf("\nChoose an operation:\n");
58 printf("1. Insert\n");
59 printf("2. Delete\n");
60 printf("3. Display\n");
61 printf("4. Exit\n");
62 printf("Enter your choice: ");
63 scanf("%d", &choice);
64
65 switch (choice) {
66 case 1:
67 printf("Enter the value to insert: ");
68 scanf("%d", &value);
69 insert(value);
70 break;
71 case 2:
72 delete();
73 break;
74 case 3:
75 display();
76 break;
77 case 4:
78 printf("Exiting...\n");
79 break;
80 default:
81 printf("Invalid choice! Please try again.\n");
82 }
83 } while (choice != 4);
84
85 return 0;
86 }
Queue\circular_linklist.c
1 // WAP to implement Circular queue using LinkList:
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 struct Node {
7 int data;
8 struct Node* next;
9 };
10
11 struct Node* front = NULL;
12 struct Node* rear = NULL;
13 // Enqueue Function
14 void insert(int value) {
15 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
16 if (newNode == NULL) {
17 printf("Memory allocation failed. Queue is Full\n");
18 return;
19 }
20 newNode->data = value;
21 newNode->next = NULL;
22
23 if (front == NULL) { // First insertion
24 front = rear = newNode;
25 rear-> next = front; // Point back to front, making it circular
26 } else {
27 rear->next = newNode; // Link the new node at the end of the queue
28 rear = newNode; // Update rear to new node
29 rear->next = front; // Maintain circular link
30 }
31 printf("Inserted %d\n", value);
32 }
33
34 void delete() {
35 if (front == NULL) {
36 printf("Queue is Empty\n");
37 return;
38 }
39 struct Node* temp = front;
40
41 if (front == rear) { // Only one element in the queue
42 front = rear = NULL;
43 } else {
44 front = front->next; // Move front to the next node
45 rear->next = front; // Maintain circular link
46 }
47 printf("Deleted %d\n", temp->data);
48 free(temp);
49 }
50
51 void display() {
52 if (front == NULL) {
53 printf("Queue is Empty\n");
54 return;
55 }
56 struct Node* temp = front;
57 printf("Queue elements are: ");
58 do {
59 printf("%d ", temp->data);
60 temp = temp->next;
61 } while (temp != front); // Loop until we circle back to the front
62 printf("\n");
63 }
64
65 int main() {
66 int choice, value;
67
68 do {
69 printf("\nChoose an operation:\n");
70 printf("1. Insert\n");
71 printf("2. Delete\n");
72 printf("3. Display\n");
73 printf("4. Exit\n");
74 printf("Enter your choice: ");
75 scanf("%d", &choice);
76
77 switch (choice) {
78 case 1:
79 printf("Enter the value to insert: ");
80 scanf("%d", &value);
81 insert(value);
82 break;
83 case 2:
84 delete();
85 break;
86 case 3:
87 display();
88 break;
89 case 4:
90 printf("Exiting...\n");
91 break;
92 default:
93 printf("Invalid choice! Please try again.\n");
94 }
95 } while (choice != 4);
96
97 return 0;
98 }
Queue\DEQUEUE_LINK.c
1 // WAP to implement Double Ended queue using Link List:
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 // dequeue
7 // Node structure for deque
8 struct Node {
9 int data;
10 struct Node* next;
11 struct Node* prev;
12 };
13
14 // Deque structure
15 struct Deque {
16 struct Node* front;
17 struct Node* rear;
18 } deque;
19
20 // Initialize deque
21 void initialize() {
22 [Link] = [Link] = NULL;
23 }
24
25 // Check if deque is empty
26 int isEmpty() {
27 return ([Link] == NULL);
28 }
29
30 // Insert element at the front
31 void insertFront(int value) {
32 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
33 if (newNode == NULL) {
34 printf("Memory overflow, unable to insert!\n");
35 return;
36 }
37 newNode->data = value;
38 newNode->next = [Link];
39 newNode->prev = NULL;
40
41 if (isEmpty()) {
42 [Link] = newNode;
43 } else {
44 [Link]->prev = newNode;
45 }
46 [Link] = newNode;
47 printf("Inserted %d at the front.\n", value);
48 }
49
50 // Insert element at the rear
51 void insertRear(int value) {
52 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
53 if (newNode == NULL) {
54 printf("Memory overflow, unable to insert!\n");
55 return;
56 }
57 newNode->data = value;
58 newNode->prev = [Link];
59 newNode->next = NULL;
60
61 if (isEmpty()) {
62 [Link] = newNode;
63 } else {
64 [Link]->next = newNode;
65 }
66 [Link] = newNode;
67 printf("Inserted %d at the rear.\n", value);
68 }
69
70 // Delete element from the front
71 void deleteFront() {
72 if (isEmpty()) {
73 printf("Deque is empty! Cannot delete from front.\n");
74 return;
75 }
76 struct Node* temp = [Link];
77 printf("Deleted %d from the front.\n", temp->data);
78
79 [Link] = [Link]->next;
80 if ([Link] == NULL) {
81 [Link] = NULL;
82 } else {
83 [Link]->prev = NULL;
84 }
85 free(temp);
86 }
87
88 // Delete element from the rear
89 void deleteRear() {
90 if (isEmpty()) {
91 printf("Deque is empty! Cannot delete from rear.\n");
92 return;
93 }
94 struct Node* temp = [Link];
95 printf("Deleted %d from the rear.\n", temp->data);
96
97 [Link] = [Link]->prev;
98 if ([Link] == NULL) {
99 [Link] = NULL;
100 } else {
101 [Link]->next = NULL;
102 }
103 free(temp);
104 }
105
106 // Display elements in deque
107 void display() {
108 if (isEmpty()) {
109 printf("Deque is empty!\n");
110 return;
111 }
112 struct Node* temp = [Link];
113 printf("Deque elements are: ");
114 while (temp != NULL) {
115 printf("%d ", temp->data);
116 temp = temp->next;
117 }
118 printf("\n");
119 }
120
121 // Main function with menu for deque operations
122 int main() {
123 int choice, value;
124 initialize();
125
126 while (1) {
127 printf("\n--- Deque Operations ---\n");
128 printf("1. Insert at Front\n");
129 printf("2. Insert at Rear\n");
130 printf("3. Delete from Front\n");
131 printf("4. Delete from Rear\n");
132 printf("5. Display\n");
133 printf("6. Exit\n");
134 printf("Enter your choice: ");
135 scanf("%d", &choice);
136
137 switch (choice) {
138 case 1:
139 printf("Enter the value to insert at front: ");
140 scanf("%d", &value);
141 insertFront(value);
142 break;
143 case 2:
144 printf("Enter the value to insert at rear: ");
145 scanf("%d", &value);
146 insertRear(value);
147 break;
148 case 3:
149 deleteFront();
150 break;
151 case 4:
152 deleteRear();
153 break;
154 case 5:
155 display();
156 break;
157 case 6:
158 printf("Exiting...\n");
159 return 0;
160 default:
161 printf("Invalid choice! Please enter a valid option.\n");
162 }
163 }
164 }
Queue\DEQUEUE_array.c
1 // WAP to implement Double Ended queue using Array:
2
3
4 #include <stdio.h>
5 #define MAX 5
6
7 // Double-ended Queue structure
8 struct Deque {
9 int arr[MAX];
10 int front;
11 int rear;
12 } deque;
13
14 // Initialize deque
15 void initialize() {
16 [Link] = -1;
17 [Link] = -1;
18 }
19
20 // Check if deque is full
21 int isFull() {
22 return (([Link] == 0 && [Link] == MAX - 1) || ([Link] == [Link] + 1));
23 }
24
25 // Check if deque is empty
26 int isEmpty() {
27 return ([Link] == -1);
28 }
29
30 // Insert element at the front
31 void insertFront(int value) {
32 if (isFull()) {
33 printf("Deque is full! Cannot insert at front.\n");
34 return;
35 }
36 if ([Link] == -1) {
37 [Link] = [Link] = 0;
38 } else if ([Link] == 0) {
39 [Link] = MAX - 1;
40 } else {
41 [Link] -= 1;
42 }
43 [Link][[Link]] = value;
44 printf("Inserted %d at the front.\n", value);
45 }
46
47 // Insert element at the rear
48 void insertRear(int value) {
49 if (isFull()) {
50 printf("Deque is full! Cannot insert at rear.\n");
51 return;
52 }
53 if ([Link] == -1) {
54 [Link] = [Link] = 0;
55 } else if ([Link] == MAX - 1) {
56 [Link] = 0;
57 } else {
58 [Link] += 1;
59 }
60 [Link][[Link]] = value;
61 printf("Inserted %d at the rear.\n", value);
62 }
63
64 // Delete element from the front
65 void deleteFront() {
66 if (isEmpty()) {
67 printf("Deque is empty! Cannot delete from front.\n");
68 return;
69 }
70 printf("Deleted %d from the front.\n", [Link][[Link]]);
71 if ([Link] == [Link]) {
72 [Link] = [Link] = -1;
73 } else if ([Link] == MAX - 1) {
74 [Link] = 0;
75 } else {
76 [Link] += 1;
77 }
78 }
79
80 // Delete element from the rear
81 void deleteRear() {
82 if (isEmpty()) {
83 printf("Deque is empty! Cannot delete from rear.\n");
84 return;
85 }
86 printf("Deleted %d from the rear.\n", [Link][[Link]]);
87 if ([Link] == [Link]) {
88 [Link] = [Link] = -1;
89 } else if ([Link] == 0) {
90 [Link] = MAX - 1;
91 } else {
92 [Link] -= 1;
93 }
94 }
95
96 // Display elements in deque
97 void display() {
98 if (isEmpty()) {
99 printf("Deque is empty!\n");
100 return;
101 }
102 printf("Deque elements are: ");
103 int i = [Link];
104 while (1) {
105 printf("%d ", [Link][i]);
106 if (i == [Link])
107 break;
108 i = (i + 1) % MAX;
109 }
110 printf("\n");
111 }
112
113 // Main function with menu for deque operations
114 int main() {
115 int choice, value;
116 initialize();
117
118 while (1) {
119 printf("\n--- Deque Operations ---\n");
120 printf("1. Insert at Front\n");
121 printf("2. Insert at Rear\n");
122 printf("3. Delete from Front\n");
123 printf("4. Delete from Rear\n");
124 printf("5. Display\n");
125 printf("6. Exit\n");
126 printf("Enter your choice: ");
127 scanf("%d", &choice);
128
129 switch (choice) {
130 case 1:
131 printf("Enter the value to insert at front: ");
132 scanf("%d", &value);
133 insertFront(value);
134 break;
135 case 2:
136 printf("Enter the value to insert at rear: ");
137 scanf("%d", &value);
138 insertRear(value);
139 break;
140 case 3:
141 deleteFront();
142 break;
143 case 4:
144 deleteRear();
145 break;
146 case 5:
147 display();
148 break;
149 case 6:
150 printf("Exiting...\n");
151 return 0;
152 default:
153 printf("Invalid choice! Please enter a valid option.\n");
154 }
155 }
156 }
Queue\queue_linklist.c
1 // WAP to implement queue using Link List:
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 // queue
6 // Node structure for linked list
7 struct Node {
8 int data;
9 struct Node* next;
10 };
11
12 // Queue structure
13 struct Queue {
14 struct Node* front;
15 struct Node* rear;
16 };
17
18 // Function to create a new queue
19 struct Queue* createQueue() {
20 struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
21 queue->front = NULL;
22 queue->rear = NULL;
23 return queue;
24 }
25
26 // Function to insert an element in the queue
27 void insert(struct Queue* queue, int element) {
28 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
29 newNode->data = element;
30 newNode->next = NULL;
31
32 if (queue->rear == NULL ) {
33 // If the queue is empty, both front and rear will point to the new node
34 queue->front = queue->rear = newNode;
35 } else {
36 // Add the new node at the end of the queue and update rear
37 queue->rear->next = newNode;
38 queue->rear = newNode;
39 }
40 printf("Inserted %d\n", element);
41 }
42
43 // Function to delete an element from the queue
44 void delete(struct Queue* queue) {
45 if (queue->front == NULL) {
46 printf("Queue Underflow\n");
47 return;
48 }
49 struct Node* temp = queue->front;
50 printf("Deleted %d\n", temp->data);
51 queue->front = queue->front->next;
52
53 // If the front becomes NULL, then update rear to NULL as well
54 if (queue->front == NULL) {
55 queue->rear = NULL;
56 }
57 free(temp); // Free the memory of the deleted node
58 }
59
60 // Function to display elements of the queue
61 void display(struct Queue* queue) {
62 if (queue->front == NULL) {
63 printf("Queue is empty\n");
64 return;
65 }
66 // current=temp
67 struct Node* current = queue->front;
68 printf("Queue elements: ");
69 while (current != NULL) {
70 printf("%d ", current->data);
71 current = current->next;
72 }
73 printf("\n");
74 }
75
76 // Main function
77 int main() {
78 struct Queue* queue = createQueue();
79 int choice, element;
80
81 while (1) {
82 printf("\nQueue Operations:\n");
83 printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
84 printf("Enter your choice: ");
85 scanf("%d", &choice);
86
87 switch (choice) {
88 case 1:
89 printf("Enter the element to insert: ");
90 scanf("%d", &element);
91 insert(queue, element);
92 break;
93 case 2:
94 delete(queue);
95 break;
96 case 3:
97 display(queue);
98 break;
99 case 4:
100 printf("Exiting...\n");
101 // Free the memory allocated for the queue
102 while (queue->front != NULL) {
103 delete(queue);
104 }
105 free(queue);
106 return 0;
107 default:
108 printf("Invalid choice! Please try again.\n");
109 }
110 }
111 }
Queue\queue_array.c
1 // WAP to implement Queue Using array:
2
3 #include <stdio.h>
4 #define MAX 5 // Maximum size of the queue
5
6 int queue[MAX];
7 int front = -1, rear = -1;
8
9 // Function to insert an element in the queue
10 void insert(int element) {
11 if (rear == MAX - 1) {
12 printf("Queue Overflow\n");
13 return;
14 }
15 if (front == -1) {
16 front = 0; // Initialize front to 0 if inserting first element
17 }
18 rear++;
19 queue[rear] = element;
20 printf("Inserted %d\n", element);
21 }
22
23 // Function to delete an element from the queue
24 void delete() {
25 if (front == -1 || front > rear) {
26 printf("Queue Underflow\n");
27 return;
28 }
29 printf("Deleted %d\n", queue[front]);
30 front++;
31 if (front > rear) { // Reset queue if empty
32 front = rear = -1;
33 }
34 }
35
36 // Function to display elements of the queue
37 void display() {
38 if (front == -1) {
39 printf("Queue is empty\n");
40 return;
41 }
42 printf("Queue elements: ");
43 for (int i = front; i <= rear; i++) {
44 printf("%d ", queue[i]);
45 }
46 printf("\n");
47 }
48
49 // Main function
50 int main() {
51 int choice, element;
52 while (1) {
53 printf("\nQueue Operations:\n");
54 printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
55 printf("Enter your choice: ");
56 scanf("%d", &choice);
57
58 switch (choice) {
59 case 1:
60 printf("Enter the element to insert: ");
61 scanf("%d", &element);
62 insert(element);
63 break;
64 case 2:
65 delete();
66 break;
67 case 3:
68 display();
69 break;
70 case 4:
71 printf("Exiting...\n");
72 return 0;
73 default:
74 printf("Invalid choice! Please try again.\n");
75 }
76 }
77 }
Polynomial\two_var.c
1 // WAP to implement Two variable polynomial using Link List:
2
3 #include <stdio.h>
4 #define MAX 10 // Maximum degree of the polynomials
5
6 // Function to add two polynomials
7 void addPoly(int poly1[MAX][MAX], int poly2[MAX][MAX], int result[MAX][MAX], int m, int n) {
8 for (int i = 0; i <= m; i++) {
9 for (int j = 0; j <= n; j++) {
10 result[i][j] = poly1[i][j] + poly2[i][j];
11 }
12 }
13 }
14
15 // Function to subtract two polynomials
16 void subPoly(int poly1[MAX][MAX], int poly2[MAX][MAX], int result[MAX][MAX], int m, int n) {
17 for (int i = 0; i <= m; i++) {
18 for (int j = 0; j <= n; j++) {
19 result[i][j] = poly1[i][j] - poly2[i][j];
20 }
21 }
22 }
23
24 // Function to multiply two polynomials
25 void mulPoly(int poly1[MAX][MAX], int poly2[MAX][MAX], int result[MAX][MAX], int m1, int n1,
int m2, int n2) {
26 for (int i = 0; i <= m1 + m2; i++) {
27 for (int j = 0; j <= n1 + n2; j++) {
28 result[i][j] = 0; // Initialize result array
29 }
30 }
31
32 for (int i = 0; i <= m1; i++) {
33 for (int j = 0; j <= n1; j++) {
34 for (int k = 0; k <= m2; k++) {
35 for (int l = 0; l <= n2; l++) {
36 result[i + k][j + l] += poly1[i][j] * poly2[k][l];
37 }
38 }
39 }
40 }
41 }
42
43 // Function to print the polynomial
44 void printPoly(int poly[MAX][MAX], int m, int n) {
45 int first = 1; // Flag to avoid printing " + " before the first term
46 for (int i = m; i >= 0; i--) {
47 for (int j = n; j >= 0; j--) {
48 if (poly[i][j] != 0) {
49 if (!first) printf(" + ");
50 printf("%dx^%dy^%d", poly[i][j], i, j);
51 first = 0; // After first term is printed, set flag to 0
52 }
53 }
54 }
55 printf("\n");
56 }
57
58 int main() {
59 int poly1[MAX][MAX] = {0}, poly2[MAX][MAX] = {0}, result[MAX][MAX] = {0};
60 int m1, n1, m2, n2;
61
62 // Input the degree and coefficients for first polynomial
63 printf("Enter the highest degree of x and y for polynomial 1 (e.g., m1 n1): ");
64 scanf("%d %d", &m1, &n1);
65 printf("Enter the coefficients for polynomial 1:\n");
66 for (int i = 0; i <= m1; i++) {
67 for (int j = 0; j <= n1; j++) {
68 printf("Coefficient of x^%d y^%d: ", i, j);
69 scanf("%d", &poly1[i][j]);
70 }
71 }
72
73 // Input the degree and coefficients for second polynomial
74 printf("Enter the highest degree of x and y for polynomial 2 (e.g., m2 n2): ");
75 scanf("%d %d", &m2, &n2);
76 printf("Enter the coefficients for polynomial 2:\n");
77 for (int i = 0; i <= m2; i++) {
78 for (int j = 0; j <= n2; j++) {
79 printf("Coefficient of x^%d y^%d: ", i, j);
80 scanf("%d", &poly2[i][j]);
81 }
82 }
83
84 // Perform addition
85 printf("\nAddition of the polynomials:\n");
86 addPoly(poly1, poly2, result, (m1 > m2) ? m1 : m2, (n1 > n2) ? n1 : n2);
87 printPoly(result, (m1 > m2) ? m1 : m2, (n1 > n2) ? n1 : n2);
88
89 // Perform subtraction
90 printf("\nSubtraction of the polynomials:\n");
91 subPoly(poly1, poly2, result, (m1 > m2) ? m1 : m2, (n1 > n2) ? n1 : n2);
92 printPoly(result, (m1 > m2) ? m1 : m2, (n1 > n2) ? n1 : n2);
93
94 // Perform multiplication
95 printf("\nMultiplication of the polynomials:\n");
96 mulPoly(poly1, poly2, result, m1, n1, m2, n2);
97 printPoly(result, m1 + m2, n1 + n2);
98
99 return 0;
100 }
Polynomial\polynomial_1 var.c
1 // WAP to implement one variable polynomial using Array:
2
3 #include <stdio.h>
4 #define MAX 100
5
6 // Function to read a polynomial
7 void readPoly(int poly[], int degree) {
8 for (int i = 0; i <= degree; i++) {
9 printf("Enter the coefficient for x^%d: ", i);
10 scanf("%d", &poly[i]);
11 }
12 }
13
14 // Function to print a polynomial
15 void printPoly(int poly[], int degree) {
16 for (int i = degree; i >= 0; i--) {
17 if (poly[i] != 0) {
18 // Print " + " for all terms except the first one
19 if (i != degree)
20 printf(" + ");
21
22 // Print the constant term (when i == 0)
23 if (i == 0)
24 printf("%d", poly[i]);
25 // Print the linear term (when i == 1)
26 else if (i == 1)
27 printf("%dx", poly[i]);
28 // Print higher degree terms (i > 1)
29 else
30 printf("%dx^%d", poly[i], i);
31 }
32 }
33 printf("\n"); // Newline after printing the polynomial
34 }
35
36
37
38
39 // Function for addition of two polynomials
40 void addPoly(int poly1[], int poly2[], int res[], int deg1, int deg2) {
41 int maxDeg = deg1 > deg2 ? deg1 : deg2;
42
43 for (int i = 0; i <= maxDeg; i++) {
44 res[i] = poly1[i] + poly2[i];
45 }
46 }
47
48 // Function for subtraction of two polynomials
49 void subPoly(int poly1[], int poly2[], int res[], int deg1, int deg2) {
50 int maxDeg = deg1 > deg2 ? deg1 : deg2;
51
52 for (int i = 0; i <= maxDeg; i++) {
53 res[i] = poly1[i] - poly2[i];
54 }
55 }
56
57 // Function for multiplication of two polynomials
58 void mulPoly(int poly1[], int poly2[], int res[], int deg1, int deg2) {
59 int degree = deg1 + deg2;
60
61 for (int i = 0; i <= degree; i++) {
62 res[i] = 0; // Initialize result array to 0
63 }
64
65 for (int i = 0; i <= deg1; i++) {
66 for (int j = 0; j <= deg2; j++) {
67 res[i + j] += poly1[i] * poly2[j];
68 }
69 }
70 }
71
72 int main() {
73 int poly1[MAX], poly2[MAX], res[MAX];
74 int deg1, deg2, choice;
75
76 printf("Enter the degree of the first polynomial: ");
77 scanf("%d", °1);
78 readPoly(poly1, deg1);
79
80 printf("Enter the degree of the second polynomial: ");
81 scanf("%d", °2);
82 readPoly(poly2, deg2);
83
84 do {
85 printf("\n1. Add Polynomials\n2. Subtract Polynomials\n3. Multiply Polynomials\n4.
Exit\n");
86 printf("Enter your choice: ");
87 scanf("%d", &choice);
88
89 switch (choice) {
90 case 1:
91 addPoly(poly1, poly2, res, deg1, deg2);
92 printf("Resultant Polynomial after addition: ");
93 printPoly(res, (deg1 > deg2) ? deg1 : deg2);
94 break;
95
96 case 2:
97 subPoly(poly1, poly2, res, deg1, deg2);
98 printf("Resultant Polynomial after subtraction: ");
99 printPoly(res, (deg1 > deg2) ? deg1 : deg2);
100 break;
101
102 case 3:
103 mulPoly(poly1, poly2, res, deg1, deg2);
104 printf("Resultant Polynomial after multiplication: ");
105 printPoly(res, deg1 + deg2);
106 break;
107
108 case 4:
109 printf("Exiting...\n");
110 break;
111
112 default:
113 printf("Invalid choice, please try again.\n");
114 }
115 } while (choice != 4);
116
117 return 0;
118 }
Polynomial\Poly_2var_linklist.c
1 // WAP to implement two variable polynomial using Link List:
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 // Node structure for a term in the polynomial
8 struct node {
9 int coeff; // Coefficient
10 int x; // Power of x
11 int y; // Power of y
12 struct node* next;
13 };
14
15 // Function to create a new node
16 struct node* createNode(int coeff, int x, int y) {
17 struct node* newNode = (struct node*)malloc(sizeof(struct node));
18 newNode->coeff = coeff;
19 newNode->x = x;
20 newNode->y = y;
21 newNode->next = NULL;
22 return newNode;
23 }
24
25 // Function to insert a term into the polynomial
26 struct node* insertTerm(struct node* head, int coeff, int x, int y) {
27 struct node* newNode = createNode(coeff, x, y);
28
29 if (head == NULL) {
30 return newNode; // First term
31 }
32
33 struct node* temp = head;
34 while (temp->next != NULL) {
35 temp = temp->next; // Traverse to the end
36 }
37 temp->next = newNode; // Add the new term
38 return head;
39 }
40
41 // Function to print a polynomial
42 void printPoly(struct node* head) {
43 if (head == NULL) {
44 printf("0\n");
45 return;
46 }
47
48 struct node* temp = head;
49 while (temp != NULL) {
50 printf("%dx^%dy^%d", temp->coeff, temp->x, temp->y);
51 if (temp->next != NULL) printf(" + ");
52 temp = temp->next;
53 }
54 printf("\n");
55 }
56
57 // Function to add two polynomials
58 struct node* addPoly(struct node* poly1, struct node* poly2) {
59 struct node* result = NULL;
60
61 // Traverse both lists and add corresponding terms
62 while (poly1 != NULL && poly2 != NULL) {
63 if (poly1->x > poly2->x || (poly1->x == poly2->x && poly1->y > poly2->y)) {
64 result = insertTerm(result, poly1->coeff, poly1->x, poly1->y);
65 poly1 = poly1->next;
66 } else if (poly1->x < poly2->x || (poly1->x == poly2->x && poly1->y < poly2->y)) {
67 result = insertTerm(result, poly2->coeff, poly2->x, poly2->y);
68 poly2 = poly2->next;
69 } else {
70 result = insertTerm(result, poly1->coeff + poly2->coeff, poly1->x, poly1->y);
71 poly1 = poly1->next;
72 poly2 = poly2->next;
73 }
74 }
75
76 // Add remaining terms from poly1 or poly2
77 while (poly1 != NULL) {
78 result = insertTerm(result, poly1->coeff, poly1->x, poly1->y);
79 poly1 = poly1->next;
80 }
81 while (poly2 != NULL) {
82 result = insertTerm(result, poly2->coeff, poly2->x, poly2->y);
83 poly2 = poly2->next;
84 }
85
86 return result;
87 }
88
89 // Function to multiply two polynomials
90 struct node* mulPoly(struct node* poly1, struct node* poly2) {
91 struct node* result = NULL;
92
93 for (struct node* temp1 = poly1; temp1 != NULL; temp1 = temp1->next) {
94 for (struct node* temp2 = poly2; temp2 != NULL; temp2 = temp2->next) {
95 result = insertTerm(result, temp1->coeff * temp2->coeff, temp1->x + temp2->x,
temp1->y + temp2->y);
96 }
97 }
98
99 return result;
100 }
101
102 // Function to read a polynomial
103 struct node* readPoly() {
104 struct node* head = NULL;
105 int terms, coeff, x, y;
106
107 printf("Enter the number of terms: ");
108 scanf("%d", &terms);
109
110 for (int i = 0; i < terms; i++) {
111 printf("Enter coefficient and powers of x and y: ");
112 scanf("%d %d %d", &coeff, &x, &y);
113 head = insertTerm(head, coeff, x, y);
114 }
115
116 return head;
117 }
118
119 // Main function
120 int main() {
121 struct node* poly1 = NULL;
122 struct node* poly2 = NULL;
123 struct node* result = NULL;
124 int choice;
125
126 printf("Enter the first polynomial:\n");
127 poly1 = readPoly();
128
129 printf("Enter the second polynomial:\n");
130 poly2 = readPoly();
131
132 do {
133 printf("\n1. Add Polynomials\n2. Multiply Polynomials\n3. Exit\n");
134 printf("Enter your choice: ");
135 scanf("%d", &choice);
136
137 switch (choice) {
138 case 1:
139 result = addPoly(poly1, poly2);
140 printf("Resultant Polynomial after addition: ");
141 printPoly(result);
142 break;
143
144 case 2:
145 result = mulPoly(poly1, poly2);
146 printf("Resultant Polynomial after multiplication: ");
147 printPoly(result);
148 break;
149
150 case 3:
151 printf("Exiting...\n");
152 break;
153
154 default:
155 printf("Invalid choice, try again.\n");
156 }
157 } while (choice != 3);
158
159 return 0;
160 }
Polynomial\Poly_1var_linklist.c
1 // WAP to implement one variable polynomial using Link List:
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 // Node structure for a polynomial term
7 struct node {
8 int coeff; // Coefficient
9 int exp; // Exponent
10 struct node* next;
11 };
12
13 // Function to create a new node
14 struct node* createNode(int coeff, int exp) {
15 struct node* newNode = (struct node*)malloc(sizeof(struct node));
16 newNode->coeff = coeff;
17 newNode->exp = exp;
18 newNode->next = NULL;
19 return newNode;
20 }
21
22 // Function to add a term to the polynomial
23 struct node* insertTerm(struct node* head, int coeff, int exp) {
24 struct node* newNode = createNode(coeff, exp);
25
26 if (head == NULL) {
27 return newNode; // First node
28 }
29
30 struct node* temp = head;
31 while (temp->next != NULL) {
32 temp = temp->next; // Traverse to the end
33 }
34
35 temp->next = newNode; // Append the new term
36 return head;
37 }
38
39 // Function to read a polynomial
40 struct node* readPoly() {
41 struct node* head = NULL;
42 int n, coeff, exp;
43
44 printf("Enter the number of terms: ");
45 scanf("%d", &n);
46
47 for (int i = 0; i < n; i++) {
48 printf("Enter coefficient and exponent for term %d: ", i + 1);
49 scanf("%d %d", &coeff, &exp);
50 head = insertTerm(head, coeff, exp);
51 }
52
53 return head;
54 }
55
56 // Function to print a polynomial
57 void printPoly(struct node* head) {
58 if (head == NULL) {
59 printf("Polynomial is empty.\n");
60 return;
61 }
62
63 struct node* temp = head;
64 while (temp != NULL) {
65 printf("%d", temp->coeff);
66
67 if (temp->exp != 0) {
68 printf("x^%d", temp->exp);
69 }
70
71 if (temp->next != NULL) {
72 printf(" + ");
73 }
74
75 temp = temp->next;
76 }
77
78 printf("\n");
79 }
80
81 // Function to add two polynomials
82 struct node* addPoly(struct node* poly1, struct node* poly2) {
83 struct node* result = NULL;
84
85 while (poly1 != NULL && poly2 != NULL) {
86 if (poly1->exp > poly2->exp) {
87 result = insertTerm(result, poly1->coeff, poly1->exp);
88 poly1 = poly1->next;
89 } else if (poly1->exp < poly2->exp) {
90 result = insertTerm(result, poly2->coeff, poly2->exp);
91 poly2 = poly2->next;
92 } else {
93 result = insertTerm(result, poly1->coeff + poly2->coeff, poly1->exp);
94 poly1 = poly1->next;
95 poly2 = poly2->next;
96 }
97 }
98
99 // Append remaining terms
100 while (poly1 != NULL) {
101 result = insertTerm(result, poly1->coeff, poly1->exp);
102 poly1 = poly1->next;
103 }
104
105 while (poly2 != NULL) {
106 result = insertTerm(result, poly2->coeff, poly2->exp);
107 poly2 = poly2->next;
108 }
109
110 return result;
111 }
112
113 // Function to multiply two polynomials
114 struct node* mulPoly(struct node* poly1, struct node* poly2) {
115 struct node* result = NULL;
116
117 for (struct node* temp1 = poly1; temp1 != NULL; temp1 = temp1->next) {
118 for (struct node* temp2 = poly2; temp2 != NULL; temp2 = temp2->next) {
119 int coeff = temp1->coeff * temp2->coeff;
120 int exp = temp1->exp + temp2->exp;
121
122 struct node* temp = result;
123 struct node* prev = NULL;
124
125 while (temp != NULL && temp->exp > exp) {
126 prev = temp;
127 temp = temp->next;
128 }
129
130 if (temp != NULL && temp->exp == exp) {
131 temp->coeff += coeff; // Combine coefficients
132 } else {
133 struct node* newNode = createNode(coeff, exp);
134
135 if (prev == NULL) {
136 newNode->next = result;
137 result = newNode;
138 } else {
139 newNode->next = temp;
140 prev->next = newNode;
141 }
142 }
143 }
144 }
145
146 return result;
147 }
148
149 // Main function
150 int main() {
151 struct node* poly1 = NULL;
152 struct node* poly2 = NULL;
153 struct node* result = NULL;
154 int choice;
155
156 printf("Enter the first polynomial:\n");
157 poly1 = readPoly();
158
159 printf("Enter the second polynomial:\n");
160 poly2 = readPoly();
161
162 do {
163 printf("\n1. Add Polynomials\n2. Multiply Polynomials\n3. Exit\n");
164 printf("Enter your choice: ");
165 scanf("%d", &choice);
166
167 switch (choice) {
168 case 1:
169 result = addPoly(poly1, poly2);
170 printf("Resultant Polynomial after addition: ");
171 printPoly(result);
172 break;
173
174 case 2:
175 result = mulPoly(poly1, poly2);
176 printf("Resultant Polynomial after multiplication: ");
177 printPoly(result);
178 break;
179
180 case 3:
181 printf("Exiting...\n");
182 break;
183
184 default:
185 printf("Invalid choice, please try again.\n");
186 }
187 } while (choice != 3);
188
189 return 0;
190 }
Linked List\circular_operation.c
1
2 //WAP to implement circular Link list operation:
3
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 // circular
8 // Definition of a node in the circular linked list
9 struct node {
10 int data;
11 struct node* next;
12 };
13
14 // Function to insert a node at the beginning
15 struct node* insert_at_begin(struct node* head, int data) {
16 struct node* newNode = (struct node*)malloc(sizeof(struct node));
17 newNode->data = data;
18
19 if (head == NULL) {
20 newNode->next = newNode; // Point to itself, circular link
21 return newNode; // New node is the only node
22 }
23
24 struct node* temp = head;
25 while (temp->next != head) {
26 temp = temp->next; // Traverse to the last node
27 }
28
29 temp->next = newNode; // Link last node to new node
30 newNode->next = head; // New node points to head
31 return newNode; // New node is now the head
32 }
33
34 // Function to insert a node at the end
35 struct node* insert_at_end(struct node* head, int data) {
36 struct node* newNode = (struct node*)malloc(sizeof(struct node));
37 newNode->data = data;
38
39 if (head == NULL) {
40 newNode->next = newNode; // Point to itself, circular link
41 return newNode; // New node is the only node
42 }
43
44 struct node* temp = head;
45 while (temp->next != head) {
46 temp = temp->next; // Traverse to the last node
47 }
48
49 temp->next = newNode; // Link last node to new node
50 newNode->next = head; // New node points to head
51 return head; // Return the head of the list
52 }
53
54 // Function to insert a node at a specific position
55 struct node* insert_at_position(struct node* head, int data, int position) {
56 struct node* newNode = (struct node*)malloc(sizeof(struct node));
57 newNode->data = data;
58
59 // If position is 1, insert at the beginning
60 if (position == 1) {
61 return insert_at_begin(head, data);
62 } else {
63 struct node* temp = head;
64 int i;
65
66 // Traverse to the node just before the specified position
67 for (i = 1; i < position - 1 && temp->next != head; i++) {
68 temp = temp->next;
69 }
70
71 // If position is out of bounds
72 if (i < position - 1) {
73 printf("Position out of bounds\n");
74 free(newNode); // Free the newly allocated memory
75 return head;
76 }
77
78 // Insert the new node at the specified position
79 newNode->next = temp->next; // Link new node to the next node
80 temp->next = newNode; // Link current node to the new node
81 }
82
83 return head;
84 }
85
86
87
88 // Function to delete a node at the beginning
89 struct node* delete_at_begin(struct node* head) {
90 if (head == NULL) {
91 printf("List is empty, nothing to delete.\n");
92 return head;
93 }
94
95 if (head->next == head) {
96 free(head); // If there's only one node
97 return NULL;
98 }
99
100 struct node* temp = head;
101 struct node* last = head;
102
103 // Traverse to the last node
104 while (last->next != head) {
105 last = last->next;
106 }
107
108 head = head->next; // Update head to the next node
109 last->next = head; // Update the last node's next pointer
110 free(temp); // Free the old head
111 return head;
112 }
113
114 // Function to delete a node at the end
115 struct node* delete_at_end(struct node* head) {
116 if (head == NULL) {
117 printf("List is empty, nothing to delete.\n");
118 return head;
119 }
120
121 if (head->next == head) {
122 free(head); // If there's only one node
123 return NULL;
124 }
125
126 struct node* temp = head;
127 struct node* prev = NULL;
128
129 // Traverse to the last node
130 while (temp->next != head) {
131 prev = temp;
132 temp = temp->next;
133 }
134
135 prev->next = head; // Update the second-last node's next pointer
136 free(temp); // Free the last node
137 return head;
138 }
139
140 // Function to delete a node at a specific position
141 struct node* delete_at_position(struct node* head, int position) {
142 if (head == NULL) {
143 printf("List is empty, nothing to delete.\n");
144 return head;
145 }
146
147 if (position == 1) {
148 return delete_at_begin(head); // Special case: delete at beginning
149 }
150
151 struct node* temp = head;
152 struct node* prev = NULL;
153 int i;
154
155 // Traverse to the node at the specified position
156 for (i = 1; i < position && temp->next != head; i++) {
157 prev = temp;
158 temp = temp->next;
159 }
160
161 // If position is out of bounds
162 if (i < position) {
163 printf("Position out of bounds.\n");
164 return head;
165 }
166
167 prev->next = temp->next; // Link the previous node to the next node
168 free(temp); // Free the node at the specified position
169 return head;
170 }
171
172 // Function to display the circular linked list
173 void display_list(struct node* head) {
174 if (head == NULL) {
175 printf("List is empty\n");
176 return;
177 }
178
179 struct node* temp = head;
180 printf("Elements in the list are: ");
181 do {
182 printf("%d -> ", temp->data);
183 temp = temp->next;
184 } while (temp != head);
185 printf("(back to %d)\n", head->data); // Indicate it's circular
186 }
187
188 // Main function to test the circular linked list operations
189 int main() {
190 struct node* head = NULL;
191 int n, i, data;
192
193 // Initial input for nodes
194 printf("Enter number of initial nodes: ");
195 scanf("%d", &n);
196 for (i = 0; i < n; i++) {
197 printf("Enter data for node %d: ", i + 1);
198 scanf("%d", &data);
199
200 // Create a new node directly in the loop
201 struct node* newNode = (struct node*)malloc(sizeof(struct node));
202 newNode->data = data;
203
204 // Insert the new node at the end of the list
205 if (head == NULL) {
206 newNode->next = newNode; // Point to itself, circular link
207 head = newNode; // The list is empty
208 } else {
209 struct node* temp = head;
210 while (temp->next != head) {
211 temp = temp->next; // Traverse to the last node
212 }
213 temp->next = newNode; // Link last node to new node
214 newNode->next = head; // New node points to head
215 }
216 }
217
218 display_list(head); // Display the initial list
219
220 // Inserting at the beginning
221 printf("Enter the data to insert at beginning: ");
222 scanf("%d", &data);
223 head = insert_at_begin(head, data);
224 printf("Updated list after insertion at beginning: ");
225 display_list(head);
226
227 // Inserting at a specific position
228 int position;
229 printf("Enter the position to insert the data: ");
230 scanf("%d", &position);
231 printf("Enter the data to insert at position %d: ", position);
232 scanf("%d", &data);
233 head = insert_at_position(head, data, position);
234 printf("Updated list after insertion at position: ");
235 display_list(head);
236
237 // Inserting at the end
238 printf("Enter the data to insert at end: ");
239 scanf("%d", &data);
240 head = insert_at_end(head, data);
241 printf("Updated list after insertion at end: ");
242 display_list(head);
243
244
245 // Deleting at the beginning
246 printf("Deleting at the beginning...\n");
247 head = delete_at_begin(head);
248 printf("Updated list after deletion at beginning: ");
249 display_list(head);
250
251 // Deleting at the end
252 printf("Deleting at the end...\n");
253 head = delete_at_end(head);
254 printf("Updated list after deletion at end: ");
255 display_list(head);
256
257 // Deleting at a specific position
258 printf("Enter the position to delete a node: ");
259 scanf("%d", &position);
260 head = delete_at_position(head, position);
261 printf("Updated list after deletion at position %d: ", position);
262 display_list(head);
263
264
265 return 0;
266 }
Linked List\doubly_operation.c
1 // WAP to implement Doubly Linked List operation:
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 // doubly link list
6 // Definition of a node in the doubly linked list
7 struct node {
8 int data;
9 struct node* next;
10 struct node* prev;
11 };
12
13 // Function to insert a node at the beginning
14 struct node* insert_at_begin(struct node* head, int data) {
15 struct node* newNode = (struct node*)malloc(sizeof(struct node));
16 newNode->data = data;
17
18 if (head == NULL) {
19 newNode->next = NULL;
20 newNode->prev = NULL;
21 return newNode; // The list is empty
22 }
23
24 newNode->next = head; // Point next of new node to current head
25 head->prev = newNode; // Set previous of current head to new node
26 newNode->prev = NULL; // The new node is now the first node
27 return newNode; // Return the new head
28 }
29
30 // Function to insert a node at the end
31 struct node* insert_at_end(struct node* head, int data) {
32 struct node* newNode = (struct node*)malloc(sizeof(struct node));
33 newNode->data = data;
34 newNode->next = NULL; // New node will be the last node
35
36 if (head == NULL) {
37 newNode->prev = NULL; // The list is empty
38 return newNode; // Return new node as head
39 } else {
40 struct node* temp = head;
41 while (temp->next != NULL) {
42 temp = temp->next; // Traverse to the last node
43 }
44 temp->next = newNode; // Link the new node at the end
45 newNode->prev = temp; // Set the previous pointer of the new node
46 }
47
48 return head; // Return the head of the list
49 }
50
51 // Function to insert a node at a specific position
52 struct node* insert_at_position(struct node* head, int data, int position) {
53 struct node* newNode = (struct node*)malloc(sizeof(struct node));
54 newNode->data = data;
55
56 // If position is 1, insert at the beginning
57 if (position == 1) {
58 return insert_at_begin(head, data);
59 } else {
60 struct node* temp = head;
61 int i;
62
63 // Traverse to the node just before the specified position
64 for (i = 1; i < position - 1 && temp != NULL; i++) {
65 temp = temp->next;
66 }
67
68 // If position is out of bounds
69 if (temp == NULL) {
70 printf("Position out of bounds\n");
71 free(newNode); // Free the newly allocated memory
72 return head;
73 }
74
75 // Insert the new node at the specified position
76 newNode->next = temp->next; // Link new node to the next node
77 if (temp->next != NULL) {
78 temp->next->prev = newNode; // Set the previous pointer of the next node
79 }
80 temp->next = newNode; // Link the current node to the new node
81 newNode->prev = temp; // Set the previous pointer of the new node
82 }
83
84 return head;
85 }
86
87
88
89 // Function to delete a node at the beginning
90 struct node* delete_at_begin(struct node* head) {
91 if (head == NULL) {
92 printf("List is empty, nothing to delete.\n");
93 return head;
94 }
95 struct node* temp = head;
96 head = head->next;
97 if (head != NULL) {
98 head->prev = NULL; // Update the previous pointer of the new head
99 }
100 free(temp); // Free memory of the old head
101 return head;
102 }
103
104 // Function to delete a node at the end
105 struct node* delete_at_end(struct node* head) {
106 if (head == NULL) {
107 printf("List is empty, nothing to delete.\n");
108 return head;
109 }
110 if (head->next == NULL) {
111 free(head); // If there's only one node
112 return NULL;
113 }
114 struct node* temp = head;
115 while (temp->next != NULL) {
116 temp = temp->next; // Traverse to the last node
117 }
118 temp->prev->next = NULL; // Disconnect the last node
119 free(temp); // Free memory of the last node
120 return head;
121 }
122
123 // Function to delete a node at a specific position
124 struct node* delete_at_position(struct node* head, int position) {
125 if (head == NULL) {
126 printf("List is empty, nothing to delete.\n");
127 return head;
128 }
129 if (position == 1) {
130 return delete_at_begin(head); // Special case: delete at beginning
131 }
132 struct node* temp = head;
133 for (int i = 1; i < position && temp != NULL; i++) {
134 temp = temp->next; // Traverse to the specified position
135 }
136 if (temp == NULL) {
137 printf("Position out of bounds.\n");
138 return head;
139 }
140 if (temp->next != NULL) {
141 temp->next->prev = temp->prev; // Update the next node's previous pointer
142 }
143 if (temp->prev != NULL) {
144 temp->prev->next = temp->next; // Update the previous node's next pointer
145 }
146 free(temp); // Free memory of the node to delete
147 return head;
148 }
149
150
151 // Function to display the doubly linked list
152 void display_list(struct node* head) {
153 struct node* temp = head;
154 if (temp == NULL) {
155 printf("List is empty\n");
156 return;
157 }
158 printf("Elements in the list are: ");
159 while (temp != NULL) {
160 printf("%d <-> ", temp->data);
161 temp = temp->next;
162 }
163 printf("NULL\n");
164 }
165
166 // Main function to test the doubly linked list operations
167 int main() {
168 struct node* head = NULL;
169 int n, i, data;
170
171 // Initial input for nodes
172 printf("Enter number of initial nodes: ");
173 scanf("%d", &n);
174 for (i = 0; i < n; i++) {
175 printf("Enter data for node %d: ", i + 1);
176 scanf("%d", &data);
177
178 // Create a new node directly in the loop
179 struct node* newNode = (struct node*)malloc(sizeof(struct node));
180 newNode->data = data;
181 newNode->next = NULL;
182 newNode->prev = NULL;
183
184 // Insert the new node at the end of the list
185 if (head == NULL) {
186 head = newNode; // The list is empty
187 } else {
188 struct node* temp = head;
189 while (temp->next != NULL) {
190 temp = temp->next; // Traverse to the last node
191 }
192 temp->next = newNode; // Link the new node at the end
193 newNode->prev = temp; // Set the previous pointer of the new node
194 }
195 }
196
197 display_list(head); // Display the initial list
198
199 // Inserting at the beginning
200 printf("Enter the data to insert at beginning: ");
201 scanf("%d", &data);
202 head = insert_at_begin(head, data);
203 printf("Updated list after insertion at beginning: ");
204 display_list(head);
205
206 // Inserting at a specific position
207 int position;
208 printf("Enter the position to insert the data: ");
209 scanf("%d", &position);
210 printf("Enter the data to insert at position %d: ", position);
211 scanf("%d", &data);
212 head = insert_at_position(head, data, position);
213 printf("Updated list after insertion at position: ");
214 display_list(head);
215
216 // Inserting at the end
217 printf("Enter the data to insert at end: ");
218 scanf("%d", &data);
219 head = insert_at_end(head, data);
220 printf("Updated list after insertion at end: ");
221 display_list(head);
222
223
224
225 printf("Deleting at the beginning...\n");
226 head = delete_at_begin(head);
227 printf("Updated list after deletion at beginning: ");
228 display_list(head);
229
230 printf("Deleting at the end...\n");
231 head = delete_at_end(head);
232 printf("Updated list after deletion at end: ");
233 display_list(head);
234
235 printf("Enter the position to delete a node: ");
236 scanf("%d", &position);
237 head = delete_at_position(head, position);
238 printf("Updated list after deletion at position %d: ", position);
239 display_list(head);
240
241
242 return 0;
243 }
Linked List\singly_operation.c
1 // WAP to implement Singly Link List Operations:
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 // singly link list
6 struct node {
7 int data;
8 struct node* next;
9 };
10
11 // Function to insert a node at a specific position
12 struct node* insert_at_position(struct node* head, int data, int position) {
13 struct node* new_node = (struct node*)malloc(sizeof(struct node));
14 new_node->data = data;
15
16 // If position is 1, insert at the beginning
17 if (position == 1) {
18 new_node->next = head;
19 head = new_node;
20 return head;
21 } else {
22 struct node* temp = head;
23 int i;
24
25 // Traverse to the node just before the position
26 for (i = 1; i < position - 1 && temp != NULL; i++) {
27 temp = temp->next;
28 }
29
30 // If position is out of bounds (greater than the number of nodes)
31 if (temp == NULL) {
32 printf("Position out of bounds\n");
33 free(new_node); // Free the newly allocated memory
34 return head;
35 }
36
37 // Insert the new node at the specified position
38 new_node->next = temp->next;
39 temp->next = new_node;
40 }
41
42 return head;
43 }
44
45 // Function to insert a node at the end
46 struct node* insert_at_end(struct node* head, int data) {
47 struct node* new_node = (struct node*)malloc(sizeof(struct node));
48 new_node->data = data;
49 new_node->next = NULL;
50
51 if (head == NULL) {
52 return new_node;
53 } else {
54 struct node* temp = head;
55 while (temp->next != NULL) {
56 temp = temp->next;
57 }
58 temp->next = new_node;
59 }
60
61 return head; // Return the head pointer
62 }
63
64 // Function to insert a node at the beginning
65 struct node* insert_at_begin(struct node* head, int data) {
66 struct node* new_node = (struct node*)malloc(sizeof(struct node));
67 new_node->data = data;
68 new_node->next = head;
69 head = new_node;
70 return head;
71 }
72
73 // Function to delete a node at the beginning
74 struct node* delete_at_begin(struct node* head) {
75 if (head == NULL) {
76 printf("List is empty, nothing to delete.\n");
77 return head;
78 }
79 struct node* temp = head;
80 head = head->next;
81 free(temp);
82 return head;
83 }
84
85 // Function to delete a node at the end
86 struct node* delete_at_end(struct node* head) {
87 if (head == NULL) {
88 printf("List is empty, nothing to delete.\n");
89 return head;
90 }
91 if (head->next == NULL) {
92 free(head);
93 return NULL;
94 }
95 struct node* temp = head;
96 while (temp->next->next != NULL) {
97 temp = temp->next;
98 }
99 free(temp->next);
100 temp->next = NULL;
101 return head;
102 }
103
104 // Function to delete a node at a specific position
105 struct node* delete_at_position(struct node* head, int position) {
106 if (head == NULL) {
107 printf("List is empty, nothing to delete.\n");
108 return head;
109 }
110 if (position == 1) {
111 return delete_at_begin(head);
112 }
113 struct node* temp = head;
114 for (int i = 1; i < position - 1 && temp != NULL; i++) {
115 temp = temp->next;
116 }
117 if (temp == NULL || temp->next == NULL) {
118 printf("Position out of bounds.\n");
119 return head;
120 }
121 struct node* node_to_delete = temp->next;
122 temp->next = node_to_delete->next;
123 free(node_to_delete);
124 return head;
125 }
126
127
128 // Function to display the linked list
129 void display_list(struct node* head) {
130 struct node* temp = head;
131 if (temp == NULL) {
132 printf("List is empty\n");
133 } else {
134 printf("Elements in the list are: ");
135 while (temp != NULL) {
136 printf("%d -> ", temp->data);
137 temp = temp->next;
138 }
139 printf("NULL\n");
140 }
141 }
142
143 // Main function to test the linked list operations
144 int main() {
145 struct node* head = NULL, * new_node, * temp;
146 int n, i, data;
147
148 printf("Enter n: ");
149 scanf("%d", &n);
150 for (i = 0; i < n; i++) {
151 new_node = (struct node*)malloc(sizeof(struct node));
152 printf("Enter data for node %d: ", i + 1);
153 scanf("%d", &new_node->data);
154 new_node->next = NULL;
155 if (head == NULL) {
156 head = temp = new_node;
157 } else {
158 temp->next = new_node;
159 temp = new_node;
160 }
161 }
162
163 display_list(head);
164
165 printf("Enter the data to insert at begin: ");
166 scanf("%d", &data);
167 head = insert_at_begin(head, data);
168 printf("Updated list after insertion at beginning: ");
169 display_list(head);
170
171 int position;
172 printf("Enter the position to insert the data: ");
173 scanf("%d", &position);
174 printf("Enter the data to insert at position %d: ", position);
175 scanf("%d", &data);
176 head = insert_at_position(head, data, position);
177 printf("Updated list after insertion at position: ");
178 display_list(head);
179
180 printf("Enter the data to insert at end: ");
181 scanf("%d", &data);
182 head = insert_at_end(head, data);
183 printf("Updated list after insertion at end: ");
184 display_list(head);
185
186 printf("Deleting at the beginning...\n");
187 head = delete_at_begin(head);
188 printf("Updated list after deletion at beginning: ");
189 display_list(head);
190
191 printf("Deleting at the end...\n");
192 head = delete_at_end(head);
193 printf("Updated list after deletion at end: ");
194 display_list(head);
195
196 printf("Enter the position to delete a node: ");
197 scanf("%d", &position);
198 head = delete_at_position(head, position);
199 printf("Updated list after deletion at position %d: ", position);
200 display_list(head);
201
202
203 return 0;
204 }