diff --git a/week2/Makefile b/week2/Makefile new file mode 100644 index 0000000..2c915a9 --- /dev/null +++ b/week2/Makefile @@ -0,0 +1,2 @@ +list: list.c list.h + gcc -o list list.c list.h diff --git a/week2/list.c b/week2/list.c new file mode 100644 index 0000000..6f3780c --- /dev/null +++ b/week2/list.c @@ -0,0 +1,178 @@ +/* + * Author: Walter + * Student ID: 1930006025 + * Week_2 + */ + +#include "list.h" +#include +#include +#include + +// Write all your functions here. +bool IsEmpty(Node *head) { + if (head == NULL) { + return true; + } else { + return false; + } +} + +/* code from pdf */ +Node *InsertNode(Node **phead, int index, double x) { + if (index < 0) + return NULL; + int currIndex = 1; + Node *currNode = *phead; + while (currNode != NULL && index > currIndex) { + currNode = currNode->next; + currIndex++; + } + if (index > 0 && currNode == NULL) + return NULL; + Node *newNode = (Node *)malloc(sizeof(Node)); + newNode->data = x; + if (index == 0) { + newNode->next = *phead; + *phead = newNode; + } else { + newNode->next = currNode->next; + currNode->next = newNode; + } + return newNode; +} + +int FindNode(Node *head, double x) { + int position; + Node *pnode; + + /* position start from 1 */ + for (pnode = head, position = 1; pnode; pnode = pnode->next, position++) { + if (pnode->data == x) { + return position; + } + } + + return 0; +} + +int DeleteNode(Node **phead, double x) { + /* position start from 1 */ + int position; + Node *pnode; + Node *delNode; + + /* if delete the first node */ + if ((*phead)->data == x) { + delNode = *phead; + *phead = (*phead)->next; + free(delNode); + return 1; + } + + /* if not the first node */ + for (pnode=*phead, position=2; pnode->next; pnode=pnode->next, position++) { + /* loop for pnode->next->data == x */ + if (pnode->next->data == x) { + /* delete node */ + delNode = pnode->next; + pnode->next = pnode->next->next; + free(delNode); + return position; + } + } + + /* if node not found, return 0 */ + return 0; +} + +void DisplayList(Node *head) { + Node *pnode; + /* loop for each node */ + for (pnode=head; pnode; pnode=pnode->next) { + printf("%f ", pnode->data); + } + /* new line at the end */ + printf("\n"); +} + +void DestroyList(Node *head) { + Node *pnode = head; + Node *delNode; + /* loop for each node */ + while (pnode) { + /* store the tmp node */ + delNode = pnode; + pnode = pnode->next; + free(delNode); + } +} + +// Testing all the functions: +int main(void) { + Node *head = NULL; + + // Output: true + printf("%s\n", IsEmpty(head) ? "true" : "false"); + + // Must output an empty line: + DisplayList(head); + + // Must output "insert failed": + Node *result = InsertNode(&head, 20, 7); + printf("%s\n", result == NULL ? "insert failed" : "insert succeeded"); + + // Output: 0.000000 1.000000 2.000000 3.000000 4.000000 + for (int i = 0; i < 5; i++) { + InsertNode(&head, i, i); + } + DisplayList(head); + + // Output: false + printf("%s\n", IsEmpty(head) ? "true" : "false"); + + // Output: 4.000000 3.000000 2.000000 1.000000 0.000000 + // 0.000000 1.000000 2.000000 3.000000 4.000000 + for (int i = 0; i < 5; i++) { + InsertNode(&head, 0, i); + } + DisplayList(head); + + // Output: 0.000000 is at position 5 + // 2.000000 is at position 3 + // 4.000000 is at position 1 + // 6.000000 is not in the list + for (int i = 0; i < 7; i += 2) { + int idx = FindNode(head, i); + if (idx > 0) { + printf("%f is at position %d\n", (double)i, idx); + } else { + printf("%f is not in the list\n", (double)i); + } + } + + // Output: inserted 10.000000 + // 4.000000 3.000000 2.000000 10.000000 1.000000 0.000000 + // 0.000000 1.000000 2.000000 3.000000 4.000000 + Node *in = InsertNode(&head, 3, 10); + printf("inserted %f\n", in->data); + DisplayList(head); + + // Output: 4.000000 3.000000 2.000000 10.000000 1.000000 + // 0.000000 1.000000 2.000000 3.000000 4.000000 + // 3.000000 2.000000 10.000000 1.000000 + // 0.000000 1.000000 2.000000 3.000000 4.000000 + // 3.000000 2.000000 10.000000 1.000000 + // 0.000000 1.000000 2.000000 3.000000 + DeleteNode(&head, 0); // Delete in the middle of the list. + DisplayList(head); + DeleteNode(&head, 4); // Delete at the front of the list. + DisplayList(head); + DeleteNode(&head, 4); // Delete at the end of the list. + DisplayList(head); + + DestroyList(head); + head = NULL; + + return 0; +} diff --git a/week2/list.h b/week2/list.h new file mode 100644 index 0000000..c1038e2 --- /dev/null +++ b/week2/list.h @@ -0,0 +1,47 @@ +#include + +typedef struct node { + double data; + struct node *next; +} Node; + +/* function: tests whether a list is empty +input: head: pointer to the first node +output: true if the list is empty and false otherwise +*/ +bool IsEmpty(Node *head); + +/* function: inserts a new node with certain data after a certain position +input: phead: pointer to the pointer to the first node +index: the new node is inserted after position index +insert a new node as the head if index is 0 +x: data of the new node +output: a pointer to the new node if insertion is successful, NULL otherwise +*/ +Node *InsertNode(Node **phead, int index, double x); + +/* function: finds node with certain data +input: head: pointer to the first node +x: the first node whose data = x is returned +output: returns the position of the first node whose data = x +returns 0 if no such node exists +*/ +int FindNode(Node *head, double x); + +/* function: deletes a node with certain data +input: phead: pointer to the pointer to the first node +x: the first node whose data = x is deleted +output: returns the position of the deleted node +returns 0 if no such node exists +*/ +int DeleteNode(Node **phead, double x); + +/* function: prints all the nodes in the list +input: head: pointer to the first node +*/ +void DisplayList(Node *head); + +/* function: deletes all the nodes in the list and frees their memory +input: head: pointer to the first node +*/ +void DestroyList(Node *head);