Solved

Compiling error for deleting from linked lists

Posted on 2004-07-31
10
229 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

18 Experts available now in Live!

Get 1:1 Help Now