Solved

Compiling error for deleting from linked lists

Posted on 2004-07-31
10
233 Views
Last Modified: 2013-12-14
This is source for a program that will build a link list with 1,2,3,4,5.  It will then delete 3 and insert 8, I can't figure out how to make it compile.  I have the error messages pasted below.  Thanks for the help.


#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; }
   int Length()   {
      node* n = head;
      for (int i = 0; n != NULL; n = n->next, ++i)
      return i; }
    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( 2);
     bl. Insert( 3);
     bl. Insert( 4);
     bl. Insert( 5);
           
     bl.Print();
     node * temp = head;
          while ( temp->next.someProperty != 3 )
                    {
                    temp = temp->next;
                    }
                    node * remove = temp->next;
                    temp->next=temp->next->next;
                    delete remove;
      bl. Insert (8)
      return 0;
}

51      '
51      {each
52       request
63       parse
0
Comment
Question by:rumi54
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 11685131
> while ( temp->next.someProperty != 3 )

struct node does not have someProperty defined in it.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11685134
Hi rumi54,

Asumming you will define someProperty correctly, you must use an arrow operator after "next":
while ( temp->next->someProperty != 3 )

Good luck,
Jaime.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 11685136
If I were you I'd add comments through the code to say what your loops promise to do.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:rumi54
ID: 11685163
I made the following changes (added int someproperty, changed arrow and added comments).  It still won't compile here are the errors
52 '
52 {Each
61 parse


#include <iostream>
using namespace std;

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

};

class Buildlist
{
    node* head;
public:
    Buildlist() : head(NULL) {}
    ~Buildlist() { delete head; }
   int Length()   {
      node* n = head;
      for (int i = 0; n != NULL; n = n->next, ++i) //iterates until finds null
      return i; } //returns number of iterations to Length
    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) //not null
                        { 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( 2);
     bl. Insert( 3);
     bl. Insert( 4);
     bl. Insert( 5);
           
     bl.Print();
     node* temp = head; //to delete an insert
          while ( temp->next->someProperty != 3 )
                    {
                    temp = temp->next;
                    }
                    node* remove = temp->next;
                    temp->next=temp->next->next;
                    delete remove;
      bl. Insert (8)
      return 0;

}
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 350 total points
ID: 11685183
head doesn't exist in the scope of main, only in the BuildList class.  You could add a "GetHead()" function in BuildList to return the head node.  Also, someProperty should be data.  You also need a semicolon after bl.Insert(8).  You may have other problems, but if you change these you will at least be able to get your code to compile.

#include <iostream>
using namespace std;

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

};

class Buildlist
{
    node* head;
public:
    Buildlist() : head(NULL) {}
    ~Buildlist() { delete head; }
    node *GetHead() { return head; }
   int Length()   {
      node* n = head;
      for (int i = 0; n != NULL; n = n->next, ++i) //iterates until finds null
      return i; } //returns number of iterations to Length
    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) //not null
                        { 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( 2);
     bl. Insert( 3);
     bl. Insert( 4);
     bl. Insert( 5);
           
     bl.Print();
     node* temp = bl.GetHead(); //to delete an insert
          while ( temp->next->data != 3 )
                    {
                    temp = temp->next;
                    }
                    node* remove = temp->next;
                    temp->next=temp->next->next;
                    delete remove;
      bl. Insert (8)
      return 0;
}
0
 

Author Comment

by:rumi54
ID: 11685226
int gethead
      node* n = head;
      for (int i == NULL);

Would this function in buildlist work?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 11685250
I see there is a missing semicolon in your Lenght()  function:

   int Length()   {
      node* n = head;
      for (int i = 0; n != NULL; n = n->next, ++i);     //  <--------- missing semicolon!!!!
      return i; } //returns number of iterations to Length

0
 

Author Comment

by:rumi54
ID: 11688440
mnashadka I made your corrections and the program now compiles.  However, when I run it the screen only flashes and I don't see any output.  The points are yours if you can help me out.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 11689786
> when I run it the screen only flashes

Sounds like you've got your VGA cable caught up in the linked list ;-)

Rather than fixing your code for you it would be better if we could help you step through it. Your error messages don't look like GNU's or Visual C's. Let us know what compiler/OS you are using and we may be able to help you step through this with an appropriate debugger.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11689826
That problem has solved in another rumi54 question. Just was a missing pause before ending main function.
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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

810 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