473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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;
}
Sep 21 '08 #1
17 1413
yang2006 wrote:
I'm using Dev-c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Node
[...]
You're in the wrong newsgroup. comp.lang.c++ is down
the hall to your left, second door past the water cooler.
Yes, the room with the bad karaoke party. Have fun!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 21 '08 #2
"yang2006" writes:

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
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();
#endif
system("PAUSE");
return 0;
}

Sep 21 '08 #3
yang2006 wrote:
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
Sep 21 '08 #4
yang2006 wrote:
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 <news:comp.lang.c++>. 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
<cstringbefore posting there. You woll also want to check your
textbook on the use of namespaces and qualified identifiers, especially
those in the std namespace.
Sep 21 '08 #5
osmium wrote:
"yang2006" writes:

Look more closely at your screen. Actually it *does* compile,
Not with a C compiler. Don't mislead the clueless.
Sep 21 '08 #6
Martin Ambuhl <ma*****@earthlink.netwrites:
yang2006 wrote:
>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 <news:comp.lang.c++>.
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
<cstringbefore 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.
Sep 21 '08 #7
On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthlink.netwrote:
You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstringbefore 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?
Sep 21 '08 #8
gw****@aol.com wrote:
On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthlink.netwrote:
>You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstringbefore 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?
If he wants to write C++, he does. If he wants to avoid unmerciful
flames in posting to comp.lang.c++, he does. Why don't you crawl back
under your bridge?

Sep 21 '08 #9
yang2006 wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 22 '08 #10


Martin Ambuhl wrote:
gw****@aol.com wrote:
On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthlink.netwrote:
You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstringbefore 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?

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.
Sep 22 '08 #11
In article <gb**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
....
>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.

Sep 22 '08 #12
ga*****@shell.xmission.com (Kenny McCormack) writes:
In article <gb**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
...
>>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.
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.

Sep 22 '08 #13
Martin Ambuhl wrote:
gw****@aol.com wrote:
>On 21 Sep, 18:25, Martin Ambuhl <mamb...@earthlink.netwrote:
>>You will want to change your C headers, <stdio.h>, <stdlib.h>,
<string.h>, to be the C++ standard headers <cstdio>, <cstdlib>, and
<cstringbefore 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?

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

--
Ian Collins.
Sep 22 '08 #14
CBFalconer wrote:
>
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?

--
Ian Collins.
Sep 22 '08 #15
In article <gb**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
....
>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.

Sep 22 '08 #16
In article <6j************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>CBFalconer wrote:
>>
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?
It's the only life he will ever know.

Sep 22 '08 #17
Ian Collins <ia******@hotmail.comwrites:
CBFalconer wrote:
>>
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?
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.
Sep 22 '08 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Schade | last post by:
I have a large project I am developing in C#. I have two problems that occur many times a day. 1. Frequently when I edit the code by typing in the IDE editor all of the color coding disappears...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
41
by: SkyBlue | last post by:
Hi, can someone explain why the following simple C code segfaulted? I've stared it for 30mins but couldn't find the problem. Thx in advance #include <stdio.h> int main() { char *one; char...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.