-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedlist.c
More file actions
110 lines (81 loc) · 1.35 KB
/
Linkedlist.c
File metadata and controls
110 lines (81 loc) · 1.35 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
#include <stdio.h>
#include <stdlib.h>
struct list_el
{
int val;
struct list_el * next;
}item_default={0,NULL}; //Default values
typedef struct list_el node;
node * head = NULL;
void insert_element(){
int a;
printf("Enter: ");
scanf("%d", &a);
node *linked = (node*)malloc(sizeof(node));
linked->val = a;
linked->next = head;
head = linked;
}
void delete_element(){
int a;
printf("Enter: ");
scanf("%d", &a);
node * curr = head;
node * curr1 = NULL;
//delete all matched values
while(curr != NULL ){
if (curr->val == a)
{
if (curr == head)
{
head = head->next;
free(curr);
curr = head;
/* code */
}
else{
curr1->next = curr->next;
free(curr);
curr = curr1->next;
}
}
else{
curr1 = curr;
curr = curr->next;
}
}
}
void display_elements(){
node * curr = head;
while (curr!=NULL )
{
printf(" %d -->", curr->val );
curr = curr->next;
}
printf("\n");
}
int main(int argc, char const *argv[])
{
int loop =1;
while(loop == 1)
{
int choice;
printf("Enter the choice 1-Insert, 2-Delete, 3-Display, 4-Stop\n");
scanf("%d",&choice);
switch(choice){
case 1:
insert_element();
break;
case 2:
delete_element();
break;
case 3:
display_elements();
break;
case 4:
loop = 0;
break;
}
}
return 0;
}