Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The code below needs a couple things. The linked list needs to be a doubly linked list with pointers to next and previous. The vector
The code below needs a couple things. The linked list needs to be a doubly linked list with pointers to next and previous. The vector code needs an add() and expands() function. The linked list needs these functions: Insert functions (1 insert function that handles all 3 cases, which is insert to front, insert to back and insert after an inputted node) Remove functions (1 remove function that handles all 3 cases, remove from front, remove from back, remove after an inputted node) Traverse functions (front to back traversal, back to front traversal) Below is the linked list code #include#include #include int *expand(int *sarray, int *max, int *len) { int *temp; *max = *max * 2; temp = calloc(*max, sizeof(int)); for(int i = 0; i < *len; i++) { temp[i] = sarray[i]; } free(sarray); return temp; } int *add(int *carray, int *max, int *len, int item) { /* printf("Printing array "); for (int x = 0; x < *len; x++) { printf("%d ", carray[x]); } */ if (*len >= *max) { carray = expand(carray, &max, &len); } carray[*len] = item; *len = *len + 1; return carray; } int main() { int size = 5; int *array; array = calloc (size, sizeof(int)); //Initial array size 5 int curr = 0; srand(time(NULL)); printf("How many items to put in? "); int user = 0; scanf("%d", &user); for (int x = 0; x < user; x++) { array = add(array, &size, &curr, rand()%100); } for (int x = 0; x < curr; x++) { printf("%d ", array[x]); } return 0; }
Below is the vector's code #include#include #include struct node { int data; struct node *next; }; int main() { srand(time(NULL)); struct node *curr, *head; curr = NULL; head = NULL; for (int i = 0; i < 3; i++) { curr = malloc(sizeof(struct node)); curr->data = rand() % 100; curr -> next = head; head = curr; } curr = head; while(curr != NULL) { printf("%d ", curr->data); curr = curr->next; } curr = head->next; head->next = curr->next; curr->data = 0; curr->next = NULL; free(curr); curr = NULL; printf("PRINTING AFTER REMOVING MIDDLE ELEMENT "); curr = head; //Start at front while(curr != NULL) { printf("%d ", curr->data); curr = curr->next; } curr = head; while (curr != NULL) { curr = curr->next; head->data = 0; head->next = NULL; free(head); head = curr; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started