编译环境为TC2.0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *pnext;
};
void looplink(struct node *head,int n)
{
int i;
struct node *p;
struct node *q;
for(i=0;i <n;i++)
{
if(i==0)
{
p=(struct node*)malloc(sizeof(struct node));
head=p;//关键
q=p;//关键
}
else
{
p=(struct node*)malloc(sizeof(struct node));
q->pnext=p;//关键
q=p;//关键
}
}
q->pnext=head;
}
void main()
{
struct node *head;
struct node *p;
int n=8;
int a;
clrscr();
head=(struct node*)malloc(sizeof(struct node));
p=head;
looplink(head,8);
for(a=0;a <n;a++)
{
p->data=a+1;
printf("%d",p->data);
p=p->pnext;
}
free(head);
free(p);
scanf("%d",&a);
}