diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-04 18:17:50 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-04 18:17:50 -0600 |
| commit | 146089585ecd2ef8abe5144cc28a74d2d84d9d10 (patch) | |
| tree | 394179c88563fe9ddd4be038740fab91da1cdbde /src/01/02b | |
| parent | 7c7704b20daef22c6c6fe79241a7d33e31e9e390 (diff) | |
Pretty print the doubly linked list
Diffstat (limited to 'src/01/02b')
| -rw-r--r-- | src/01/02b/README.md | 35 | ||||
| -rw-r--r-- | src/01/02b/doubly_linked_list.c | 13 | ||||
| -rw-r--r-- | src/01/02b/doubly_linked_list.h | 1 | ||||
| -rw-r--r-- | src/01/02b/main.c | 19 |
4 files changed, 62 insertions, 6 deletions
diff --git a/src/01/02b/README.md b/src/01/02b/README.md index 006c6bd..0731527 100644 --- a/src/01/02b/README.md +++ b/src/01/02b/README.md @@ -9,5 +9,40 @@ Swap two adjacent elements in a list by adjusting only the links (and not the da ## Description of the Code ## Errors and Warnings + +```bash +モ make run_test +mkdir build +clang -c -o build/doubly_linked_list.o doubly_linked_list.c +clang -c -o build/doubly_linked_list_test.o doubly_linked_list_test.c +clang build/doubly_linked_list.o build/doubly_linked_list_test.o -lcgreen -o build/test +Running "main" (22 tests)... + "swap_doubly_linked_list_tests": 164 passes in 7ms. + Completed "main": 164 passes in 7ms. +``` + ## Sample Input and Output + +The program defined in [`main.c`](./main.c) adds 10 randomly generated numbers to a +doubly linked list then swaps it pair of nodes from index 0 to the end of the list. + +```bash +モ make run +clang -c -o build/main.o main.c +clang build/doubly_linked_list.o build/main.o -o build/program +./build/program +=== COMP-272 - Assignment 1 - Question 2b === + [ (nil<83>86) (83<86>77) (86<77>15) (77<15>93) (15<93>35) (93<35>86) (35<86>92) (86<92>49) (92<49>21) (49<21>nil) ] +swap: 0,1 + [ (nil<86>83) (86<83>77) (83<77>15) (77<15>93) (15<93>35) (93<35>86) (35<86>92) (86<92>49) (92<49>21) (49<21>nil) ] +swap: 2,3 + [ (nil<86>83) (86<83>15) (83<15>77) (15<77>93) (77<93>35) (93<35>86) (35<86>92) (86<92>49) (92<49>21) (49<21>nil) ] +swap: 4,5 + [ (nil<86>83) (86<83>15) (83<15>77) (15<77>35) (77<35>93) (35<93>86) (93<86>92) (86<92>49) (92<49>21) (49<21>nil) ] +swap: 6,7 + [ (nil<86>83) (86<83>15) (83<15>77) (15<77>35) (77<35>93) (35<93>92) (93<92>86) (92<86>49) (86<49>21) (49<21>nil) ] +swap: 8,9 + [ (nil<86>83) (86<83>15) (83<15>77) (15<77>35) (77<35>93) (35<93>92) (93<92>86) (92<86>21) (86<21>49) (21<49>nil) ] +``` + ## Discussion diff --git a/src/01/02b/doubly_linked_list.c b/src/01/02b/doubly_linked_list.c index 88e49ee..4bb71b8 100644 --- a/src/01/02b/doubly_linked_list.c +++ b/src/01/02b/doubly_linked_list.c @@ -92,21 +92,22 @@ Node *reverse(Node *head) { static void print(Node *node) { if (node->prev && node->next) - printf("%d <- %d -> %d\n", node->prev->data, node->data, node->next->data); + printf("(%d<%d>%d)", node->prev->data, node->data, node->next->data); else if (node->next) - printf("nil <- %d -> %d\n", node->data, node->next->data); + printf("(nil<%d>%d)", node->data, node->next->data); else - printf("%d <- %d -> nil\n", node->prev->data, node->data); + printf("(%d<%d>nil)", node->prev->data, node->data); } -static void inspect(Node *node) { +void inspect(Node *node) { if (!node) return; - printf("*******\n"); + printf("[ "); while (node) { print(node); + printf(" "); node = node->next; } - printf("*******\n"); + printf("]\n"); } diff --git a/src/01/02b/doubly_linked_list.h b/src/01/02b/doubly_linked_list.h index 63470c4..5a44ae3 100644 --- a/src/01/02b/doubly_linked_list.h +++ b/src/01/02b/doubly_linked_list.h @@ -11,3 +11,4 @@ Node *add(Node *head, int data); Node *get(Node *from, int index); void swap(Node *x, Node *y); Node *reverse(Node *head); +void inspect(Node *node); diff --git a/src/01/02b/main.c b/src/01/02b/main.c index 8249019..10e18e3 100644 --- a/src/01/02b/main.c +++ b/src/01/02b/main.c @@ -10,6 +10,25 @@ int main(int argc, char *argv[]) { printf("=== COMP-272 - Assignment 1 - Question 2b ===\n"); Node *head = initialize(next()); + Node *new_head = NULL; + + for (int i = 0; i < 9; ++i) + add(head, next()); + + printf("\t"); + inspect(head); + + new_head = get(head, 1); + swap(head, new_head); + head = new_head; + printf("swap: 0,1\n\t"); + inspect(head); + + for (int i = 2; i < 10; i+=2) { + swap(get(head, i), get(head, i + 1)); + printf("swap: %d,%d\n\t", i, i + 1); + inspect(head); + } return 0; } |
