Solved

One error to compile for linked list program

Posted on 2004-07-31
6
262 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

920 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

15 Experts available now in Live!

Get 1:1 Help Now