You first check if if(temp == NULL)
and if it is so you nevertheless want to access members of it. That cannot work. If temp
is NULL
you have to create new node struct and add it to the list (depending on what temp
exactly represents). So some more information about the node structure and the list would be great.
EDIT Let me summarize what you should do:
- Add default constructors to both structs that initialize
ant
to0
andnext
toNULL
(ornullptr
if you can use the new standard). - As already mentioned several times: get rid of the code that tries to access members of a nullptr.
- Just drop the
if (cin >> x)
. You don't need it as you ignore the old value that was read intox
in the loop condition (and furthermore ifcin >> x
fails you overwrite an existing entry). But only the condition itself - not the code executed inside it: you definitly need that code as it creates the new node to store the new word. Draw a picture with boxes and arrows if you don't know how that could happen.
If you do all that it should work - at least it then worked for me. Furthermore:
- I hope you know that there is some kind of sentinal in this list: the element created with
node* ord = new node;
always remains in the list and counts all occurences of the string""
. You must pay attention to that (especially when freeing the elements in the list). - I would merge both structs into one single struct as it makes the code more readable (at least in my opinion).
- Don't forget to free your list properly!