Solved

Compiling error for deleting from linked lists

Posted on 2004-07-31
10
231 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
 

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
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.

 

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

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

895 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

13 Experts available now in Live!

Get 1:1 Help Now