Solved

One error to compile for linked list program

Posted on 2004-07-31
6
264 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 75
C++ question 3 70
Header of docx file 17 113
oracle 11g 23 106
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

792 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