Solved

One error to compile for linked list program

Posted on 2004-07-31
6
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
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.

763 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