the code of two-way linked list failed to be compiled.

Y

yang2006

I'm using Dev-c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


class Node
{
friend class List;
public:
Node(int d,Node* p=NULL,Node*
n=NULL):data(d),prev(p),next(n){}
private:
int data;
Node* prev;
Node* next;
};


class List
{
public:
List():head(NULL){}
~List()
{
Node* cur=head;
Node* prev=NULL;
while(cur!=NULL)
{
prev=cur;
cur=cur->next;
delete prev;
}
}


void insert(int data)
{
Node* node=new Node(data);
if(head=NULL)
{
head=node;
return;
}

Node* cur=head;
while(cur->next!=NULL)
{
if(data<=cur->data)
{
if(cur->prev==NULL)
{
head=node;
head->next=cur;
cur->prev=head;
}
else
{
node->prev=cur->prev;
node->next=cur;
cur->prev->next=node; //???
}
return;
}
else
{
cur=cur->next;
}
}


if(cur->data<=data)
{
cur->next=node;
node->prev=cur;
}
else
{
node->next=cur;
node->prev=cur->prev;
cur->prev->next=node;
}
}


void DelNode(int da)
{
Node* cur=head;
while(cur->next!=NULL)
{
if(cur->data==da)
{
if(cur->prev==NULL)
{
head=cur->next;
delete cur;
}
else
{
cur->prev->next=cur->next;
cur->next->prev=cur->prev;
delete cur;
}
return;
}
else
{
cur=cur->next;
}
}
if(cur->data==da)
{
cur->prev->next=NULL;
delete cur;
}

}

void display()
{
Node* cur=head;
while(cur!=NULL)
{
printf("%d\n",cur->data);
cur=cur->next;
}
}

private:
Node* head;
};





int main(int argc, char *argv[])
{
List l;
l.insert(10);
l.insert(100);
l.insert(150);
l.insert(145);
l.insert(200);
l.insert(2);
l.insert(180);
l.insert(135);
l.insert(130);
l.insert(1);
l.insert(1);
l.insert(200);
l.display();
printf("*******************\n");
l.DelNode(200);
l.display();

system("PAUSE");
return 0;
}
 
O

osmium

:

Look more closely at your screen. Actually it *does* compile, it just makes
an error before anything is printed. Next step: isolate the input by
putting #if 0 --- #endif around the input data. This should be enough so
you can proceed under your own capabilities. Two elementary notes in code

I'm using Dev-c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


class Node
{
friend class List;
public:
Node(int d,Node* p=NULL,Node*
n=NULL):data(d),prev(p),next(n){}
private:
int data;
Node* prev;
Node* next;
};


class List
{
public:
List():head(NULL){}
~List()
{
Node* cur=head;
Node* prev=NULL;
while(cur!=NULL)
{
prev=cur;
cur=cur->next;
delete prev;
}
}


void insert(int data)
{
Node* node=new Node(data);
if(head=NULL)
{
head=node;
return;
}

Node* cur=head;
while(cur->next!=NULL)
{
if(data<=cur->data)
{
if(cur->prev==NULL)
{
head=node;
head->next=cur;
cur->prev=head;
}
else
{
node->prev=cur->prev;
node->next=cur;
cur->prev->next=node; //???
}
return;
}
else
{
cur=cur->next;
}
}


if(cur->data<=data)
{
cur->next=node;
node->prev=cur;
}
else
{
node->next=cur;
node->prev=cur->prev;
cur->prev->next=node;
}
}


void DelNode(int da)
{
Node* cur=head;
while(cur->next!=NULL)
{
if(cur->data==da)
{
if(cur->prev==NULL)
{
head=cur->next;
delete cur;
}
else
{
cur->prev->next=cur->next;
cur->next->prev=cur->prev;
delete cur;
}
return;
}
else
{
cur=cur->next;
}
}
if(cur->data==da)
{
cur->prev->next=NULL;
delete cur;
}

}

void display()
{
Node* cur=head;
while(cur!=NULL)
{
printf("%d\n",cur->data);
cur=cur->next;
}
}

private:
Node* head;
};





