Solved

One error to compile for linked list program

Posted on 2004-07-31
6
261 Views
Last Modified: 2013-12-14
Can someone help me figure out why this won't compile?  There is only line, the return = i;
that is problematic.  What am i doing wrong?


#include <iostream>
using namespace std;

struct node
{
     int data;
     node* next;
     ~node() { delete next; }
     void Print() { cout << data << "," ;  if (next) next->Print(); }

};

class Buildlist
{
    node* head;
public:
    Buildlist() : head(NULL) {}
    ~Buildlist() { delete head; } //clean up
    int Length()  
    { node* n = head;
        for (int i = 0;
        n != NULL;   //iterates unless null which signifies end
        n = n->next, ++i);
        return i; } //this is where I'm having problems.
    void Insert(int dat)
        { node* p = 0;
    node* n = head;
    node* nw = new node;
    nw->next = NULL; nw->data = dat;
       for ( ; n != NULL; n = n->next)
          { if (n->data > dat) break; p = n; }
            if (p == NULL)   head = nw;
                else                   p->next = nw;
                  if (n != NULL)    nw->next = n;
                                }
      void Print() { if (head) head->Print(); cout << endl; }
};

int main ()
{
     Buildlist bl;
     bl. Insert( 0);
     bl. Insert( 1);
     bl. Insert( 5);
     bl. Insert( 2);
     bl. Insert( 3);
     bl. Insert( 4);
           
     cout << "The list has " << bl.Length() << " entries: ";
     bl.Print();

     return 0;
}
0
Comment
Question by:rumi54
  • 4
  • 2
6 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11684783
The problem is in the Insert function. Try to be clearer, use indentation properly to read your code easy:

      void Insert(int dat)
      {
            node* prior = NULL;
            node* nw = new node;
            nw->data = dat;
            for (node *n=head; n; n = n->next)  {
                  if (n->data > dat)
                        break;
                  prior = n;
            }
            if (!prior) {  
                  head = nw;
                  nw->next = NULL;
            } else {
                  nw->next = prior->next;
                  prior->next = nw;
            }
      }
0
 

Author Comment

by:rumi54
ID: 11684807
That didn't work it still doesn't compile.  Any other ideas?  The return i statement is problematic in line 24
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11684857
>That didn't work it still doesn't compile.  Any other ideas?  The return i statement is problematic in line 24
Please be specific because I have compiled before posting you. If a portion of code doesn't compile, you don't have to "discard" it and ask for "another idea".
You GetLenght() function is simple and clear, you are having problem in your insertion function, so your GetLenght() doesn't work as expected.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11684861
Also you can post all your compiling error messages.
0
 

Author Comment

by:rumi54
ID: 11684882
Return i;} is highlighted and these are the only error messages.

24 C:\Documents and Settings\Will Rosellini\My Documents\Linked list from experts.cpp name
21 C:\Documents and Settings\Will Rosellini\My Documents\Linked list from experts.cpp


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11684923
>Return i;}

I supose you have not written return with upper case.
I don't know about your compiler capabilities but you can try to simple reorder, because your code is perfectly valid and compiles in Visual C++:

int Length()  
{
        node* n = head;
        for (int i = 0;  n != NULL; n = n->next, ++i);
        return i;
}
 
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now