![]() We need to reverse the list in place by changing the references in a way that the next. Reading address on the stack returns 0 instead of 00400018 in MIPS. Our goal is to reverse the linked list with a given pointer to the head. OR I can make a copy of the list and then reverse it, but this uses additional O (n) memory. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant. But this involves changing the list which is bad. Examples might be simplified to improve reading and learning. Void bar(struct node *elem, struct node *sentinel)īar(elem->next, sentinel), printf("%d\n", elem->data) Īnd compiling with $ g++ -g -O3 -Wa,-alh test.cpp -o test. Im currently working on an assignment for class where we have to recursively find the sum of a linked list implemented in MIPS. How to read a singly linked list backwards Ask Question Asked 14 years ago Modified 6 years, 6 months ago Viewed 19k times 12 One method which I can think of is to reverse the list and then read it. Algorithm : Traverse the list and push all of its nodes onto a stack. You should not be checking for NULL in the while loop because your list is circular, so there should not be NULL. If you had intended to print it starting at the end, you should be starting at start->backlink, not at start. I have seen this related question, however I am specifically interested in solving it using recursion and TCO. The rest of your functions look reasonable but there are least two mistakes in your PrintBackwards function. If I change it to print forwards instead of backwards, the function call is properly eliminated. Another example where a value is returned is represented by the size() method. By inspecting the disassembly, I see that the function is being called and not jumped to. Reversing statements S1 and S2 will print the list in reverse order. ![]() However, I am having difficulty due to trying to print the current element after making the recursive call. But there is no way to traverse a singly linked list backwards. ![]() I didn't say there is no way to reverse a singly linked list - there obviously is. Conceptually this is the same as creating a new, reversed list. While ((line = (String) queue.It seems to me like it should be possible to print a circular linked list backwards in constant space and linear time using recursion and tail-call-optimization. You've added a new data structure (the stack), to which you copy the information in your linked list and read it out backwards. Gets the strings from the queue and prints them ![]() While ((line = reader.readLine()) != null) And, as far as I can tell, gcc -O3 does a tail-call optimization so the algorithm runs in constant space. The list is traversed in two passes: first to reverse it in-place, second to print the elements and reverse it again. ("Please invoke the program like this:") This function assumes that the end of the list is signaled by NULL. LinkedQueue queue = new LinkedQueue() // initialization The solution is included in the followingīufferedReader reader = null // reading buffer ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |