- 注册时间
- 2004-10-13
- 最后登录
- 2019-5-15
⑧专业
*永恒国度*
- 积分
- 14145

|
[code]
#include<stdio.h>
#include<stdlib.h>
struct list
{
char data;
struct list *next;
}*head;
setnull(struct list **p)
{
*p=NULL;
}
int length(struct list **p)
{
int n=0;
struct list *q=*p;
while(q!=NULL)
{
n++;
q=q->next;
}
return n;
}
void insert(struct list **p,char x,int i)
{
int j=1;
struct list *s,*q;
s=(struct list *)malloc(sizeof(struct list));
s->data=x;
q=*p;
if(i==1)
{
s->next=q;
*p=s;
}
else
{
while(j<i-1&&q->next!=NULL)
{
q=q->next;
j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置不正确.");
}
}
void del(struct list **p,int i)
{
int j=1;
struct list *t,*q;
q=*p;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j<i-1&&q->next!=NULL)
{
q=q->next;
j++;
}
if(j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置不对.");
}
if(t!=NULL)
free(t);
}
void display(struct list **p)
{
struct list *q;
q=*p;
printf("单链表显示:");
if(q==NULL)
printf("链表为空!");
else if(q->next==NULL)
printf("%c",q->data);
else
{
while(q->next!=NULL)
{ printf("%c-",q->data);
q=q->next;
}
printf("%c",q->data);
}
printf("\n");
}
void main()
{
char ch;
setnull(&head);
printf("请输入字符:");
scanf("%c",&ch);fflush(stdin);
insert(&head,ch,1);
insert(&head,'c',2);
insert(&head,'b',2);
printf("长度为%d\n",length(&head));
display(&head);
del(&head,1);
display(&head);
}
[/cide]
在这程序中,在表头是赋了个NULL 但是在插入值以后并没有在最后一个值后赋以NULL 那像
while(j<i-1&&q->next!=NULL)
{
q=q->next;
j++;
怎么来判断q->next是否等于NULL 它在插入后差没给最后一个赋NULL
请问这到底是怎么理解呢?[s:6][s:6][s:6] |
|