Modify the LinkedList1 presented in this chapter by adding sort() and reverse()
ID: 3621837 • Letter: M
Question
Modify the LinkedList1 presented in this chapter by adding sort() and reverse() methods.The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods.
Explanation / Answer
struct node { int data; struct node *link; }; void reverse() { struct node *p = first, *q = NULL, *r; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } q = first; }static void reverse(struct node** head_ref) { struct node* prev = NULL; struct node* current = *head_ref; struct node* link; while (current != NULL) { link = current->link; current->link = prev; prev = current; current = linkt; } *head_ref = prev; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.