Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compiling error for deleting from linked lists

Posted on 2004-07-31
10
Medium Priority
?
239 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1050 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 450 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

885 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