-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink-list-with-3-ins-hard.c
More file actions
151 lines (142 loc) · 3.38 KB
/
link-list-with-3-ins-hard.c
File metadata and controls
151 lines (142 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Program to create a link list and give options to insert data at the beginning, at any position or at the end - HARD*/
/* Accept the data in the main part of the program */
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void insertAtBeginning(struct node **head, int val) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode -> data = val;
newNode -> next = *head;
*head = newNode;
}
void insertAtAnyPos(struct node **head, int val, int pos) {
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
newNode -> data = val;
if(*head == NULL || pos == 0) {
newNode -> next = *head;
*head = newNode;
} else {
temp = *head;
for (int i = 1; i < pos-1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position. Element cannot be inserted.\n");
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
}
void insertAtEnd(struct node **head, int val) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printValue(struct node *head) {
struct node *temp = head;
while (temp != NULL) {
printf("%d | ", temp->data);
temp = temp->next;
}
}
int main() {
int op, pos, val;
struct node *head = NULL;
while (1) {
printf("Enter the value to be inserted: ");
scanf("%d", &val);
printf("Choose an option:\n1. Insert at the beginning\n2. Insert at any position\n3. Insert at the end\n4. Exit\nOption: ");
scanf("%d", &op);
switch(op){
case 1:
insertAtBeginning(&head, val);
break;
case 2:
printf("Enter the position where you want to insert: ");
scanf("%d", &pos);
insertAtAnyPos(&head, val, pos);
break;
case 3:
insertAtEnd(&head, val);
break;
case 4:
printf("Exiting the program....\n");
exit(0);
default:
printf("Invalid option. Please choose a valid option.\n");
break;
}
printf("Updated Linked List: ");
printValue(head);
printf("\n");
}
return 0;
}
// Output:
/*
Enter the value to be inserted: 21
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 1
Updated Linked List: 21 |
Enter the value to be inserted: 6
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 1
Updated Linked List: 6 | 21 |
Enter the value to be inserted: 78
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 3
Updated Linked List: 6 | 21 | 78 |
Enter the value to be inserted: 67
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 2
Enter the position where you want to insert: 3
Updated Linked List: 6 | 21 | 67 | 78 |
Enter the value to be inserted: 21
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 2
Enter the position where you want to insert: 10
Invalid position. Element cannot be inserted.
Updated Linked List: 6 | 21 | 67 | 78 |
Enter the value to be inserted: 21
Choose an option:
1. Insert at the beginning
2. Insert at any position
3. Insert at the end
4. Exit
Option: 4
Exiting the program....
*/