一个关于链表的程序,我将链表的建立、插入,删除等源程序合在一起却无法运行,求解!
#include <stdio.h>#include <stdio.h>
#include <malloc.h>
struct node *creat()
struct node
{int num;
struct node *next;};
{int n,i;
struct node *head, *new;
struct node *p;
print("input the node num\n");
if (n>0)
{printf(input %d date:\n",n):
head=(struct node*)malloc(sizeof(struct node));
p=head;
if(head!=NULL)
{ scanf("%d",&head->num); head->next=NULL; }
else
{ printf("out of momery\n"); exit(0); }
for(i=1;i<n;i++)
{ new=(struct node*)malloc(sizeof(struct node));
if(new!=NULL)
{ scanf("%d",&new->num);
p->next=new; /* 新结点连在表尾 */
p=new; /* 新结点为表尾结点*/
}
else
{ printf("out of momery\n"); exit(0); }
p->next=NULL; /* 表尾置 NULL */
} }
else
{ printf("n<=0\n"); head=NULL; }
return head;
}
struct node *insert(struct node *head, int num)
{ struct node *p, *new, *q;
new=(struct node*)malloc(sizeof(struct node));
if(new!=NULL)
{ new->num=num;
if (num<head->num || head= =NULL)
{ new->next=head; head=new; /* 插入表头 */ }
else
{ p=head;
while (p!=NULL&&p->num<num) /*查找插入位置**/
{ q=p; p=p->next; }
new->next=p; q->next=new; /*插入表中或表尾*/
}
return(head);
}
else
{ printf("out of momery\n"); exit(0); }
}
struct node *delete(struct node *head,int n)
{ struct node *p, *q;
if (head!=NULL)
{q=head;
if(head->num= =n)
{ head=head->next; free(q); } /* 删除头结点 */ else
{ while (q!=NULL&&q->num!=n)
{ p=q; q=q->next; } /* 查找待删结点 q */
if (q= =NULL)
printf("%d not been found\n",n); /* 没找到待删结点 */
else
{ p->next=q->next; free(q); } /* 删除结点q */
}}
else
printf("the list empty\n"); /* 表空 */
return head;
}