0% found this document useful (0 votes)
12 views6 pages

Linkedlist Final

Uploaded by

Amisha Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Linkedlist Final

Uploaded by

Amisha Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1 #include <stdio.

h>
2 #include <stdlib.h>
3
4 typedef struct Node
5 {
6 int data;
7 struct Node *next;
8 } Node;
9
10 Node *createNode(int data)
11 {
12 Node* temp = (Node*)malloc(sizeof(Node));
13 temp->data=data;
14 temp->next=NULL;
15 return temp;
16 }
17
18 Node* createList()
19 {
20 Node* head = NULL;
21 Node* tail = NULL;
22 int n, data,i;
23
24 printf("\n Enter the number of elements: ");
25 scanf("%d", &n);
26
27 for (i = 1; i <=n; i++)
28 {
29 printf("Enter element %d: ", i );
30 scanf("%d", &data);
31
32 Node* newNode = createNode(data);
33
34 if (head == NULL)
35 {
36 head = newNode;
37 tail = newNode;
38 }
39 else
40 {
41 tail->next = newNode;
42 tail = newNode;
43 }
44 }
45 return head;
46 }
47
48 void insertAtBeginning(Node** head, int data)
49 {
50 Node* newNode = createNode(data);
51 newNode->next = *head;
52 *head = newNode;
53 }
54
55 void insertAtEnd(Node** head, int data)
56 {
57 Node* newNode = createNode(data);
58 if (*head == NULL)
59 {
60 *head = newNode;
61 return;
62 }
63 Node *current = *head;
64 while (current->next != NULL)
65 {
66 current = current->next;
67 }
68 current->next = newNode;
69 }
70
71 void insertAtPosition(Node** head, int data, int position)
72 {
73 int i;
74 if (position == 1)
75 {
76 insertAtBeginning(head, data);
77 return;
78 }
79 Node *newNode = createNode(data);
80 Node *current = *head;
81 for (i = 1; i < position-1 && current != NULL; i++)
82 {
83 current = current->next;
84 }
85 if (current == NULL) {
86 printf("Position is out of range\n");
87 free(newNode);
88 return;
89 }
90 newNode->next = current->next;
91 current->next = newNode;
92 }
93
94 void deleteFromBeginning(Node** head)
95 {
96 if (*head == NULL)
97 {
98 printf("List is empty\n");
99 return;
100 }
101 Node* temp = *head;
102 *head = (*head)->next;
103 printf("\nDeleted element: %d\n", temp->data);
104 free(temp);
105 }
106
107 void deleteFromEnd(Node** head)
108 {
109 if (*head == NULL)
110 {
111 printf("List is empty\n");
112 return;
113 }
114 Node* current = *head;
115 if (current->next == NULL)
116 {
117 printf("\nDeleted element: %d\n", current->data);
118 free(current);
119 *head = NULL;
120 return;
121 }
122 while (current->next->next != NULL)
123 {
124 current = current->next;
125 }
126 Node* temp = current->next;
127 printf("\nDeleted element: %d\n", temp->data);
128 free(temp);
129 current->next = NULL;
130 }
131
132 void deleteElement(Node** head, int position)
133 {
134 if (*head == NULL)
135 {
136 printf("\nList is empty\n");
137 return;
138 }
139 Node* temp = *head;
140 if (position == 0)
141 {
142 *head = temp->next;
143 printf("\nDeleted element: %d\n", temp->data);
144 free(temp);
145 return;
146 }
147 Node* current = *head;
148 if (current->next == NULL)
149 {
150 printf("\nDeleted element: %d\n", current->data);
151 free(current);
152 *head = NULL;
153 return;
154 }
155 for (int i = 1; i < position - 1 && current->next != NULL; i++)
156 {
157 current = current->next;
158 }
159 if (current->next == NULL)
160 {
161 printf("\nPosition is out of range\n");
162 return;
163 }
164 temp = current->next;
165 printf("\nDeleted element: %d\n", temp->data);
166 current->next = temp->next;
167 free(temp);
168 }
169
170 int countNodes(Node* head)
171 {
172 int count = 0;
173 Node* current = head;
174 while (current != NULL)
175 {
176 count++;
177 current = current->next;
178 }
179 return count;
180 }
181
182 int searchElement(Node* head, int data)
183 {
184 int position = 0;
185 Node *current = head;
186 while (current != NULL) {
187 if (current->data == data) {
188 return position;
189 }
190 current = current->next;
191 position++;
192 }
193 return -1;
194 }
195
196 void reverseList(Node** head)
197 {
198 Node* prev = NULL;
199 Node* current = *head;
200 Node* next = NULL;
201 while (current != NULL)
202 {
203 next = current->next;
204 current->next = prev;
205 prev = current;
206 current = next;
207 }
208 *head = prev;
209 }
210
211 void displayList(Node* head)
212 {
213 printf("\n The list is : \n");
214 Node* current = head;
215 while (current != NULL)
216 {
217 printf("%d -> ", current->data);
218 current = current->next;
219 }
220 printf("NULL\n");
221 }
222
223 int main()
224 {
225 Node* head = NULL;
226 int ch, data, position;
227 while (1)
228 {
229 printf("\nMenu:\n");
230 printf("1. Create the list\n");
231 printf("2. Insert an element at the beginning\n");
232 printf("3. Insert an element at the end\n");
233 printf("4. Insert an element at a given position\n");
234 printf("5. Delete an element from the beginning\n");
235 printf("6. Delete an element from the end\n");
236 printf("7. Delete an element from a given position\n");
237 printf("8. Count the total number of nodes\n");
238 printf("9. Search for an element\n");
239 printf("10. Reverse the list\n");
240 printf("11. Display the list\n");
241 printf("12. Exit\n");
242 printf("\nEnter your choice: ");
243 scanf("%d", &ch);
244 switch (ch)
245 {
246 case 1:
247 head = createList();
248 break;
249 case 2:
250 printf("\nEnter the element to insert at the beginning: ");
251 scanf("%d", &data);
252 insertAtBeginning(&head, data);
253 break;
254 case 3:
255 printf("\nEnter the element to insert at the end: ");
256 scanf("%d", &data);
257 insertAtEnd(&head, data);
258 break;
259 case 4:
260 printf("Enter the element to insert: ");
261 scanf("%d", &data);
262 printf("Enter the position to insert: ");
263 scanf("%d", &position);
264 insertAtPosition(&head, data, position);
265 break;
266 case 5:
267 deleteFromBeginning(&head);
268 break;
269 case 6:
270 deleteFromEnd(&head);
271 break;
272 case 7:
273 printf("Enter the position to delete: ");
274 scanf("%d", &position);
275 deleteElement(&head, position);
276 break;
277 case 8:
278 printf("\nTotal number of nodes : %d\n", countNodes(head));
279 break;
280 case 9:
281 printf("\nEnter the element to search: ");
282 scanf("%d", &data);
283 position = searchElement(head, data);
284 if (position != -1) {
285 printf("\nElement found at position : %d\n", position+1);
286 } else {
287 printf("\nElement not found!!\n");
288 }
289 break;
290 case 10:
291 reverseList(&head);
292 printf("\nList is reversed\n");
293 break;
294 case 11:
295 displayList(head);
296 break;
297 case 12:
298 exit(0);
299 default:
300 printf("\nInvalid choice. Enter between 1 and 11. \n");
301 }
302 }
303 return 0;
304 }
305

You might also like