int main(int argc, char *argv[])
{
List l;

#if 0
 
J

Joachim Schmitz

yang2006 said:
I'm using Dev-c.

No, you're using Dev-C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


class Node

C doesn't have class
{
friend class List;
public:

nor friend or public
Node(int d,Node* p=NULL,Node*
n=NULL):data(d),prev(p),next(n){}
private:

or private. C++ does have these, so you're better off asking your quesion in
comp.lang.c++. If you do, do also mention the error message(s) you get from
your compiler.

However: your code compiles cleanly with VisualC++ Express, but crashes when
started.
int data;
Node* prev;
Node* next;
};


class List
{
public:
List():head(NULL){}
~List()
{
Node* cur=head;
Node* prev=NULL;
while(cur!=NULL)
{
prev=cur;
cur=cur->next;
delete prev;
}
}


void insert(int data)
{
Node* node=new Node(data);
if(head=NULL)

I guess you most probably meant to write
if(head==NULL)
here, and as this is a typical error in C as well as in C++, you're not
completly OT here.
{
head=node;
return;
}

Node* cur=head;
while(cur->next!=NULL)

Here it crashes. It does not with above mentioned fix...
{
if(data<=cur->data)
{
if(cur->prev==NULL)
{
head=node;
head->next=cur;
cur->prev=head;
}
else
{
node->prev=cur->prev;
node->next=cur;
cur->prev->next=node; //???
}
return;
}
else
{
cur=cur->next;
}
}


if(cur->data<=data)
{
cur->next=node;
node->prev=cur;
}
else
{
node->next=cur;
node->prev=cur->prev;
cur->prev->next=node;
}
}


void DelNode(int da)
{
Node* cur=head;
while(cur->next!=NULL)
{
if(cur->data==da)
{
if(cur->prev==NULL)
{
head=cur->next;
delete cur;
}
else
{
cur->prev->next=cur->next;
cur->next->prev=cur->prev;
delete cur;
}
return;
}
else
{
cur=cur->next;
}
}
if(cur->data==da)
{
cur->prev->next=NULL;
delete cur;
}

}

void display()
{
Node* cur=head;
while(cur!=NULL)
{
printf("%d\n",cur->data);
cur=cur->next;
}
}

private:
Node* head;
};





int main(int argc, char *argv[])
{
List l;
l.insert(10);
l.insert(100);
l.insert(150);
l.insert(145);
l.insert(200);
l.insert(2);
l.insert(180);
l.insert(135);
l.insert(130);
l.insert(1);
l.insert(1);
l.insert(200);
l.display();
printf("*******************\n");
l.DelNode(200);
l.display();

system("PAUSE");
return 0;
}

Bye, Jojo
 
M

Martin Ambuhl

yang2006 said:
class Node
{
friend class List;
public:
Node(int d,Node* p=NULL,Node*

The above (and most of what follows) is not C, but C++, a different
language. You need to post C++ questions in < Even
though such etiquette seems to have been lost in the invasion of the
barbarians, you might consider following it by checking the FAQ and post
postings to that newsgroup before posting there.

You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstring> before posting there. You woll also want to check your
textbook on the use of namespaces and qualified identifiers, especially
those in the std namespace.
 
R

Richard

Martin Ambuhl said:
The above (and most of what follows) is not C, but C++, a different
language. You need to post C++ questions in <Even though such etiquette seems to have been lost in the invasion of
the barbarians, you might consider following it by checking the FAQ
and post postings to that newsgroup before posting there.

You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstring> before posting there. You woll also want to check your
textbook on the use of namespaces and qualified identifiers,
especially those in the std namespace.

I have a simple question for a few posters here:

Which news servers do you use?

I see no reason whatsoever for 5 or 6 people to post the exact same
reprimand. The days of servers taking days to sync are over. Read the
responses before throwing in your response. Please.

Now you use Motzarella from what I can see. That is pretty damn quick. I
saw that OP and the replies ages ago. Why could you not do the same?

Unnecessary tellings off are worse than spam.
 
G

gw7rib

You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstring> before posting there.  You woll also want to check your
textbook on the use of namespaces and qualified identifiers, especially
those in the std namespace.

He doesn't need to do any of that, and he may not want to. Why not let
him progress at his own rate?
 
C

CBFalconer

yang2006 said:
I'm using Dev-c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class Node {
friend class List;
public:
Node(int d, Node* p = NULL, Node* n = NULL) : data(d),
prev(p), next(n){}+
private:
int data;
Node* prev;
Node* next;
};

You are also polluting this newsgroup with C++ code. Don't do
that. C++ is a different language.
 
J

jameskuyper

Martin said:
If he wants to write C++, he does. ...

No, the C standard headers all work in C++, though some of them have
minor modifications. When they are used, the C standard library
routines are provided both in the std:: namespace, and are also made
available in the global namespace, as if by use of a using-
declaration.
... If he wants to avoid unmerciful
flames in posting to comp.lang.c++, he does. ...

There are idiots in every newsgroup who will flame you without good
cause.
 
K

Kenny McCormack

Richard said:
Which news servers do you use?

I see no reason whatsoever for 5 or 6 people to post the exact same
reprimand. The days of servers taking days to sync are over. Read the
responses before throwing in your response. Please.

Now you use Motzarella from what I can see. That is pretty damn quick. I
saw that OP and the replies ages ago. Why could you not do the same?

Unnecessary tellings off are worse than spam.

You are a mean, sadistic man. You want to deprive CBF, the Loser, and
others, of the only semblance of a life they have ever known.
 
R

Richard

You are a mean, sadistic man. You want to deprive CBF, the Loser, and
others, of the only semblance of a life they have ever known.

I simply do not believe the usual suspects when they say they did not
see the other tellings off. They are piling in to get their name up in
lights again. It's ridiculous. I would also bet a pound to a penny that
Vippstar's little post about how to have const members in dynamically
allocated structures was "based" on a previous post in that very same
thread. There are two many patterns to recognise for it not to have
been. All very silly.
 
I

Ian Collins

Martin said:
If he wants to write C++, he does.

No, he doesn't
If he wants to avoid unmerciful
flames in posting to comp.lang.c++, he does.

No, he doesn't

Using the C standard headers is common and acceptable practice in C++.
 
I

Ian Collins

CBFalconer said:
You are also polluting this newsgroup with C++ code. Don't do
that. C++ is a different language.
Yet again you repeat something that was posted 10 hours before your post.

Why?
 
K

Kenny McCormack

Richard said:
I simply do not believe the usual suspects when they say they did not
see the other tellings off. They are piling in to get their name up in
lights again. It's ridiculous. I would also bet a pound to a penny that
Vippstar's little post about how to have const members in dynamically
allocated structures was "based" on a previous post in that very same
thread. There are two many patterns to recognise for it not to have
been. All very silly.

All of what you say is, of course, completely accurate and on-the-money
in terms of its analysis of the personalities here.

But it is still just so mean and nasty to be depriving these shut-ins of
their one chance at glory.
 
R

Richard

Ian Collins said:
Yet again you repeat something that was posted 10 hours before your post.

Why?

If you look at the off topic rant replies you will see the same people
doing it time and time again. And they are simply lying in most cases
when they say their news servers did not get the previous replies. I'm
glad to see that a few of the regs have stopped doing to as
often. Default User being a particularly bad offender has finally seen
the light I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top