Linked List

I have the following functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void list::insert(const ElementType &item)
{
   nodeptr prevNode = first;
   nodeptr newNode = new Node(item);
   nodeptr tempNode;
   nodeptr currentNode = first;
   bool added = false;
   
   if (!first)
   {
      first = newNode;
      newNode->next = NULL;
      count++;
   }
   else
   {
      newNode->next = first;
      first = newNode;
      count++;
   }
}


Overloaded >= operator
1
2
3
4
5
6
7
8
bool operator>=(const ElementType &el, const KeyType &key)
{

   if(strcmp(el.key.c_str(), key.c_str()) < 0)
      return true;
   else
      return false;
}


Some Base Code information:
1
2
3
4
5
6
7
8
9
struct Article_s
{
         KeyType key;
         string name;
         string title;
};

typedef struct Article_s ElementType;
typedef std::string KeyType;


The insert function receives a ElementType item with the info to be added in a new node. The insert function needs to look through the list and add the new node in the appropriate spot for it to be alphabetical using the overloaded operator. I"m pretty sure the overloaded operator is messed up as I've been messing around with it for a couple hours now and have gotten nowhere.

If anyone could help me out it would be appreciated. And if any other of the code is needed I can post it.
Last edited on
I'm confused. You haven't even tried to use the operator>= yet. You should post a more complete example that shows us how you are trying to use it.

I will give you a hint. std::string has an operator== defined. There is no need to call c_str() or strcmp when comparing two std::string objects. Also I would think that you want an operator< that takes two objects of the same type so that the usage of the function will seem more intuitive. I guess you can define whichever operator you wish but I think operator< seems more intuitive but that is because many of the std algorithms use operator< by default so that is the one that I am used to seeing defined in these kinds of situations.

1
2
3
4
bool operator<(const ElementType &lhs, const ElementType &rhs)
{
    return lhs.key < rhs.key;
}
The following is different code I've tried (commented out):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

void list::insert(const ElementType &item)
{
   nodeptr prevNode = first;
   nodeptr newNode = new Node(item);
   nodeptr tempNode;
   nodeptr currentNode = first;
   bool added = false;
   if (count==0)
   {
      first = newNode;
      newNode->next = NULL;
      count++;
      cout << "3";
   }
   else
   {
   /*   while(currentNode!= NULL)
      {
         prevNode = currentNode;
         currentNode = currentNode->next;
         if(currentNode->Info >= newNode->Info.key)
         {
            added=true;
            tempNode =prevNode->next;
            prevNode->next =newNode;
            newNode->next = tempNode;
            
         }
         if(added=false)
            prevNode->next = newNode;
      }
            
   }*/
      newNode->next = first;
      first = newNode;
      count++;
   }
   
   /*if (currentNode == NULL)  
   {
      first = newNode;
      newNode->next = NULL;
      added=true;
      cout << "here";
   }
   else
   {
      currentNode = first;
      cout << currentNode->next;
      while(!added)
      {

      if(newNode->Info>=currentNode->Info.key  || currentNode == NULL)
      {
         tempNode = prevNode->next;
         prevNode->next = newNode;
         newNode->next = tempNode;
      }
      }

   }*/


I either end up with a runtime error or an empty list with the above code.


As far as there being better ways with the operators and such, its what my instructor has required for this assignment. We were told to use the following overloaders:

1
2
bool operator==(const ElementType &el, const KeyType &key)
bool operator>=(const ElementType &el, const KeyType &key)
Last edited on

Try this for your operator >=
1
2
3
4
5
bool operator>=(const ElementType &el, const KeyType &key)
{

   return el.key >= key;
}


I need to see the definition of the node type. What is node? Please post the entire declaration of your list class. In fact you should show the list class declaration and definition along with a small main function that demonstrates the problem. It is hard to understand without seeing the type declaration for all types that you are using. The node is constructed with the elements but how? What is a node?
Thanks for the help
Last edited on
1
2
3
4
5
6
7
8
9
10
// Make sure that you are initializing all attributes!
// who knows what count was when you called insert the first time.  Are you using a debugger?
list::list() : first(0), count(0) {
}

// Simplify this (not a big deal though)
bool operator==(const ElementType &el, const KeyType &key)
{
   return (el.key == key);
}


I'm not sure where you are at with debugging at this point but you ought to be stepping with a debugger and analyzing the state of your class and variables as you go. Always provide default initial values for classes.

Since you are taking user input many things could be happening and I am not sure where specifically you are seeing the run-time errors. I need to know what you have done on your own and what combination of inputs you have tried. You can easily determine where the program is crashing (if it is) using the debugger and if there is something that you don't understand about that follow up with some more questions.

You really should have posted things as one compilable unit. Please edit that previous post and give us exactly what you are compiling and running otherwise I cannot help you any further.
If you only need to compare strings why did you overload operator==? Also use the default operator= instead of manually assigning all values.

1
2
3
4
5
6
7
8
9
      if(currentNode->Info.key == key)
      {
         item.key = currentNode->Info.key;
         item.title = currentNode->Info.title;
         item.name = currentNode->Info.name;
         return true;
         // could be 
         // item = currentNode->Info;
      }
I updated previous post with all code.

I compile and link list.cpp and main.cpp. It reads data in from a text file (command line argument). Example follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Fox:2000:NTCa
Robert Fox
News Track: Celestial backbone; Foreign hands off China's net; Women and the web; DOE says no nuke access; Worst net nightmare; Sidebar: Ethics quiz
Crawford:2000:FTO
Diane Crawford and Brian K. E. Balke and Steve Wilstrom and Jef Raskin and Pat McGee
Forum: Tackling OO Design Principles; Educational Concerns; Building LEGO Robots
Meeks:2000:EFBa
Brock N. Meeks
Electronic frontier: bugging out: Y2K fallout or business as usual?
Soloway:2000:LEK
Elliot Soloway and Cathleen Norris and Phyllis Blumenfeld and Barry Fishman and Joseph Krajcik and Ronald Marx
Log on Education: K--12 and the Internet
Porter:2000:SCL
Tom Porter and Galyn Susman
On site: creating lifelike characters in Pixar movies
Simons:2000:PBB
Barbara Simons
From the President: building Big Brother
Konana:2000:IOI
Prabhudev Konana and Nirup M. Menon and Sridhar Balasubramanian
The implications of online investing
Gorriz:2000:EGC
Cecilia M. Gorriz and Claudia Medina
Engaging girls with computers through software games
Hargittai:2000:RLI
Eszter Hargittai
Radio's lessons for the Internet
Ackerman:2000:ROM
Mark S. Ackerman and Christine A. Hadverson
Reexamining organizational memory


And as far as overloading the == operator. I just followed what it says we are required to do in our instructions.
Last edited on
Topic archived. No new replies allowed.