Created
June 2, 2020 17:24
-
-
Save ezy023/a331b9c980127cbc81e2219eeb494bbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "linked-list.h" | |
#define ASSERT_TRUE(cond) { \ | |
if (!(cond)) { \ | |
fprintf(stderr, "%s: " #cond " assertion failed\n", __func__); \ | |
} \ | |
} | |
#define ASSERT_EQUALS(a, b) { \ | |
if ((a) != (b)) { \ | |
fprintf(stderr, "%s: " #a " != " #b "\n", __func__); \ | |
} \ | |
} | |
void test_add() { | |
Node *list = NULL; | |
Node one = {.val = "one", .next = NULL }; | |
Node two = {.val = "two", .next = NULL }; | |
add(&list, &one); | |
add(&list, &two); | |
ASSERT_EQUALS(length(&list), 2); | |
ASSERT_EQUALS(list, &two); | |
} | |
void test_head() { | |
Node *list = NULL; | |
Node one = {.val = "one", .next = NULL}; | |
Node two = {.val = "two", .next = NULL}; | |
Node three = {.val = "three", .next = NULL}; | |
add(&list, &one); | |
add(&list, &two); | |
add(&list, &three); | |
Node *h = head(&list); | |
ASSERT_EQUALS(h, &three) | |
} | |
void test_tail() { | |
Node *list = NULL; | |
Node one = {.val = "one", .next = NULL}; | |
Node two = {.val = "two", .next = NULL}; | |
Node three = {.val = "three", .next = NULL}; | |
add(&list, &one); | |
add(&list, &two); | |
add(&list, &three); | |
Node *t = tail(&list); | |
ASSERT_EQUALS(t, &one) | |
} | |
void test_search() { | |
Node *list = NULL; | |
Node one = {.val = "one", .next = NULL}; | |
Node two = {.val = "two", .next = NULL}; | |
Node three = {.val = "three", .next = NULL}; | |
add(&list, &one); | |
add(&list, &two); | |
add(&list, &three); | |
char *query = "two"; | |
Node *r = search(&list, query); | |
ASSERT_EQUALS(r, &two) | |
} | |
void test_delete() { | |
Node *list = NULL; | |
Node one = {.val = "one", .next = NULL}; | |
Node two = {.val = "two", .next = NULL}; | |
Node three = {.val = "three", .next = NULL}; | |
add(&list, &one); | |
add(&list, &two); | |
add(&list, &three); | |
ASSERT_EQUALS(3, length(&list)); | |
ASSERT_EQUALS(three.next, &two); | |
delete(&list, &two); | |
ASSERT_EQUALS(2, length(&list)); | |
ASSERT_EQUALS(three.next, &one); | |
} | |
int main(void) { | |
test_add(); | |
test_head(); | |
test_tail(); | |
test_search(); | |
test_delete(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "linked-list.h" | |
void print_list(Node **list) { | |
Node *p; | |
int i = 1; | |
for(p = *list; p != NULL; p = p->next) { | |
printf("Item[%d] val: %s\n", i++, p->val); | |
} | |
} | |
void add(Node **list, Node *n) { | |
/* add to front of list */ | |
n->next = *list; | |
*list = n; | |
} | |
int length(Node **list) { | |
Node *p = *list; | |
int i = 0; | |
while(p) { | |
i++; | |
p = p->next; | |
} | |
return i; | |
} | |
Node *head(Node **list) { | |
return *list; | |
} | |
Node *tail(Node **list) { | |
Node *p = *list; | |
while(p->next != NULL) { | |
p = p->next; | |
} | |
return p; | |
} | |
void delete(Node **list, Node *n) { | |
Node *p; | |
for(p = *list; p != NULL; p = p->next) { | |
if (p->next == n) { | |
p->next = n->next; | |
return; | |
} | |
} | |
} | |
Node *search(Node **list, char *query) { | |
Node *p; | |
for(p = *list; p != NULL; p = p->next) { | |
if (strcmp(p->val, query) == 0) { | |
return p; | |
} | |
} | |
return NULL; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct Node { | |
char *val; | |
struct Node *next; | |
} Node; | |
void print_list(Node **list); | |
void add(Node **list, Node *n); | |
int length(Node **list); | |
Node *head(Node **list); | |
Node *tail(Node **list); | |
void delete(Node **list, Node *n); | |
Node *search(Node **list, char *query); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment