Solved

One error to compile for linked list program

Posted on 2004-07-31
6
263 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 82
White board coding practice 3 88
c++ reading data from file into two dimensional array 3 109
How to Correctly derive class from CWinThread in MFC 4 41
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

803 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