为什么输出会显示NULL pointer assignment?

napoleonpan 2004-02-03 02:01:01
这是一个双向链表
/* DNodeStr.txt */

#include <stdio.h>
#define ElemType char

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

/* DNodeMut.txt */
#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)
{
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));
}
...全文
69 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
feng8208 2004-02-03
  • 打赏
  • 举报
回复
有对空指针所指的地址(NULL)赋值的操作
hell190109 2004-02-03
  • 打赏
  • 举报
回复
指针为空?

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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