Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compiling error for deleting from linked lists

Posted on 2004-07-31
10
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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.

670 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