summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-04 17:47:24 -0600
committermo khan <mo.khan@gmail.com>2020-07-04 17:47:24 -0600
commit8ef8f0e1f1e96f823a3cee027d65a34a3d515ba9 (patch)
tree1c8091ecbb6430d012e27232c1c565da17685b34 /src
parent29aa26f5fdc7d53d5fbead6347f6962288039639 (diff)
Add program output to the program profile
Diffstat (limited to 'src')
-rw-r--r--src/01/02a/README.md45
-rw-r--r--src/01/02a/main.c21
-rw-r--r--src/01/02a/singly_linked_list.c16
3 files changed, 74 insertions, 8 deletions
diff --git a/src/01/02a/README.md b/src/01/02a/README.md
index 41a842c..cf560de 100644
--- a/src/01/02a/README.md
+++ b/src/01/02a/README.md
@@ -8,6 +8,51 @@ Student ID: 3431709
Swap two adjacent elements in a list by adjusting only the links (and not the data) using singly-linked list.
## Description of the Code
+
+The singly linked list is represented by a `Node` struct.
+
+The `singly_linked_list.c` file contains the functions related to working
+with a `Node` struct. The `Node` struct represents a single node in a
+linked list.
+
## Errors and Warnings
+
+```bash
+モ make run_test
+mkdir build
+clang -c -o build/singly_linked_list.o singly_linked_list.c
+clang -c -o build/singly_linked_list_test.o singly_linked_list_test.c
+clang build/singly_linked_list.o build/singly_linked_list_test.o -lcgreen -o build/test
+Running "main" (13 tests)...
+ "swap_singly_linked_list_tests": 38 passes in 4ms.
+ Completed "main": 38 passes in 4ms.
+```
+
## Sample Input and Output
+
+The program defined in [`main`](./main.c) adds 10 randomly generated
+integers to a singly linked list then swaps each pair of items.
+
+```bash
+モ make run
+mkdir build
+clang -c -o build/singly_linked_list.o singly_linked_list.c
+clang -c -o build/main.o main.c
+clang build/singly_linked_list.o build/main.o -o build/program
+./build/program
+=== COMP-272 - Assignment 1 - Question 2a ===
+
+ [ 83 86 77 15 93 35 86 92 49 21 ]
+swap: 0,1
+ [ 86 83 77 15 93 35 86 92 49 21 ]
+swap: 2,3
+ [ 86 83 15 77 93 35 86 92 49 21 ]
+swap: 4,5
+ [ 86 83 15 77 35 93 86 92 49 21 ]
+swap: 6,7
+ [ 86 83 15 77 35 93 92 86 49 21 ]
+swap: 8,9
+ [ 86 83 15 77 35 93 92 86 21 49 ]
+```
+
## Discussion
diff --git a/src/01/02a/main.c b/src/01/02a/main.c
index d2dd2af..f566031 100644
--- a/src/01/02a/main.c
+++ b/src/01/02a/main.c
@@ -1,6 +1,27 @@
+#include "singly_linked_list.h"
#include <stdio.h>
+#include <stdlib.h>
+
+int next(void) {
+ return rand() % 100;
+}
int main(int argc, char *argv[])
{
+ printf("=== COMP-272 - Assignment 1 - Question 2a ===\n");
+ Node *head = initialize(next());
+
+ for (int i = 0; i < 9; i++)
+ add(head, next());
+
+ printf("\n\t");
+ inspect(head);
+
+ for (int i = 0; i < 10; i+=2){
+ swap(&head, i, i+1);
+ printf("swap: %d,%d\n\t", i, i+1);
+ inspect(head);
+ }
+
return 0;
}
diff --git a/src/01/02a/singly_linked_list.c b/src/01/02a/singly_linked_list.c
index e251b68..2271965 100644
--- a/src/01/02a/singly_linked_list.c
+++ b/src/01/02a/singly_linked_list.c
@@ -103,15 +103,15 @@ void swap(Node **head, int x, int y) {
* A helper method used to print a visual representation
* of a linked list
*
- * @param node The head of the linked list
+ * @param self The head of the linked list
*/
-void inspect(Node *node) {
- if (!node) return;
+void inspect(Node *self) {
+ if (!self) return;
- printf("*******\n");
- while (node) {
- printf("\t%d\n", node->data);
- node = node->next;
+ printf("[");
+ while(self) {
+ printf(" %d ", self->data);
+ self = self->next;
}
- printf("*******\n");
+ printf("]\n");
}