-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathll.c
258 lines (222 loc) · 7.12 KB
/
ll.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* Node Stucture */
typedef struct node_t {
int data;
struct node_t *next;
} Node;
/* Function Declarations */
Node * insert_top(int, Node *);
Node * insert_bottom(int, Node *);
Node * insert_after(int, int, Node *);
Node * insert_before(int, int, Node *);
void print(Node *);
int count(Node *);
/* Add a new node to the top of a list */
Node * insert_top(int num, Node *head) {
Node *new_node;
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= head;
head = new_node;
return head;
}
/* Add a new node to the bottom of a list */
Node * insert_bottom(int num, Node *head) {
Node *current_node = head;
Node *new_node;
while ( current_node != NULL && current_node->next != NULL) {
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= NULL;
if (current_node != NULL)
current_node->next = new_node;
else
head = new_node;
return head;
}
/* Add a new node after an element in the list */
Node * insert_after(int num, int prev_num, Node *head) {
Node *current_node = head;
Node *new_node;
while ( current_node->data != prev_num) {
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= current_node->next;
current_node->next = new_node;
return head;
}
/* Add a new node before an element in the list */
Node * insert_before(int num, int next_num, Node *head) {
Node *current_node = head;
Node *new_node;
while ( current_node->next->data != next_num) {
current_node = current_node->next;
}
new_node = (Node *) malloc(sizeof(Node));
new_node->data = num;
new_node->next= current_node->next;
current_node->next = new_node;
return head;
}
/* Print all the elements in the linked list */
void print(Node *head) {
Node *current_node = head;
while ( current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
}
/* Program main */
int main()
{
Node *head = NULL;
int num, prev_num, next_num;
int option;
char * temp;
char ch;
/* Display Menu */
while(1) {
printf("\n ***********************************\n");
printf("\n * Linked list operations: *\n");
printf("\n * 1. Insert at the top of list *\n");
printf("\n * 2. Insert at bottom of list *\n");
printf("\n * 3. Insert after an element *\n");
printf("\n * 4. Insert before an element *\n");
printf("\n * 5. Show all elements *\n");
printf("\n * 6. Quit *\n");
printf("\n ***********************************\n");
printf("\n Choose an option [1-5] : ");
if (scanf("%d", &option) != 1) {
printf(" *Error: Invalid input. Try again.\n");
scanf("%s", &temp); /*clear input buffer */
continue;
}
switch (option) {
case 1: /* Add to top*/
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp); /*clear input buffer */
continue;
}
head = insert_top(num, head);
printf("Number %d added to the top of the list", num);
printf("\nPress any key to continue...");
getch();
break;
case 2: /* add to bottom */
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input. \n");
scanf("%s", &temp);
continue;
}
head = insert_bottom(num, head);
printf("%d added to the bottom of the list", num);
printf("\nPress any key to continue...");
getch();
break;
case 3: /* Insert After */
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}
printf(" After which number do you want to insert : ");
if (scanf("%d", &prev_num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}
if (head != NULL) {
head = insert_after(num, prev_num, head);
printf("%d is inserted after %d", num, prev_num);
}else {
printf("The list is empty", num, prev_num);
}
printf("\nPress any key to continue...");
getch();
break;
case 4: /* Insert Before */
printf(" Enter a number to insert : ");
if (scanf("%d", &num) != 1) {
printf(" *Error: Invalid input. \n");
scanf("%s", &temp);
continue;
}
printf(" Before which number do you want to insert : ");
if (scanf("%d", &prev_num) != 1) {
printf(" *Error: Invalid input.\n");
scanf("%s", &temp);
continue;
}
if (head != NULL) {
head = insert_before(num, prev_num, head);
printf("Number %d inserted before %d", num, prev_num);
}else {
printf("The list is empty", num, prev_num);
}
printf("\nPress any key to continue...");
getch();
break;
case 5: /* Show all elements */
printf("\nElements in the list: \n [ ");
print(head);
printf("]\n\nPress any key to continue...");
getch();
break;
case 6: /* Exit */
return(0);
break;
default:
printf("Invalid Option. Please Try again.");
getch();
} /* End of Switch */
} /* End of While */
return(0);
}
Sample Output
************************************
* Linked list operations: *
* 1. Insert at the top of list *
* 2. Insert at bottom of list *
* 3. Insert after an element *
* 4. Insert before an element *
* 5. Show all elements *
* 6. Quit *
************************************
Choose an option [1-5] : 4
Enter a number to insert : 4
Before which number do you want to insert : 5
Number 4 is now inserted before 5 in the list
Press any key to continue...
Rate it
Print
fb
reddit
tweet
linkedin
Related Articles
C program to sort array elements using Bubble sort
How to reverse a string in C
C program to convert hexadecimal to decimal
C program to count lines, words and characters in a file
How to check a number is prime or not in C
Post a comment
Comments
nikhi | March 24, 2019 5:31 PM |
by compiling this there is arising an error: invalid type argument of ‘->’ (have ‘int’),what is meant by this.....
Suhetu | August 22, 2018 6:08 AM |
Bro, it's picking up garbage values when I want to show all the elements.
rizwana m | August 17, 2018 7:59 AM |
you to enter input for this program
Remy | December 13, 2017 10:55 AM |
thanks you so much
Copyright 2022 Open Tech Guides. All rights reserved