双链表问题
创建+打印双链表问题在注释里
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define SIZE sizeof(snode)
typedef struct student
{
int num;
struct student *next;
struct student *prior;
}snode;
snode *create1();
//创建链表
void print(snode *head); //打印链表
void main(void)
{
snode *head1;
printf("\n链表为:\n");
head1=create1();
printf("\n链表为:\n");
print(head1);
}
snode *create1()
{
snode *head,*r,*s;
FILE *fp;
int num1;
head=(snode *)malloc(sizeof(SIZE));
head->next=head->prior=NULL;
r=head;
fp=fopen("D:\\qq.txt","r");
while(!feof(fp))
{
fscanf(fp,"%d",&num1); //
s=(snode *)malloc(sizeof(SIZE));
s->num=num1;s->next=NULL;s->prior=r;
r->next=s;
r=s;
}
return head;
}
void print(snode *head)
{
snode *p;
p=head->next; //带头结点
while(p!=NULL)
{ // 输出时,最后一个元素重复了,为什么啊???
printf("%6d",p->num); //怎么是p->next !=NULL 呢?????????? 应该是p!=NULL啊!!!!!!
p=p->next;
}
}
[ 本帖最后由 zd123 于 2009-9-10 11:31 编辑 ]