Week 3 Honors Assignment - Doubly Linked List
Since the assignment has no upload button for the code file, I'm sharing the code through this link.unknown
c_cpp
3 years ago
3.4 kB
4
Indexable
/* Modify the singly linked list to be a doubly linked list. Now write a routine that removes all duplicate data in the doubly linked list. The data will be integers generated at random from [0,49]. Initially have a list of 200 elements. */ #include <stdlib.h> #include <stdio.h> #include <time.h> // This give us the time for generating random variables. #define ARRAY_SIZE 200 #define RAND_MAX 2147483647 // Define the max number in the rand() function. #define RANDOM_NUMBER rand() % 50 // Generate a random number from [0,49]. // Function for filling the array with random integers. #define FILL for (int j = 0; j < ARRAY_SIZE; j++) data[j] = RANDOM_NUMBER // Defining each list data node with a struct. typedef struct list { int data; struct list *next; struct list *prev; } list; // Funtion for creating the list. list *create_list(int d) { list *head = malloc(sizeof(list)); head->data = d; head->next = NULL; head->prev = NULL; return head; } // Function for adding a new element to the list. list *add_to_front(int d, list *h) { list *head = create_list(d); h->prev = head; head->next = h; return head; } // Function for removing a node from the list. void remove_from_list(list *node) { if (node == NULL) return; if (node->prev == NULL) { node->next->prev = NULL; free(node); return; } if (node->next == NULL) { node->prev->next = NULL; free(node); return; } node->next->prev = node->prev; node->prev->next = node->next; free(node); } // Function for removing all duplicates values from the list. void remove_duplicates(list *h) { while (h != NULL) { list *n = h->next; while(n != NULL) { list *temp = n->next; if (h->data == n->data) remove_from_list(n); n = temp; } h = h->next; } } // Function for creating a list from an array. list *array_to_list(int d[], int size) { list *head = create_list(d[0]); for (int i = 1; i < size; i++) { head = add_to_front(d[i], head); } return head; } // Function for printing the list in rows of 5. void print_list(list *h) { int i = 0; while (h != NULL) { printf("%d\t", h->data); h = h->next; i++; if (i % 5 == 0) printf("\n"); } printf("\n"); } // Function for freeing the memory used for the linked list creation. void free_list(list *h) { while (h != NULL) { list *temp = h; h = h->next; free(temp); } } // Here we define the program to create a list, remove duplicates, and print it. int main(void) { // Seeding the random generator with the time. srand(time(NULL)); // Defining the array. int data[ARRAY_SIZE]; // Filling the array with the random integers. FILL; // Creating a list from the array. list *new_list = array_to_list(data, ARRAY_SIZE); // Printing the old list. printf("Old list:\n"); print_list(new_list); // Remove duplicates from the list. remove_duplicates(new_list); // Printing the new list. printf("New list:\n"); print_list(new_list); // Freeing the memory. free_list(new_list); return 0; }
Editor is loading...