-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List.c
More file actions
132 lines (115 loc) · 2.84 KB
/
Copy pathLinked_List.c
File metadata and controls
132 lines (115 loc) · 2.84 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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node_type;
typedef struct
{
node_type *start;
} Linked_List;
void insert(node_type **start)
{
node_type *temp;
temp = (node_type *)malloc(sizeof(node_type));
if (temp == NULL)
printf("Memory Full\n");
else
{
//data input
int n;
printf("Enter the element : ");
scanf("%d", &n);
//inserting data in the node to be added
temp->data = n;
//inserting node in stack
temp->next = *start;
*start = temp;
}
}
void delete (node_type **start)
{
if (*start == NULL)
printf("LIST EMPTY");
else
{
int key;
printf("Enter the key : ");
scanf("%d", &key);
node_type *temp = *start, *follow = *start;
while (temp != NULL)
{
//if key is found
if (temp->data == key)
{
printf("KEY FOUND");
//if the first node is the key
if (temp == *start)
*start = (*start)->next;
else
follow->next = temp->next;
free(temp); //deleted node
return;
}
follow = temp;
temp = temp->next;
}
//when key not found
printf("KEY NOT FOUND!!");
}
}
void display(node_type *start)
{
if (start == NULL) //base condition
return;
else
{
//recursive case
display(start->next); //recursive call
printf("%d\t", start->data);
}
}
int main()
{
Linked_List linkedList;
//initializing default values of the Linked List
linkedList.start = NULL;
int run = 1; //for the loop to run forever till broke
while (run)
{
printf("Type :- \n");
printf("1) INSERT NODE\n");
printf("2) DELETE NODE\n");
printf("3) DISPLAY LIST\n");
printf("4) EXIT\n");
int ch;
printf("Enter the choice : ");
scanf("%d", &ch);
printf("\n");
switch (ch)
{
case 1:
insert(&(linkedList.start));
printf("\n");
break;
case 2:
delete (&(linkedList.start));
printf("\n\nNEW ");
case 3:
printf("LIST : ");
if (linkedList.start == NULL)
printf("EMPTY");
else
display(linkedList.start);
printf("\n\n");
break;
case 4:
run = 0;
break;
default:
printf("INVALID INPUT\n\n");
} //close of switch
} //close of switch
return 0;
} //close of main