diff options
| author | mo khan <mo.khan@gmail.com> | 2020-07-04 17:21:13 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-07-04 17:21:13 -0600 |
| commit | 29aa26f5fdc7d53d5fbead6347f6962288039639 (patch) | |
| tree | 831d4ef10956c85569d873fbd11499ed37f21a35 /src | |
| parent | c9575a555c0557be93e6b461c19cf5592e389d26 (diff) | |
Add doxygen comments
Diffstat (limited to 'src')
| -rw-r--r-- | src/01/02a/README.md | 3 | ||||
| -rw-r--r-- | src/01/02a/singly_linked_list.c | 71 | ||||
| -rw-r--r-- | src/01/02a/singly_linked_list.h | 4 | ||||
| -rw-r--r-- | src/01/02b/README.md | 13 |
4 files changed, 75 insertions, 16 deletions
diff --git a/src/01/02a/README.md b/src/01/02a/README.md index 894c9eb..41a842c 100644 --- a/src/01/02a/README.md +++ b/src/01/02a/README.md @@ -4,6 +4,9 @@ Name: Mo Khan Student ID: 3431709 ## Problem Statement + +Swap two adjacent elements in a list by adjusting only the links (and not the data) using singly-linked list. + ## Description of the Code ## Errors and Warnings ## Sample Input and Output diff --git a/src/01/02a/singly_linked_list.c b/src/01/02a/singly_linked_list.c index c4f3bb0..e251b68 100644 --- a/src/01/02a/singly_linked_list.c +++ b/src/01/02a/singly_linked_list.c @@ -2,17 +2,12 @@ #include <stdio.h> #include <stdlib.h> -static void inspect(Node *node) { - if (!node) return; - - printf("*******\n"); - while (node) { - printf("\t%d\n", node->data); - node = node->next; - } - printf("*******\n"); -} - +/** + * The equivalent of a constructor to initalize a linked list. + * + * @param data The initial data to seed the linked list + * @return Returns a new Node + */ Node *initialize(int data) { Node *node = malloc(sizeof(Node)); node->data = data; @@ -20,6 +15,12 @@ Node *initialize(int data) { return node; } +/** + * Add a node to the tail of the linked list + * + * @param head The head of the linked list + * @param data The data to add to the linked list + */ Node *add(Node *head, int data) { Node *tail; Node *tmp = head; @@ -34,22 +35,43 @@ Node *add(Node *head, int data) { return tail->next; } -Node *get(Node *from, int index) { - if (!from || index < 0) return NULL; +/** + * Gets a specific node by index starting from index 0. + * + * @param self The Node to start from + * @param index The index of the node to return + * @return The node at the specific index + */ +Node *get(Node *self, int index) { + if (!self || index < 0) return NULL; - while(index > 0 && from){ - from = from->next; + while(index > 0 && self){ + self = self->next; index--; } - return from; + return self; } +/** + * Counts the number of items in the linked list + * + * @param head The head of the linked list + * @return The # of items in the linked list + */ static int size(Node *head) { int i = 0; for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++; return i; } +/** + * Swaps nodes in a linked list that are in positions + * x and y. + * + * @param head The head of the linked list + * @param x The node in position x + * @param y The node in position y + */ void swap(Node **head, int x, int y) { int count = size(*head); @@ -76,3 +98,20 @@ void swap(Node **head, int x, int y) { yc->next = xc->next; xc->next = tmp; } + +/** + * A helper method used to print a visual representation + * of a linked list + * + * @param node The head of the linked list + */ +void inspect(Node *node) { + if (!node) return; + + printf("*******\n"); + while (node) { + printf("\t%d\n", node->data); + node = node->next; + } + printf("*******\n"); +} diff --git a/src/01/02a/singly_linked_list.h b/src/01/02a/singly_linked_list.h index e240ce4..19dd3d7 100644 --- a/src/01/02a/singly_linked_list.h +++ b/src/01/02a/singly_linked_list.h @@ -1,3 +1,6 @@ +/** + * A node in a linked list. + */ struct node { int data; struct node *next; @@ -9,3 +12,4 @@ Node *initialize(int data); Node *get(Node *from, int index); Node *add(Node *head, int data); void swap(Node **head, int x, int y); +void inspect(Node *node); diff --git a/src/01/02b/README.md b/src/01/02b/README.md new file mode 100644 index 0000000..006c6bd --- /dev/null +++ b/src/01/02b/README.md @@ -0,0 +1,13 @@ +# Learning Profile for Assignment #1 - Question #2b - Computer Science 272: Data Structures and Algorithms + +Name: Mo Khan +Student ID: 3431709 + +## Problem Statement + +Swap two adjacent elements in a list by adjusting only the links (and not the data) using doubly-linked list. + +## Description of the Code +## Errors and Warnings +## Sample Input and Output +## Discussion |
