Beginner with C++ and goofy run time problem with DBL Linked List

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #1
Jul 25th, 2005
Greetings To all!

To whomever, an advanced thank you if you help me. I am a Java programmer initially, but I have begun learning C++ and am doing something with a Double Linked List (Error occurs whether it's double or single, I teste it). Filling the contents of the list I have managed, but when I try and print each item of the list doing this, I get a run time error, which when I go to debugger looks like its trying to access a NULL

=========================================================
ListNode *p = head;

cout << "[ ";

for( ; p != NULL; p = p->next)
{
cout << p->data << " ";
}

cout << "]"

=========================================================

Now this code works fine in Java, but in C++ it makes a stink at me, a runtime stink. I shouldn't be overstepping the boundaries of the list, becuase the stopping condition in the for loop should catch before that happens, but I'm not sure what is going on. I'm stabbing at it being a NULL access that's not allowed, but I'm not sure that this is the error, becuase C++ is so fantastically wonderful at describing its errors of course(hopefully catching the sarcasm here)

Anywho, if some kind soul might shed some light on this stupid poor novice, I would appreciate becuase I'm sure it's something I missed that is probably trivial. (I tend to do that a lot). Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #2
Jul 25th, 2005
>becuase C++ is so fantastically wonderful at describing its errors of course
Java errors are no less cryptic, you're just used to them.

>I shouldn't be overstepping the boundaries of the list
You shouldn't, but you probably are if you fail to terminate the list at any point with NULL. For example, if the list is terminated by an uninitialized pointer, it probably won't be NULL, so the condition will fail and you'll walk beyond the end of the list and get an access violation. I would imagine that this is the case since Java initializes references to null, and if you were used to that you may not be watching out for an uninitialized pointer in C++.

Can you post a complete program that exhibits the problem?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #3
Jul 25th, 2005
Ah touche! Indeed Java errors can be quite convoluted as well...

Anyway getting on with it. The program right now is quite small as it is in test form. Basically I have the .h file of course with the declarations, but I can assume, I hope, you don't need to see that (If it is necessary let me know). So here are the cpp for main and the list class

There are the two functions, which I will display here below:

============================================================

void SortedList::Insert(string s)
{
if(head == NULL)
{
head = tail = new ListNode;
head->data = s;
}

else
{
ListNode *temp = tail;
string comp = temp->data;
char c;

c = s[0];

if( (int)c > (int)comp[0] ) // for purposes of alphabetic post insertion
{
temp->next = new ListNode;
temp->next->data = s;
tail = temp->next;
tail->prev = temp;
}
}
}

void SortedList:rintIt()
{
ListNode *p = head;

cout << "[ ";

for(; p != NULL; p = p->next)
{
cout << p->data << " ";
}

cout << "]";
}

=========================================================

And then the main function simply calls like so:

=========================================================
SortedList x;

int main()
{
string a = "apple";
string o = "orange";
string p = "peach";
string f = "fruit"; // "fruit" shouldn't get into the list
x.Insert(a);
x.Insert(o);
x.Insert(p);
x.Insert(f);

x.PrintIt();

return 0;
}

==========================================================

Thank you for answering my thread, and if I can provide any more information that might increase understanding of my current problem let me know. If you can figure out my problem, I would be very open to constructive suggestions and comments that might guide me, should you be willing to give them. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #4
Jul 25th, 2005
Ya might want to put some code tags in there.
That's awfully hard to read.


Anyway, is SortedList a custom class/struct of yours?
Might be nice to have that code, too...

And if it's not available, methinks we've found the problem.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #5
Jul 25th, 2005
Darn, it did not post out the way I wanted it to. That is decidedly tough to read.

Yes, the SortedList is a custom class defined in the .h file...I will repost cleaner post here. The whole workspace worth.

SortedList.h File is as follows:

==========================================================

[HTML]#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

/*
* SortedList class
*
* A SortedList is a collection of strings in alphabetical order
*
* Operations:
* constructor Initialize the list to be empty.
* Insert Add a given string to the list.
* Print Print (to the given ostream) the strings in the list in
* order, enclosed in square brackets, separated by spaces.
* Lookup Return the number of times a given string occurs in the list.
*/

class SortedList
{
public:

// constructor
SortedList();

// modifiers
void Insert(string s);

// other operations
int Lookup(string s);
void Print(ostream &output);
void PrintIt();

private:

struct ListNode
{
string data;
ListNode *prev;
ListNode *next;
};

// pointer to the first node of the list
ListNode *head;
ListNode *tail;
};[/HTML]

The SortedList.cpp file is as follows:

=========================================================
[HTML]#include "SortedList.h"

using namespace std;
/*
*
* Soretd List Constructor
*
*/

SortedList:ortedList()
{
head = tail = NULL;
}

/**
* function: Insert
* parameters: String
* returns: void
*
*/

void SortedList::Insert(string s)
{
if(head == NULL)
{
head = tail = new ListNode;
head->data = s;
}

else
{
ListNode *temp = tail;
ListNode *prevM = NULL;
string comp = temp->data;
char c;

c = s[0];

if( (int)c > (int)comp[0] )
{
temp->next = new ListNode;
temp->next->data = s;
tail = temp->next;
tail->prev = temp;
}
}
}

void SortedList:rintIt()
{
ListNode *p = head;

cout << "[ ";

for(; p != NULL; p = p->next)
{
cout << p->data << " ";
}

cout << "]";
}[/HTML]


And the main file is:

=========================================================
[HTML]#include "SortedList.h"

SortedList x;

int main()
{
string a = "apple";
string o = "orange";
string p = "peach";
string f = "fruit";
x.Insert(a);
x.Insert(o);
x.Insert(p);
x.Insert(f);

x.PrintIt();

return 0;
}[/HTML]

=========================================================

And that is the whole code so far. Hope that looks cleaner, sorry about the previous posting of it
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #6
Jul 25th, 2005
You're creating nodes without tracking all your pointers. Always, always, ALWAYS set your next pointer to NULL once you've made a new node.

  1.  
  2. void SortedList::Insert(string s)
  3. {
  4. if(head == NULL)
  5. {
  6. head = tail = new ListNode;
  7. head->data = s;
  8. head->next = NULL;//Good practice, in my opinion.
  9. head->prev = NULL;
  10.  
  11. }
  12.  
  13. else
  14. {
  15. ListNode *temp = tail;
  16. ListNode *prevM = NULL;
  17. string comp = temp->data;
  18. char c;
  19.  
  20. c = s[0];
  21.  
  22. if( (int)c > (int)comp[0] )
  23. {
  24. temp->next = new ListNode;
  25. temp->next->data = s;
  26. tail = temp->next;
  27. tail->prev = temp;
  28. tail->next = NULL; //Here was where it went wrong.
  29. }
  30. }
  31. }
Last edited by Drowzee; Jul 25th, 2005 at 6:12 pm. Reason: For pompusness.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #7
Jul 25th, 2005
Add this to your declaration of ListNode:
  1. ListNode(): prev(0), next(0) {}
That will fix the issue with access violations by properly terminating each node, but your Insert algorithm is incorrect. Remember that there are three cases when inserting into a sorted list: when the list is empty, when the new node is less than the head, and when the new node is greater. If you don't account for all three of these cases, your insertion is broken:
  1. struct ListNode
  2. {
  3. string data;
  4. ListNode *prev;
  5. ListNode *next;
  6.  
  7. ListNode(): prev(0), next(0) {}
  8. ListNode(const string& s, ListNode *p, ListNode *n)
  9. : data(s), prev(p), next(n) {}
  10. };
  11.  
  12. void SortedList::Insert(string s)
  13. {
  14. if(head == NULL)
  15. {
  16. head = tail = new ListNode;
  17. head->data = s;
  18. }
  19. else if (s < head->data)
  20. {
  21. head = new ListNode(s, 0, head);
  22. }
  23. else
  24. {
  25. ListNode *temp = head;
  26.  
  27. while (temp->next != 0 && temp->next->data < s)
  28. temp = temp->next;
  29.  
  30. temp->next = new ListNode(s, temp, temp->next);
  31.  
  32. if (temp == tail)
  33. tail = temp->next;
  34. }
  35. }
>As I suspected
:rolleyes:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #8
Jul 25th, 2005
Beginning ego deflation process...
...
.....
......
Process complete.
Please don't hurt me.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #9
Jul 25th, 2005
Thank you for your help...I salute you. I am now taking the items into account and will be implementing the problem fixes later tonight. Thanks again.

Oi, Akan zya nai desu yo!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Beginner with C++ and goofy run time problem with DBL Linked List

 
0
  #10
Jul 25th, 2005
I don't feel like I can quite take credit for my less-optimal solution... Bleh...

Narue, that
  1. ListNode(): prev(0), next(0) {}
You posted...
What is that called? Is that a variant of a constructor?

... I don't see that usage of it in my books over here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC