Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // delete the first node from a linked list containing a value argument
- // 25/5/2018
- // Lyall Beveridge & Khye Ean Lowe
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- struct node {
- struct node *next;
- int data;
- };
- struct node *delete_contains(int value, struct node *head);
- struct node *strings_to_list(int len, char *strings[]);
- void print_list(struct node *head);
- // DO NOT CHANGE THIS MAIN FUNCTION
- int main(int argc, char *argv[]) {
- if (argc < 2) {
- fprintf(stderr, "Usage: %s value list-elements\n", argv[0]);
- return 1;
- }
- int value = atoi(argv[1]);
- // create linked list from command line arguments
- struct node *head = strings_to_list(argc - 2, argv + 2);
- struct node *new_head = delete_contains(value, head);
- print_list(new_head);
- return 0;
- }
- // Delete the first node in the list containing the value `value`.
- // The deleted node is freed.
- // If no node contains `value`, the list is not changed.
- // The head of the list is returned.
- struct node *delete_contains(int value, struct node *head) {
- if (head == NULL) {
- return head;
- } else if (head->data == value) {
- struct node *after = head->next;
- free(head);
- if (after == NULL) {
- return NULL;
- } else {
- return after;
- }
- }
- struct node *current = head;
- struct node *previous = head;
- // break loop if the next value holds the correct data
- while (current->next != NULL && current->data != value) {
- previous = current;
- current = current->next;
- }
- // if loop was broken because there was the data in the next
- if (current->data == value) {
- // if the one after next is null, the current pointer will be the last
- if (current->next == NULL) {
- previous->next = NULL;
- } else {
- previous->next = current->next;
- }
- // in all cases the last value is not needed
- free(current);
- }
- return head;
- }
- // DO NOT CHANGE THIS FUNCTION
- // create linked list from array of strings
- struct node *strings_to_list(int len, char *strings[]) {
- struct node *head = NULL;
- for (int i = len - 1; i >= 0; i = i - 1) {
- struct node *n = malloc(sizeof (struct node));
- assert(n != NULL);
- n->next = head;
- n->data = atoi(strings[i]);
- head = n;
- }
- return head;
- }
- // DO NOT CHANGE THIS FUNCTION
- // print linked list
- void print_list(struct node *head) {
- printf("[");
- for (struct node *n = head; n != NULL; n = n->next) {
- // If you're getting an error here,
- // you have returned an invalid list
- printf("%d", n->data);
- if (n->next != NULL) {
- printf(", ");
- }
- }
- printf("]\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement