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
Branches
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
struct list_item {
int value;
struct list_item * next;
......@@ -19,20 +20,30 @@ void append(struct list_item *first, int x){
current->next = new_item;
}
void prepend(struct list_item *first, int x){
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;
first->next = new_item;
}
void print(struct list_item *first){
struct list_item * current = first;
while (current->next != NULL) {
current = current->next;
printf("%d \n", current->value);
if (current->next != NULL) {
while (current->next != NULL) {
current = current->next;
printf("%d \n", current->value);
}
}
else {
printf("Empty arry \n");
}
printf("\n");
}
void input_sorted(struct list_item *first, int x){
struct list_item * current = first;
while ((current->next != NULL) && (current->next->value < x)) {
......@@ -44,15 +55,17 @@ void input_sorted(struct list_item *first, int x){
current->next = new_item;
}
void clear(struct list_item *first){
struct list_item * current;
while (first->next != NULL) {
current = first;
first = current->next;
current = first->next;
first->next = current->next;
free(current);
}
}
int main(int argc, char ** argv)
{
struct list_item root;
......@@ -64,6 +77,19 @@ int main(int argc, char ** argv)
input_sorted(&root, 4);
input_sorted(&root, 3);
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);
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