Skip to content
Snippets Groups Projects
Commit d66c0455 authored by Max Skanvik's avatar Max Skanvik
Browse files

finished part 3 with no memory leaks

parent c14de348
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
struct list_item { struct list_item {
int value; int value;
struct list_item * next; struct list_item * next;
...@@ -19,20 +20,30 @@ void append(struct list_item *first, int x){ ...@@ -19,20 +20,30 @@ void append(struct list_item *first, int x){
current->next = new_item; current->next = new_item;
} }
void prepend(struct list_item *first, int x){ void prepend(struct list_item *first, int x){
struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item)); struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item));
new_item->next = first; new_item->next = first->next;
new_item->value = x; new_item->value = x;
first->next = new_item;
} }
void print(struct list_item *first){ void print(struct list_item *first){
struct list_item * current = first; struct list_item * current = first;
while (current->next != NULL) { if (current->next != NULL) {
current = current->next; while (current->next != NULL) {
printf("%d \n", current->value); current = current->next;
printf("%d \n", current->value);
}
}
else {
printf("Empty arry \n");
} }
printf("\n");
} }
void input_sorted(struct list_item *first, int x){ void input_sorted(struct list_item *first, int x){
struct list_item * current = first; struct list_item * current = first;
while ((current->next != NULL) && (current->next->value < x)) { while ((current->next != NULL) && (current->next->value < x)) {
...@@ -44,15 +55,17 @@ void input_sorted(struct list_item *first, int x){ ...@@ -44,15 +55,17 @@ void input_sorted(struct list_item *first, int x){
current->next = new_item; current->next = new_item;
} }
void clear(struct list_item *first){ void clear(struct list_item *first){
struct list_item * current; struct list_item * current;
while (first->next != NULL) { while (first->next != NULL) {
current = first; current = first->next;
first = current->next; first->next = current->next;
free(current); free(current);
} }
} }
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
struct list_item root; struct list_item root;
...@@ -64,6 +77,19 @@ int main(int argc, char ** argv) ...@@ -64,6 +77,19 @@ int main(int argc, char ** argv)
input_sorted(&root, 4); input_sorted(&root, 4);
input_sorted(&root, 3); input_sorted(&root, 3);
input_sorted(&root, 1); input_sorted(&root, 1);
// print(&root);
append(&root, 16);
append(&root, 17);
append(&root, 16);
// print(&root);
prepend(&root, 22);
prepend(&root, 21);
prepend(&root, 20);
clear(&root);
print(&root); print(&root);
return 0; return 0;
} }
ll 0 → 100755
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment