C programming complete the code for the removeItem() function in the program bel
ID: 3831890 • Letter: C
Question
C programming
complete the code for the removeItem() function in the program below. Be sure to update both the prev and next pointers where necessary.
#include <stdio.h>
struct dLink {
struct dLink *prev;
int value;
struct dLink *next;
};
// print the linked list
void printList(struct dLink *head) {
struct dLink *ptr = head;
printf("head pointer: %p ", head);
while (ptr != NULL) {
printf("item address: %p ", ptr);
printf("prev pointer: %p ", ptr->prev);
printf(" value: %d ", ptr->value);
printf("next pointer: %p ", ptr->next);
ptr = ptr->next;
}
}
// append a new entry to the end of the doubly linked list
struct dLink * appendEntry(struct dLink *head, struct dLink *newItem) {
// empty list, the new item becomes the head
if (head == NULL) {
head = newItem;
}
// non-empty list
else {
struct dLink *ptr = head;
// find the last item in the list
while(ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = newItem; // update ptr's next pointer
newItem->prev = ptr; // update newItems's prev pointer
}
return head;
}
// Problem 1: insert the new item into the linked list after the item pointed to by afterItem
struct dLink * insertEntryAfter(struct dLink *head, struct dLink *afterItem, struct dLink *newItem) {
// TODO
return head;
}
// Problem 2: insert the new item into the linked list before the item pointed to by beforeItem
struct dLink * insertEntryBefore(struct dLink *head, struct dLink *beforeItem, struct dLink *newItem) {
// TODO
return head;
}
// Problem 3: remove the item pointed to by removeItem from the linked list
struct dLink * removeItem(struct dLink *head, struct dLink *removeItem) {
// TODO
return head;
}
int main(void) {
struct dLink *head = NULL;
struct dLink a = {NULL, 100, NULL};
head = appendEntry(head, &a);
struct dLink b = {NULL, 200, NULL};
head = appendEntry(head, &b);
struct dLink c = {NULL, 300, NULL};
head = appendEntry(head, &c);
struct dLink d = {NULL, 400, NULL};
head = appendEntry(head, &d);
struct dLink e = {NULL, 500, NULL};
head = appendEntry(head, &e);
// test solution to Problem 3
head = removeItem(head, &a);
// test solution to Problem 3
head = removeItem(head, &c);
// test solution to Problem 3
head = removeItem(head, &e);
printList(head);
return 0;
}
Explanation / Answer
#include <stdio.h>
struct dLink {
struct dLink *prev;
int value;
struct dLink *next;
};
// print the linked list
void printList(struct dLink *head) {
struct dLink *ptr = head;
printf("head pointer: %p ", head);
while (ptr != NULL) {
printf("item address: %p ", ptr);
printf("prev pointer: %p ", ptr->prev);
printf(" value: %d ", ptr->value);
printf("next pointer: %p ", ptr->next);
ptr = ptr->next;
}
}
// append a new entry to the end of the doubly linked list
struct dLink * appendEntry(struct dLink *head, struct dLink *newItem) {
// empty list, the new item becomes the head
if (head == NULL) {
head = newItem;
}
// non-empty list
else {
struct dLink *ptr = head;
// find the last item in the list
while(ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = newItem; // update ptr's next pointer
newItem->prev = ptr; // update newItems's prev pointer
}
return head;
}
struct dLink * insertEntryAfter(struct dLink *head, struct dLink *afterItem, struct dLink *newItem) {
return head;
}
struct dLink * insertEntryBefore(struct dLink *head, struct dLink *beforeItem, struct dLink *newItem)
{
return head;
}
struct dLink * removeItem(struct dLink *head, struct dLink *removeItem) { return head;
}
int main(void) {
struct dLink *head = NULL;
struct dLink a = {NULL, 100, NULL};
head = appendEntry(head, &a);
struct dLink b = {NULL, 200, NULL};
head = appendEntry(head, &b);
struct dLink c = {NULL, 300, NULL};
head = appendEntry(head, &c);
struct dLink d = {NULL, 400, NULL};
head = appendEntry(head, &d);
struct dLink e = {NULL, 500, NULL};
head = appendEntry(head, &e);
head = removeItem(head, &a);
head = removeItem(head, &c);
head = removeItem(head, &e);
printList(head);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.