循环链表的建立打印出错,大家帮忙看看!
#include<stdio.h>#include<malloc.h>
#define M 10
/***定义结构体***/
typedef struct Node_List
{
int num;
Node_List *next;
}node;
/***循环链表的建立***/
node *creat(void)
{
int x;
int n;
node *head;
node *p1,*p2;
head=(node*)malloc(sizeof(node));
for(n=1;n<=M;n++)
{
p1=(node*)malloc(sizeof(node));
p1->num=n;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
}
p1->next=head;
return head;
}
/***链表打印***/
void print(node *head)
{
node *p;
p=head;
do
{
printf("%d ",*p);
p++;
}while(p->next!=head);
}
main()
{
node *head;
head=creat();
print(head);
}