Challenge 1 + 2
This commit is contained in:
44
challenges/01-intro-challenges/c-env/linked_list.c
Normal file
44
challenges/01-intro-challenges/c-env/linked_list.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "malloc.h"
|
||||
#include "linked_list.h"
|
||||
|
||||
List* InitList() {
|
||||
List *new = (List *) malloc(sizeof(List));
|
||||
new->head = NULL;
|
||||
return new;
|
||||
}
|
||||
|
||||
int Insert(List *l, char *data) {
|
||||
Item *new = (Item *) malloc(sizeof(Item));
|
||||
node->data = malloc(strlen(string) + 1);
|
||||
strcpy(node->data, string);
|
||||
|
||||
if (l->head = NULL) {
|
||||
new->next = new;
|
||||
new->prev = new;
|
||||
l->head = new;
|
||||
return;
|
||||
} else {
|
||||
new->next = l->head;
|
||||
new->prev = l->head->prev;
|
||||
l->head->prev->next = new;
|
||||
l->head->prev = new;
|
||||
l->head = new;
|
||||
}
|
||||
}
|
||||
|
||||
Item* Find(List *l, char *data) {
|
||||
if (l->head == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Item *cur = l->head;
|
||||
do {
|
||||
if (strcmp(data, cur->data) == 0) {
|
||||
return cur;
|
||||
}
|
||||
|
||||
cur = cur->next;
|
||||
} while (cur != l->head);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user