Linked Lists and what's pointing to what and when

  • Thread starter Thread starter John O' Meara
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers on the implementation and behavior of a linked list reversal function, specifically the method reverseM() within a class intList. Participants explore the mechanics of pointer manipulation during the reversal process, addressing concerns about pointer overwriting and the sequence of operations. The scope includes technical explanations and code-related inquiries.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions how the local pointers 'front' could overwrite the 'this' pointer, potentially losing the new head of the reversed linked list.
  • Another participant explains that reverseM() calls itself recursively until it reaches the end of the list, detailing how the 'this' pointer and 'prev' parameter are used during these calls.
  • A participant suggests using a source level debugger to trace the execution of the code to better understand the behavior of the pointers.
  • One participant shares an alternative main function to facilitate testing without manual input, demonstrating how to create and reverse a linked list.
  • Another participant mentions the limitations of their debugging tools, indicating they cannot use gdb due to their operating system.
  • A participant provides a detailed list of values for 'this', 'prev', 'nextEntry', and 'front' at different points during the execution of reverseM().

Areas of Agreement / Disagreement

Participants express differing views on the mechanics of pointer handling in the reversal process, with no consensus reached on the implications of the pointer behavior. The discussion remains unresolved regarding the potential for overwriting pointers.

Contextual Notes

Some participants reference diagrams and documents that are not visible in the thread, which may limit the understanding of the discussion. There are also mentions of specific debugging tools that may not be available to all participants.

John O' Meara
Messages
325
Reaction score
0
Code:
 class intList {
  int value;
  intList *nextEntry;
  intList *reverseM(intList *prev);
public:
  intList(int v, intList *next) : value(v), nextEntry(next){}
  int getValue() { return value; }
  intList *getNextEntry() { return nextEntry; }
  intList *reverseM();
};

intList *readFowardList()
{
   int inputval;
   intList *front = NULL;

  cout << "Enter number: " ; cin >> inputval;

  while(!cin.eof()) {
     front = new intList(inputval, front);
     cout << "Enter number: ";  cin >> inputval;
  }
  cout << '\n';
  return front->reverseM();
}


int main() {
  intList *theList;


  cout << theList;

  deleteList(theList);

  return 0;
}
See the 2nd page of the attachment for reverseM() definition please. If you look at the 1st page of the attachment; the top of that page shows the way I think how the function reverseM() is called; in the middle of that page I show the 'returns' of reverseM() as I think what is going on. where the list consists of just three cells, namely, 5 3 and 2, and we want reverseM() to reverse that list. According to my middle of the page diagram, the 'this' pointer is the first to return (with the new head of the list) after reverseM() is finished being called and then the local 'front' pointer then returns; why don't the the local pointers 'front' overwrite the local 'this' pointer and in conquence the new head of the reversed linked list be lost? Or is that middle diagram wrong, also are the upper diagrams wrong then? Time increases downward vertically on the 1st page of the attachment. I just don't see how it is possible not to overwrite the the 'this' pointer which returns the address of the value 2 as the head of the reversed list. So if anyone can draw/write out the sequence of events in this three cell linked list, I would be grateful. Thanks in advance.
 

Attachments

Technology news on Phys.org
Why not attach your code as a text file instead of a PDF?
 
I didn't think that hand drawn sketches could be stored as a .txt file.
 
No they can't, other than very rudimentary sorts of things. I didn't realize that you had something other than code in the pdf (which I didn't open).
 
If you have a source level debugger, you could follow the code to see what is happening.

Here is a summary: reverseM() calls itself until it reaches the end of the list. The public call to reverseM() is overridden so it the initial call passes a parameter of NULL for "prev". For each instance of reverseM(), its "this" pointer will be a pointer to the instance of intList used to make the call to reverseM(), while "prev" will be a parameter passed to reverseM(). "front" doesn't get set until the end of the list is reached, and then it's just returned back up the call chain without ever getting modified.

Using your pdf document, initially theList points to "5" (the element with a 5). On the initial call to reverseM(), "this" will point to "5", and prev will be set to NULL (due to the override declaration). Then reverseM() will call itself using the nextEntry pointer to "3" and passing the pointer to 5, and that instance of reverseM()'s "this" will point to "3", and "prev" will point to "5". In the next instance, "this" points to "2", and "prev" points to "3", and since this->nextEntry = NULL, it sets this->nextEntry to "3", sets front to "2", and returns front. The prior instance of reverseM() will set "3"->nextEntry to "5", and return front still = "2". The next prior instance of reverseM() will set "5"->nextEntry to the NULL passed from the initial call, and return front still = "2", returning back to main.

You might want to replace main with something like this if you use a source level debugger to avoid having to key in numbers:

Code:
int main(){
using namespace std;
intList *theList = NULL;
intList *delList;
int i;

    for(i = 0; i < 8; i++)
        theList = new intList(i, theList);

    theList = theList->reverseM();
    
    while(theList != NULL){
        i = theList->getValue();
        cout << i << endl;
        delList = theList;
        theList = theList->getNextEntry();
        delete delList;
    }

    return(0);
}
 
Last edited:
I use a port of gcc for my operating system called RISC OS, but they don't seem to have the gdb debugger ported. Thank you very much for your time in answering my thread and for the code. I have included another pdf document that shows now my understanding of what happens when reverseM() is called and then returns.
 

Attachments

John O' Meara said:
I have included another pdf document that shows now my understanding of what happens when reverseM() is called and then returns.
Here's a list starting at time = zero

Code:
this   prev nextentry front   

   5    null             3      x   values at entry to reverseM()
   3       5             2      x
   2       3          null      x
 -------------------------
   2       3            3       2   values at exit from reverseM()
   3       5            5       2
   5    null         null       2
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K