rumi54
asked on
Compiling error for deleting from linked lists
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->nex t;
delete remove;
bl. Insert (8)
return 0;
}
51 '
51 {each
52 request
63 parse
#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->nex
delete remove;
bl. Insert (8)
return 0;
}
51 '
51 {each
52 request
63 parse
Hi rumi54,
Asumming you will define someProperty correctly, you must use an arrow operator after "next":
while ( temp->next->someProperty != 3 )
Good luck,
Jaime.
Asumming you will define someProperty correctly, you must use an arrow operator after "next":
while ( temp->next->someProperty != 3 )
Good luck,
Jaime.
If I were you I'd add comments through the code to say what your loops promise to do.
ASKER
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->nex t;
delete remove;
bl. Insert (8)
return 0;
}
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->nex
delete remove;
bl. Insert (8)
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
int gethead
node* n = head;
for (int i == NULL);
Would this function in buildlist work?
node* n = head;
for (int i == NULL);
Would this function in buildlist work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
> 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.
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.
That problem has solved in another rumi54 question. Just was a missing pause before ending main function.
struct node does not have someProperty defined in it.