-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll.c
202 lines (158 loc) · 4.54 KB
/
ll.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
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
#include <stdio.h>
#include <stdlib.h>
#include "ll.h"
/* creates a basic linked list with default values */
struct node *build_new() {
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
head = (struct node *) malloc(sizeof(struct node));
second = (struct node *) malloc(sizeof(struct node));
third = (struct node *) malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return head;
}
/* appends a values to a linked list */
void lappend(struct node **list, int data) {
struct node *tmp_node = *list;
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
if (tmp_node == NULL) {
*list = new_node;
} else {
while (tmp_node->next != NULL) {
tmp_node = tmp_node->next;
}
}
tmp_node->next = new_node;
}
/* pushes a value onto a linked list */
void lpush(struct node **list, int data) {
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = *list;
*list = new_node;
}
/* inserts a value at the specified position of
* the linked list */
int linsert(struct node **list, int position, int data) {
struct node *tmp_node = NULL;
if (list == NULL && position == 0) {
tmp_node = *list;
tmp_node = (struct node *) malloc(sizeof(struct node));
tmp_node->data = data;
tmp_node->next = NULL;
return 0;
} else if (position == 0) {
lpush(list, data);
return 0;
}
int counter = 1;
struct node *new_node = (struct node *) malloc(sizeof(struct node));
for (tmp_node = *list; tmp_node != NULL; tmp_node = tmp_node->next, counter++) {
if (counter == position) {
new_node->data = data;
new_node->next = tmp_node->next;
tmp_node->next = new_node;
return 0;
}
}
return -1;
}
/* print out all the elements of the linked list */
void lprint(struct node *mylist) {
struct node *tmp_node = mylist;
while (tmp_node != NULL) {
printf("%d ", tmp_node->data);
tmp_node = tmp_node->next;
}
printf("\n");
}
/* get the length of the list */
int llen(struct node *list) {
struct node *tmp_node = NULL;
int counter = 0;
for (tmp_node = list; tmp_node != NULL; tmp_node = tmp_node->next) {
counter++;
}
return counter;
}
/* get the count of occurences of 'needle' */
int lcount(struct node *head, int needle) {
int counter = 0;
while (head != NULL) {
if (head->data == needle)
counter++;
head = head->next;
}
return counter;
}
/* get data at the specified position */
int lget(struct node *list, int index) {
struct node *tmp = list;
int counter = 0;
while (tmp != NULL) {
if (counter == index)
return tmp->data;
counter++;
tmp = tmp->next;
}
return -1;
}
/* deletes the entire linked list */
void ldelete(struct node **list) {
struct node *dummy_node;
while(*list != NULL) {
dummy_node = (*list)->next;
free(*list);
*list = dummy_node;
}
}
/* pops an element off the list */
int lpop(struct node **list) {
int data = (*list)->data;
struct node *tmp = (*list)->next;
free(*list);
*list = tmp;
return data;
}
/*an ordered insert*/
void lsorted_insert(struct node **list, struct node *elem) {
struct node *tmp_list;
struct node *prev_node = NULL;
struct node *elem_prev_node = NULL;
int elem_prev_set = 0;
int sorted = 0;
/*if the element was at the head*/
if (*list == elem) {
*list = (*list)->next;
elem_prev_set = 1;
}
for(tmp_list = *list; tmp_list != NULL; tmp_list = tmp_list->next) {
if (elem == tmp_list)
elem_prev_set = 1;
if (!elem_prev_set)
elem_prev_node = tmp_list;
if (elem->data < tmp_list->data) {
if (elem_prev_node != NULL)
elem_prev_node->next = elem_prev_node->next->next;
prev_node->next = elem;
elem->next = tmp_list;
sorted = 1;
}
prev_node = tmp_list;
}
/*if element was the largest on in the list*/
if (!sorted) {
if (elem_prev_node != NULL)
elem_prev_node->next = elem_prev_node->next->next;
prev_node->next = elem;
elem->next = NULL;
}
}