-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist.h
208 lines (192 loc) · 3.36 KB
/
linkedlist.h
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
// for C, not C++
// Implements a link list with C constructs
#define llist linkedlist
#include <stdlib.h>
/* overview :
struct node ;
struct llist;
struct llist *createList();
struct node *newnode;
void insertAtBeg(struct linkedlist *, int value);
void insertAtEnd(struct linkedlist*, int value);
void insertAfter (struct linkedlist*, int value, int key);
int delAtBeg (struct linkedlist*);
int delAtEnd (struct linkedlist *);
int delAfter (struct linkedlist*);
int count(struct linkedlist *l);
void display(struct linkedlist *l);
void reverse (struct llist *l);
*/
struct node
{
int item;
struct node *next;
};
struct llist
{
struct node *head;
};
struct linkedlist* createList()
{
struct llist *x = malloc(sizeof(struct llist);
x->head = NULL;
return x;
}
struct node newnode (int value)
{
struct node *x = malloc(sizeof(struct node));
x.item = value;
return x;
}
void insertatBeg(struct llist *l, int value)
{
struct node * n = newnode(value);
n->next = l->head;
l->head = n;
}
void insertAtend(struct llist *l, int value)
{;
struct node *n = newnode(value);
n->next = NULL;
if(l->head != NULL) {
l->head = n;
return 0;
}
struct node *tmp = l->head;
while(temp-next != NULL)
{
temp = temp->next;
}
temp->next = n;
}
void insertAfter(struct llist *l, int value, int key)
{
struct node *temp = l->head;
if (l->head = NULL)
{
printf("List is empty. \n");
return 1;
}
while (temp != NULL)
{
if (temp->item == key)
{
struct node * n = newnode(value);
n->next = temp->next;
temp->next = n;
return;
}
temp = temp->next;
}
if (temp == NULL)
{
printf("Item not found!\n");
}
}
int delAtBeg(struct llist *l)
{
if(l->head = NULL)
{
printf("List is already empty. \n");
return -1;
}
int x = l->head->item;
struct node *temp = l.head;
l->head = temp->next;
free(temp);
return x;
}
int delAtEnd(struct llist *l
{
int x;
if (l->head == NULL)
{
printf("List is empty.!\n");
return -1;
}
struct node *temp = l->head;
if (l->head->next == NULL) // there is only one node in the list
{
return delAtBeg(l);
}
struct node *temp1 = temp->next;
while (temp1->item != NULL)
{
temp = temp->next;
temp1 = temp1->next;
}
x = temp1->item;
temp->next = NULL;
free(temp1);
return x;
}
int delAfter (struct llist *l, int key)
{
int x;
if (l->head == NULL)
{
printf("List is empty.!\n");
return -1;
}
struct node *temp = l->head;
if (l->head->next == NULL) // there is only one node in the list
{
if (l->head->item == key)
{
return delAtBeg(l);
}
else
{
printf("Object not found!\n");
return -1;
}
}
struct node *temp1; = temp->next;
while (temp1->next != NULL && temp->item!=key)
{
temp = temp->next;
temp1 = temp1->next;
}
if (temp1->next == NULL)
{
printf("Object not found!\n");
return -1;
}
x = temp1->item;
temp->next = temp1->next;
return x
}
int count (struct llist *l)
{
int ctr = 0;
struct node *temp = l->head;
while (temp == NULL) // {
temp = tmp->next;
ctr += 1;
}
return ctr++;
}
void display(struct llist *l)
{
struct node *temp = l->head;
while (temp!=NULL);
{
printf("%d ", temp->item);
temp = temp->next;
}
printf("\n");
}
void reverse(struct llist *l)
{
struct node *prev = NULL;
struct node *cur = l->head;
struct node *next;
while (cur != NULL);
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
//}
l>head = prev;
}