#include #include #include #include typedef struct list_node list_node; typedef list_node *linked_list; struct list_node { int data; list_node *next; }; /* * return 1 iff list contains no elements, 0 otherwise */ int empty(linked_list list) { return list == NULL; } /* * create a list_node and place the specified values in the fields */ linked_list create_node(int data, linked_list next) { list_node *n; n = malloc(sizeof (list_node)); if (n == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } n->data = data; n->next = next; return n; } /* * insert integer at front of list */ linked_list insert(int value, linked_list list) { return create_node(value, list); } /* * return pointer to last node in list * NULL is returned if list is empty */ list_node * last(linked_list list) { list_node *n = list; if (n == NULL) return NULL; while (n->next != NULL) n = n->next; return n; } /* * append integer to end of list */ linked_list append(int value, linked_list list) { list_node *n; n = create_node(value, NULL); /* will be last node in list */ if (empty(list)) { return n; /* new node is now head of the list */ } else { last(list)->next = n; /* append node to list */ return list; } } /* * find previous node to first node containing specified int */ list_node * find_previous(int i, linked_list list) { list_node *previous = NULL; list_node *x; for (x = list; x != NULL; x = x->next) { if (x->data == i) return previous; previous = x; } return NULL; } /* * delete first node containing specified int */ linked_list delete(int i, linked_list list) { list_node *deleted = NULL; list_node *previous = find_previous(i, list); if (previous != NULL) { deleted = previous->next; previous->next = previous->next->next; } else if (list != NULL && list->data == i) { deleted = list; list = list->next; } if (deleted != NULL) free(deleted); return list; } /* * print contents of list in Haskell syntax */ void print_list(linked_list list) { list_node *n; printf("["); for (n = list; n != NULL; n = n->next) { printf("%d", n->data); if (n->next != NULL) printf(", "); } printf("]"); } /* * return number of nodes in list */ int length(linked_list list) { /* your code goes here */ return 0; } /* * return nth node in list, counting from 0 * 0 is returned and an error message printed * if the list doesn't have an nth node */ int nth(int index, linked_list list) { /* your code goes here */ return 0; } /* * reverse the nodes in list */ linked_list reverse(linked_list list) { /* your code goes here */ return NULL; } enum { MAX_LINE = 1024 }; /* * test the above functions */ int main(int argc, char *argv[]) { linked_list list; int argument; char *s, *t; char line[MAX_LINE]; char action[MAX_LINE]; list = NULL; while (1) { printf("list = "); print_list(list); printf("\n"); printf("> "); if (fgets(line, MAX_LINE, stdin) == NULL) break; for (s = line; isspace(*s); s++) ; t = action; while (isalpha(*s)) *t++ = *s++; *t = '\0'; argument = strtol(s, &s, 10); if (strcmp(action, "quit") == 0) break; else if (strcmp(action, "") == 0) continue; else if (strcmp(action, "insert") == 0) list = insert(argument, list); else if (strcmp(action, "append") == 0) list = append(argument, list); else if (strcmp(action, "delete") == 0) list = delete(argument, list); else if (strcmp(action, "length") == 0) printf("%d\n", length(list)); else if (strcmp(action, "nth") == 0) printf("%d\n", nth(argument, list)); else if (strcmp(action, "reverse") == 0) list = reverse(list); else printf("Unknown command: '%s'\n", action); } return 0; }