一道c题 运行出错 。。。。。
#include <stdio.h>#include <stdlib.h>
#include <time.h>
typedef struct dhlink{
int num;
struct dlink *next;
}DHLINK,*P;
void main()
{int n,m;
p head,p1,p2;
p insert(p,p,p);
p delet(p,int);
void output(p);
randomize();
m=random(10)+8;
head=(p)malloc(sizeof(DHLINK));
head->next=NULL;
for(n=1,p2=head;n<=m;n++)
{p1=(p)malloc(sizeof(DHLINK));
p1->num=n;
p1->next=NULL;
p2=insert(head,p1,p2);
}
output(head);
head=delet(head,m);
output(head);
getchar();
getchar();
}
void output(p head)
{p p1=head->next;
do
{printf("%4d",p1->num);
p1=p1->next;
}
while(p1!=head->next);
printf("\n");
}
p insert(p head,p p1,p p2)
{p1->next=head;
head->next=p2;
return (head);
}
p delet(p head,int m)
{p p1,p2=head;
int n,i;
n=random(10)+1;
printf("n=%d\n",n);
for(i=1;i<=n;p2=p2->next,i++);
while(m>2)
{p1=p2;
p2=p2->next;
p1=p2;
p2=p2->next;
p1->next=p2->next;
if(p2==head->next) head->next=p2->next;
free(p2);
m--;
p2=p1->next;
}
head->next=p2;
return head;
}