Skip to content
Snippets Groups Projects
Commit c14de348 authored by Lars Öberg's avatar Lars Öberg
Browse files

Implemented All Functions, tested two

parent fccacc1e
No related branches found
No related tags found
No related merge requests found
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct list_item {
int value;
struct list_item * next;
};
void append(struct list_item *first, int x){
......@@ -7,54 +14,45 @@ void append(struct list_item *first, int x){
current = current->next;
}
struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item));
new_item.next = NULL;
new_item.value = x;
current.next = new_item;
new_item->next = NULL;
new_item->value = 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.value = x;
new_item->next = first;
new_item->value = x;
}
void print(struct list_item *first){
struct list_item * current = first;
while (current->next != NULL) {
current = current->next;
printf("%d \n", current.value);
printf("%d \n", current->value);
}
}
void input_sorted(struct list_item *first, int x){
struct list_item * current = first;
bool found = false;
while ((current->next != NULL) && found == false) {
if (current->next.value > x){
struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item));
new_item.next = current.next;
new_item.value = x;
current.next = new_item;
found = true;
}
while ((current->next != NULL) && (current->next->value < x)) {
current = current->next;
}
struct list_item * new_item = (struct list_item *) malloc(sizeof(struct list_item));
new_item->next = current->next;
new_item->value = x;
current->next = new_item;
}
void clear(struct list_item *first){
struct list_item * current;
while (first->next != NULL) {
current = first;
first = current.next;
first = current->next;
free(current);
}
}
struct list_item {
int value;
struct list_item * next;
};
int main(int argc, char ** argv)
{
struct list_item root;
......@@ -66,5 +64,6 @@ int main(int argc, char ** argv)
input_sorted(&root, 4);
input_sorted(&root, 3);
input_sorted(&root, 1);
print(&root);
print(&root);
return 0;
}
test 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