写了个双向链表,有错,不知道原因

napoleonpan 2004-02-02 03:12:49
一共有两处错误
/* DNodeStr.txt */

#include <stdio.h>
#define ElemType char

struct DNode
{
ElemType data;
struct DNode *right, *left;
};

/* DNodeMut.txt */ /* error message: compound statement missing } in function insert */
#include "DNodeStr.txt"

setnull (struct DNode **p)
{
*p=NULL;
}

int length (struct DNode **p)
{
int n=0;
struct DNode *q;
q=*p;
while (q!=NULL)
{
n++;
q=q->right;
}
return (n);
}

ElemType get (struct DNode **p, int i)
{
int j;
struct DNode *q;
q=*p;
for (j=1; j<i && q!=NULL; j++)
q=q->right;
if (q!=NULL)
return (q->data);
else
printf ("wrong location number!\n");

}

int locate (struct DNode **p, ElemType x)
{
int j=1;
struct DNode *q;
q=*p;
while (q!=NULL && q->data!=x)
{
q=q->right;
j++;
}
if (q!=NULL)
return (j);
else
return (-1);
}

void insert (struct DNode **p, ElemType x, int i)
{
int j;
struct DNode *s,*q;
q=*p;

s=(struct DNode *) malloc (sizeof (struct DNode));
s->data=x;
s->left=NULL;
s->right=NULL;

if (i==1)

{
s->right=q;
q->left=s;
*p=s;
}
else
{
for (j=1; j<i-1 && q->right!=NULL; j++)
q=q->right;
if (j==i-1)
{
if (q->right!=NULL)
{
s->right=q->right;
q->right->left=s;
q->right=s;
s->left=q;
}
else
{
q->right=s;
s->left=q;
}
}
else
printf("wrong location number!\n");
}

void del (struct DNode **p, int i) /* error message: expression syntax in function insert */
{
struct DNode *q,*t;
q=*p;
t=q;
if (i==1)
{
q=q->right;
if (q!=NULL)
{
q->left=NULL;
*p=q;
}
}
else
{
for (int j=1; j<i-1 && q->right!=NULL; j++)
q=q->right;
if (j=i-1)
{
if (q->right->right!=NULL)
{
t=q;
q->right=t->right->right;
q->right->left=t;
}
else
q->right=NULL;
}
else
printf("wrong location number!\n");
}
}

void display(struct DNode **p)
{
struct DNode *q;
q=*p;
printf ("display the Double Node table: \n");
if (q==NULL)
printf ("Empty Double Node table.\n");
else if (q->right==NULL)
printf ("%c\n ", q->data);
else
{
while (q->right!=NULL)
{
printf ("%c->", q->data);
q=q->right;
}
printf ("%c \n", q->data)
}
}




/* DNodeMai.txt */
#include "DNodeMut.txt"

int main()
{
struct DNode *head;
setnull (&head);
insert (&head, 'a', 1);
insert (&head, 'b', 2);
insert (&head, 'c', 2);
insert (&head, 'd', 1);
display (&head); /*the output should be: dacb */
printf("The length of the LNode is: %d \n", length (&head));
printf("The location of the elem a is: %d \n", locate(&head, 'a'));
printf("The value of the location 3 is: %c \n", get (&head, 3));
del (&head, 2);
display (&head); /*the output should be: dcb */
printf("The length of the LNode is: %d \n", length (&head));
}
...全文
46 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
napoleonpan 2004-02-03
  • 打赏
  • 举报
回复
各位,这个链表是我参照书上的提示写的,本人觉着对链表的算法好像也只能这么写啊?要不大家提示一下,还能怎么写?
比如insert函数,就是先把一个节点的尾部接上链表,再把头部连上,我甚至想不出第二种方法来插入,大家如果觉着我写的不规范的,能给点提示吗?
还有,这个程序执行以后,输出的结果最后一行总是多显示一个:NULL POINT ASSINMENT.
不知道是怎么回事。
wangfeng20004 2004-02-02
  • 打赏
  • 举报
回复
也许这也是种好方法,书上规则要遵守,但其它所有算法结构等问题,各抒己见!!有时往往一些小错误会让人哭笑不得!!
HeartSmiling 2004-02-02
  • 打赏
  • 举报
回复
老兄,你的链表函数接口设计有点问题。例如你的Insert函数。关于算法的教材看过一点,这么怪的插入函数很少见到。
napoleonpan 2004-02-02
  • 打赏
  • 举报
回复
谢谢诸位,已调试成功
DoItFreely 2004-02-02
  • 打赏
  • 举报
回复
其实只有一个语法错误,在insert()函数的末尾,少写了一个}
这么一来,下一个函数的定义被编译器认为是insert()里面定义的一个变量 - 而C/C++语言不允许函数里面定义函数,so……
dddd8888 2004-02-02
  • 打赏
  • 举报
回复
1.printf ("%c \n", q->data) 少一个分号

2.else
printf("wrong location number!\n");

这句话不要,但你写的程序还是有错误
napoleonpan 2004-02-02
  • 打赏
  • 举报
回复
有好心人帮我看一下吗?
(自己顶一下,希望有人回答)
dddd8888 2004-02-02
  • 打赏
  • 举报
回复
但原理必须明白,否则用着也没意思
sunriselx 2004-02-02
  • 打赏
  • 举报
回复
何必呢,用list多省事,肯定比自己写的好

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