This is an extension in relation to Programming Exercise 2 in Chapter 18, howeve
ID: 3638953 • Letter: T
Question
This is an extension in relation to Programming Exercise 2 in Chapter 18, however I already have that program. These are the extras I need to add also, which is not part of the book. Any help with this would be appreciated because we were crunched for time in class, and the lecture was run through pretty quickly.* Delete duplicate nodes in a list. That is, check the value and if that value already occurs in the linked list, delete the extra one. The way to do this would be for each node of a list, check whether any other node has the same data value, and if so delete the extra one. In pseudo-code:
for p points to each node of a list
for p2 each node in the list from p til the end of the list
if nodes p and p2 have the same value data, delete node p2
* Check whether the list is equal to another linked list which is passed as an argument. Return a bool, true if they are equal false if they are not. "Equal" means that the value in each node is equivalent. So for example, 2-> 3-> 4 is equal to 2->3->4 but not equal to 2->3 nor to 3->4->2.
Explanation / Answer
Delete duplicate node: r = first; while ( r != nil ) { target = (*r).info; q = r; p = (*q).next; while ( ((*p).next != nil) ) { if ( target == (*p).info ) { (*q).next = (*p).next; } else { q = p; p = (*p).next; }//end if }//end while r = (*r).next; } Equality check: int compareLinkedList(struct Node *node1, struct Node *node2) { static int flag; /* both lists are NULL */ if(node1 == NULL && node2 == NULL) { flag = 1; } else { if(node1 == NULL || node2 == NULL) flag = 0; else if(node1->info != node2->info) flag = 0; else compareLinkedList(node1->next, node2->next); } return flag; } if function returns 1 linked lists are equal else not equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.