-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
164 lines (144 loc) · 3.43 KB
/
linked_list.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node Node;
struct node
{
int n;
int data;
Node* next;
};
Node* createList(){ // function to create a list
Node* list;
Node* head;
head = (Node*) malloc(sizeof(Node));
list = head;
head->next = NULL;
return list;
}
void insertNode (Node* list,Node* extra,int n){ //function to insert a node a given n or to start a list
Node* temp = list->next;
int z = 0;
if (temp == NULL)
{
list->next = extra;
extra->next = NULL;
z = 2;
}
while (temp != NULL && z ==0)
{
if (temp->n == n ){}
{
extra->next = temp->next;
temp->next = extra;
z = 1;
}
temp = temp->next;
}
if (z == 0)
{
printf("node not found\n");
}
else if (z == 1)
{
printf("node inserted successfully\n");
}
else if (z == 2)
{
printf("list started successfully with its first node\n");
}
}
int isEmpty(Node* list){ // returns 0 if list is empty otherwise returns 1
if (list->next == NULL)
{
return 0;
}else
{
return 1;
}
}
Node* findbyn(Node* list ,int n){ // to find node by n
Node* temp = list->next;
int z = 0;
while (temp != NULL)
{
if (temp->n == n)
{
return temp;
}
temp = temp->next;
}
if (z == 0)
{
printf("node not found\n");
}
return NULL;
}
Node* findbydata(Node* list ,int data){ // to find node by data
Node* temp = list->next;
int z = 0;
while (temp != NULL)
{
if (temp->data == data)
{
return temp;
}
temp = temp->next;
}
if (z == 0)
{
printf("node not found\n");
}
return NULL;
}
void deleteNode(Node* list ,int n){ //to delete node
Node* temp = list->next;
Node* currentNode = list;
while (temp != NULL)
{
if (temp->n == n)
{
currentNode->next = temp->next;
free(temp);
temp = NULL;
break;
}
currentNode = temp;
temp = temp->next;
}
}
void printList(Node* list){ //to print list
Node* temp = list->next;
while (temp != NULL)
{
printf("\n n = %d \n data = %d \n",temp->n,temp->data);
temp = temp->next;
}
}
Node* makeNode(int n, int data){ // to make a node element
Node* temp = (Node*) malloc(sizeof(Node));
temp->n = n;
temp->data = data;
return temp;
}
int main(){
Node* devu = createList();
int n = isEmpty(devu);
printf("%d\n",n);
Node*first;
first = (Node*) malloc(sizeof(Node));
first->n = 1;
first->data = 69;
Node* second = makeNode(3,70);
insertNode(devu,first,0);
insertNode(devu,second,1);
n = isEmpty(devu);
printf("%d\n",n);
if (n)
{
printList(devu);
}
deleteNode(devu,1);
printList(devu);
Node* desh = findbyn(devu,1);
desh = findbydata(devu,90);
}