来帮忙改,链表的。
#include <stdlib.h>#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct node
{
int num;
char str[20];
struct node *next;
}
main()
{
struct node *creat();
struct node *insert();
struct node *delet();
void print();
struct node *head;
char str[20];
int n;
head=NULL;
head=creat();
print();
printf("\n input inserted num,name:\n");
gets(str);
n=atoi(str);
gets(str);
head=insert();
print();
printf("\n input deleted name:\n");
gets(str);
head=delet();
print();
return;
}
struct node *creat(struct node *head)
{
char temp[30];
struct node *p1,*p2;
p2=p1=(struct node *)malloc(sizeof(struct node));
gets(temp);
gets(p1->str);
p1->num=atoi(temp);
p1->next=NULL;
while(strlen(p1->str)>0)
{
if(head==NULL)
head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct node *)malloc(sizeof(struct node));
printf("\n input inserted num,name:\n");
gets(p1->str);
gets(temp);
p1->num=atoi(temp);
p1->next=NULL;
}
return head;
}
struct node *insert(struct node *head,char *pstr,int n)
{
struct node *p1,*p2,*p3;
p1=(struct node *)malloc(sizeof(struct node));
strcpy(p1->str,pstr);
p1->num=n;
p2=head;
if(head=NULL)
{
head=p1;
p1->next=NULL;
}
else
{
while(n>p2->num&&p2->next!=NULL)
{
p3=p2;
p2=p2->next;
}
if(n<=p2->num)
if(head==p2)
{
head=p1;
p1->next=p2;
}
else
{
p3->next=p1;
p1->next=p2;
}
else
{
p2->next=p1;
p1->next=NULL;
}
}
return head;
}
struct node *delet(struct node *head,char *pstr)
{
struct node *p,*temp;
temp=head;
if(head==NULL)
printf("\nList is null!\n");
else
{
temp=head;
while(strcmp(pstr,temp->str)!=0&&temp->next!=NULL);
{
p=temp;
temp=temp->next;
}
if(strcmp(pstr,temp->str)==0)
{
if(head==temp)
{
head=head->next;
free(temp);
}
else
{
p->next=temp->next;
free(temp);
}
}
else
printf("no find in the line");
}
return head;
}
void print(struct node *head)
{
struct node *temp;
temp=head;
printf("\noutput strings:\n");
while(temp!=NULL)
{
printf("\n%d-----%s\n",temp->num,temp->str);
temp=temp->next;
}
return;
}
自己改了很多遍就是运行不了,知道出现一下看不懂的东西之后我就不会改了,它说有致命错误,4个外部什么,高手帮忙改下顺便解释,谢谢