无法编译双向链表的代码。 [英] the code of two-way linked list failed to be compiled.
问题描述
我正在使用Dev-c。
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
class Node
{
朋友类列表;
public:
Node(int d,Node * p = NULL,Node *
n = NULL):data(d),prev(p),next( n){}
private:
int data;
Node * prev;
Node * next; < br $>
};
班级列表
{
public:
List(): head(NULL){}
~List()
{
Node * cur = head;
Node * prev = NULL;
while(cur!= NULL)
{
prev = cur;
cur = cur-> next;
删除prev;
}
}
void insert(int data)
{
Node * node = new Node(data);
if(head = NULL)
{
head = node;
return;
}
Node * cur = head;
while(cur-> next!= NULL)
{
if(数据< = cur->数据)
{
if(cur-> prev == NULL)
{
head = node;
head-> next = cur;
cur-> prev = head;
}
其他
{
node-> prev = cur-> prev;
node-> next = cur;
cur-> prev-> next = node; // ???
}
返回;
}
其他
{
cur = cur-> next;
}
}
if(cur-> data< ; =数据)
{
cur-> next = node;
node-> prev = cur;
}
其他
{
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;
}
其他
{
cur-> prev-> next = cur-> next;
cur-> next-> prev = cur-> prev;
delete cur;
}
return; < br $>
}
其他
{
cur = cur-> next;
}
}
if(cur-> da ta == da)
{
cur-> prev-> next = NULL;
delete cur;
}
}
无效显示()
{
节点* cur = head;
while(cur!= NULL)
{
printf("%d \ nn",cur - >数据);
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");
返回0;
}
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;
}
推荐答案
yang2006写道:
yang2006 wrote:
我正在使用Dev- c。
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
class Node
[...]
I''m using Dev-c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Node
[...]
你''在错误的新闻组中。 comp.lang.c ++下来
左边的大厅,第二扇门经过水冷却器。
是的,房间里有卡拉OK派对。玩得开心!
-
Eric Sosman
es ***** @ ieee-dot-org.inva 盖子
" yang2006"写道:
仔细看看你的屏幕。实际上它* *编译,它只是在打印任何内容之前使
出错。下一步:通过
隔离输入,将#if 0 --- #endif放在输入数据周围。这应该足够了,所以你可以根据自己的能力继续前进。代码中的两个基本注释
"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
我正在使用Dev-c。
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
class Node
{
朋友类列表;
public:
Node(int d,Node * p = NULL,节点*
n = NULL):data(d),prev(p),next(n){}
private:
int data ;
节点*上一个;
节点*下一个;
};
类列表
{
public:
List():head(NULL){}
~List()
{
节点* cur =头;
节点* prev = NULL;
while(cur!= NULL)
{
prev = cur;
cur = cur-> next;
删除上一页;
}
}
void insert(int data)
{
Node * node = new Node(data);
if(head = NULL)
{
head = node ;
返回;
}
节点* cur = head;
while(cur- >下一个!= 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 - >接着=节点; // ???
}
返回;
}
其他
{
cur = cur-> next;
}
}
if(cur - >数据< =数据)
{
cur-> next = node;
node-> prev = cur; < br $>
}
其他
{
node-> next = cur;
node-> prev = cur-> prev;
cur-> prev-> next = node;
}
}
void DelNode(int da)
{
Node * cur = head;
while (CUR->接着!= NULL)
{
if(cur-> data == da)
{
if (cur-> prev == NULL)
{
head = cur-> next;
delete cur;
}
其他
{
cur-> prev-> next = cur-> next;
cur-> next-> prev = cur-> prev;
delete cur;
}
return ;
}
其他
{
cur = cur-> next;
$
}
if(cur-> data == da)
{
cur-> prev-> next = NULL;
delete cur ;
}
}
无效显示()
{
节点* cur = head;
while(cur!= NULL)
{
printf("%d \ n",cur->数据);
cur = cur-> next;
}
}
私人:
节点*头;
};
int main (int argc,char * argv [])
{
List l;
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
#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();
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
#endif
system(" PAUSE");
返回0;
}
system("PAUSE");
return 0;
}
yang2006写道:
yang2006 wrote:
我正在使用Dev-c。
I''m using Dev-c.
不,你正在使用Dev-C ++
No, you''re using Dev-C++
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
class Node
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Node
C没有上课
C doesn''t have class
{
朋友类列表;
public:
{
friend class List;
public:
也不是朋友或公众
nor friend or public
Node(int d,Node * p = NULL,Node *
n = NULL):data(d),prev(p),next(n){}
private:
Node(int d,Node* p=NULL,Node*
n=NULL):data(d),prev(p),next(n){}
private:
或私有。 C ++确实有这些,所以你最好在
comp.lang.c ++中询问你的问题。如果你这样做,还要提到你从编译器获得的错误消息。
但是:你的代码用VisualC ++ Express完全编译,但是
开始时崩溃。
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;
节点* prev;
节点*下一个;
};
类列表
{
public:
List():head(NULL){}
~List()
{
Node * cur = head ;
节点* prev = NULL;
while(cur!= NULL)
{
prev = cur ;
cur = cur-> next;
删除上一页;
}
}
void insert(int data)
{
Node * nod e =新节点(数据);
if(head = NULL)
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)
我猜你最有可能写的是
if(head == NULL)
这里,因为这是C和C ++中的典型错误,你不是完全错误的b
OT在这里。
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)
{
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->接着=节点; // ???
}
返回;
}
其他
{
cur = cur-> next;
}
}
if(cur - >数据< =数据)
{
cur-> next = node;
node-> prev = cur; < br $>
}
其他
{
node-> next = cur;
node-> prev = cur-> prev;
cur-> prev-> next = node;
}
}
void DelNode(int da)
{
Node * cur = head;
while (CUR->接着!= NULL)
{
if(cur-> data == da)
{
if (cur-> prev == NULL)
{
head = cur-> next;
delete cur;
}
其他
{
cur-> prev-> next = cur-> next;
cur-> next-> prev = cur-> prev;
delete cur;
}
return ;
}
其他
{
cur = cur-> next;
$
}
if(cur-> data == da)
{
cur-> prev-> next = NULL;
delete cur ;
}
}
无效显示()
{
节点* cur = head;
while(cur!= NULL)
{
printf("%d \ n",cur->数据);
cur = cur-> next;
}
}
私人:
节点*头;
};
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.dplay();
system(PAUSE);
返回0;
}
{
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;
}
再见,Jojo
Bye, Jojo
这篇关于无法编译双向链表的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!