当前位置: 代码迷 >> C语言 >> [讨论]打死我都不相信有错误(双向链表)
  详细解决方案

[讨论]打死我都不相信有错误(双向链表)

热度:325   发布时间:2007-10-10 22:09:30.0
[讨论]打死我都不相信有错误(双向链表)
如下是我的代码:
错误是结构体的定义
'node' : 'struct' type redefinition
大家说说为什么错(vc6.0)


搜索更多相关的解决方案: 链表  

----------------解决方案--------------------------------------------------------

#define YES 1
#define NO 0
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
struct node *prior;
}*dlink;

/***********************************************/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include"2-3define.h"
dlink creatdlink(int n)
{
int j;
dlink head,p,s;
p=head=(dlink)malloc(sizeof(dlink));
for(j=0;j<n;j++)
{
s=(dlink)malloc(sizeof(dlink));
printf("Input the %d data:\n",j);
scanf("%d",&s->data);
p->next=s;
s->next=NULL;
s->prior=p;
p=s;
}//P->prior通个p=s来实现
p->next=NULL;
return head;
}
int getlen(dlink head)
{
int j=0;
dlink p;
p=head->next;
while(p!=NULL)
{
j++;
p=p->next;
}
return j;
}
int getelem(dlink head ,int n)
{ dlink q;
int j;
q=head->next;
if(head->next==NULL)
printf("The link is empty !\n");
return NO;
while(q!=NULL&&j<n)
{
q=q->next;
j++;
}
if(q==NULL)
return NO;
else
return q->data;
}
int locate(dlink head ,elemtype x)

{
dlink q;
int j=1;
q=head->next;
if(head->next=NULL)
printf("The link is empty !\n");
while(q!=NULL&&q->data!=x)
{
q=q->next;
j++;
}
if(q==NULL)
return NO;
else
return j;
}
int delete(dlink head ,int q)
{
dlink p,s;
int j;
if(q<1)
printf("wrong input:\n");
return NO;
p=head ;
j=0;
while(p->next!=NULL&&j<q)
{
p=p->next;
j++;
}
if(p->next==NULL)
return NO;
s=p->next;
p->next=s->next;
if(s->next!=NULL)
s->next->prior=p;
free(s);
return YES;
}
int insert(dlink head ,int p,elemtype x)
{
dlink q,s;
int j;
if(p<1)
printf("wrong input:\n");
return NO;
q=head;
j=0;
while (q!=NULL&&j<p-1)
{
q=q->next;
j++;
}
if(q==NULL)
return NO;
s=(dlink)malloc(sizeof(dlink));
s->data=x;
s->next=q->next;
s->prior=q;
if(q->next!=NULL)
q->next->prior=s;
q->next=s;
return YES;
}
void list(dlink head)
{
dlink p;
p=head->next;
while(p!=NULL)
{
printf("%4d",p->next->data);
p=p->next;
}
printf("\n");
while(p!=NULL)
{
printf("%4d",p->data);
p=p->prior;
}
printf("\n");

}
/*******************************************************/
#include<stdio.h>
#include"2-3define.h"
#include"2-3lib.h"
void main()
{
int i;
dlink H;

dlink creatdlink(int n);
void list(dlink head);
i=4;
H=creatdlink(i);
list(H);
}
//随意用的两个函数




----------------解决方案--------------------------------------------------------
重复包含重复声明错误
主cpp里struct node 因为有两次包含,所以重复



by 雨中飞燕 QQ:78803110 C/C++讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
Blog: http://yzfy.programfan.com

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
----------------解决方案--------------------------------------------------------
好象是说 结构体 node被重复定义了..
----------------解决方案--------------------------------------------------------
对啊,怎么办
----------------解决方案--------------------------------------------------------
#include"2-3define.h"
#include"2-3lib.h"

看看这里面,应该会有node的定义,所以你重复定义了.
----------------解决方案--------------------------------------------------------
如果有相同的 node定义就改成别的.
你把这两个头文件有关 node的定义发上来看看,
我估计是这里面的重复定义.
----------------解决方案--------------------------------------------------------
在最上面,我不知道怎么改:
下面的方法不行typedef struct node
{
elemtype data;
struct node *next,*prior;

}*dlink;
----------------解决方案--------------------------------------------------------
用查找与替换把所有node替换成别的,比如node1看看行不行
----------------解决方案--------------------------------------------------------

#define YES 1
#define NO 0
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
struct node *prior;
}*dlink;

改:
#ifndef _MYDEFINEFILE_H
#define _MYDEFINEFILE_H

#define YES 1
#define NO 0
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
struct node *prior;
}*dlink;

#endif



by 雨中飞燕 QQ:78803110 C/C++讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
Blog: http://yzfy.programfan.com

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]


----------------解决方案--------------------------------------------------------
  相关解决方案