Click here to Skip to main content
15,391,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

So I'm trying to reverse a sentence without reversing the letters of the string. For example, Given the input: I->" "->l->i->k->e->" "->p->i->e->NULL , the output should be Output: p->i->e->" "->l->i->k->e->" "->I->NULL. My code seg. faults at the last letter of the sentence and I'm not able to figure out why.

void List:: reverseList() {
  ListNode * prevNode = NULL;
  ListNode * currNode = head; 
  ListNode * spaceNode = NULL;
  while(currNode != NULL) {
    cout << "CurrNode: " << currNode -> value << endl;
    if(currNode -> next -> value == '-' || currNode -> next == NULL) {
      spaceNode = currNode -> next;
      currNode -> next = spaceNode;
      if(prevNode != NULL) {
	spaceNode -> next = prevNode;
      }
      prevNode = spaceNode -> next;
      currNode = currNode -> next;
    }
    else {
      currNode = currNode -> next;	
    }
  }
}


What I have tried:

I've tried changing the if-statement conditions so my code can try to go through it for the last letter of the inputted sentence.
Posted
Updated 23-Jun-20 17:19pm
Comments
Garth J Lancaster 23-Jun-20 22:34pm
   
Your issue would suggest that the first and/or last 'node' of your list isn't correct - since you don't provide the code that builds the list I/we can't say.

I am assuming (always dangerous) that this is an assignment/homework of some sort where you must use your own list - else you would just use the STL List implementation and a standard reverse iterator with rbegin && rend, yes ?

I think you should use 'Improve question' and show the code that build's the list and comment why prevNode/curNode and particularly spaceNode exist

I'm not sure what dev environment you're using, but you should also be able to highlight the line that causes the exception (and it will almost certainly have null's where you dont expect) by 'single stepping' through the code - debugging is a useful skill

Your code snipset is incomplete, so we can't run it, this complicate any test we can do. What remain is guessing the missing parts of code and then guessing what does the code just by reading it.
The only tool that can help you is the debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
As Patrice said, It's not very clear what List is. Assuming it is similar to std::list, the fragment below works:
C++
void reverse (const list<char>& in, list<char>& out)
{
  auto pi = in.begin ();
  auto pchar = out.begin ();

  while (pi != in.end ())
  {
    if (*pi != ' ')
      pchar = out.insert (pchar, *pi)++;
    else
      pchar = out.insert (out.begin(), *pi);
    pi++;
  }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900