首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言创建循环双链表找异常

2012-11-03 
C语言创建循环双链表找错误求帮忙找错误、我这个是创建一个循环双链表、然后输出值、但为什么一直出错呢?#inc

C语言创建循环双链表找错误
求帮忙找错误、我这个是创建一个循环双链表、然后输出值、但为什么一直出错呢?
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *prior;
struct node *next;
}node,linklist;
node *creat()
{
int flag=1;
char ch;
node *p,*s,*head;
head=(node*)malloc(sizeof(node));
p=head;
while(flag)
{
scanf("%c",&ch);
if(ch!='$')
{
s=(node*)malloc(sizeof(node));
s->data=ch;
p->next=s;
s->prior=p;
p=s;
}
else{
flag=0;
}
}
head=head->next;
head->prior=NULL;
p->next=NULL;
return head;
}
void main()
{


node *s;
s=creat();
while(s->next!=NULL)
printf("%c\n",s->data);
}

[解决办法]
1.最起码要while(s)
{
printf("%c\n",s->data);
s=s->next;
}
2.你说的是双向循环链表,但是你建表的时候,又把第一个节点前驱和最后节点后继赋为空了,不是循环链表。

热点排行