主题:一个有问题链表的追加,删除程序,帮忙分析一下
#include "stdio.h"
#include "stdlib.h"
typedef struct name_link{
char *name;
struct name_link *next;
}*name_ptr,name_struct; /* 定义一个以名字为分量的结构体*/
void show_link(name_ptr head)
{
name_ptr temp1;
temp1=head->next;
while(temp1)
{
printf("%s\n",temp1->name);
temp1=temp1->next;
}
} /*遍历单链表,显示所有结点*/
name_ptr search_end(name_ptr head)
{
name_ptr temp;
temp=head;
while(temp->next != 0)
temp=temp->next;
return temp;
}
void addname(name_ptr head)
{
name_ptr temp1,temp2;
char *newname;
temp2 = search_end(head);
temp1 =(name_ptr)malloc (sizeof(name_struct));
printf("\nplease input the name you want to add!\n");
scanf("%s",newname);
temp1->name=newname;
temp2->next = temp1;
temp1->next = 0;
} /*尾部追加新结点*/
name_ptr find_prev_name(name_ptr head,char *match_name)
{
name_ptr temp1;
temp1=head;
while(temp1->next != 0)
{
if(temp1->next->name == match_name) return temp1;
temp1=temp1->next;
}
return 0;
} /*找出名字匹配的结点*/
void del_node(name_ptr head,char * name)
{
name_ptr temp1,temp2;
temp1=find_prev_name(head,name);
if(temp1)
{
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}
}
int main()
{
name_ptr head;
char choice;
char *del_name;
/*head=(name_ptr)malloc(sizeof(name_struct));*/
head->next=0;
printf("\n please input your choice:\n");
printf(" 1.A Add a new name;\n");
printf(" 2.D Delete a name;\n");
printf(" 3.S Show the list;\n");
printf(" 4.E End the program\n");
scanf("%c",&choice);
while(choice != 'E' || 'e')
{
switch(choice)
{
case 'a':
case 'A':
{
addname(head);
break;
}
case 'd':
case 'D':
{
printf("\nplease input the name you want to delete!");
scanf("%s",del_name);
del_node(head,del_name);
break;
}
case 's':
case 'S':
{
show_link(head);
break;
}
case 'e':
case 'E':
return(0);
default:
break;
}
printf("\nplease input your choice!");
scanf("%c",&choice);
}
}
#include "stdlib.h"
typedef struct name_link{
char *name;
struct name_link *next;
}*name_ptr,name_struct; /* 定义一个以名字为分量的结构体*/
void show_link(name_ptr head)
{
name_ptr temp1;
temp1=head->next;
while(temp1)
{
printf("%s\n",temp1->name);
temp1=temp1->next;
}
} /*遍历单链表,显示所有结点*/
name_ptr search_end(name_ptr head)
{
name_ptr temp;
temp=head;
while(temp->next != 0)
temp=temp->next;
return temp;
}
void addname(name_ptr head)
{
name_ptr temp1,temp2;
char *newname;
temp2 = search_end(head);
temp1 =(name_ptr)malloc (sizeof(name_struct));
printf("\nplease input the name you want to add!\n");
scanf("%s",newname);
temp1->name=newname;
temp2->next = temp1;
temp1->next = 0;
} /*尾部追加新结点*/
name_ptr find_prev_name(name_ptr head,char *match_name)
{
name_ptr temp1;
temp1=head;
while(temp1->next != 0)
{
if(temp1->next->name == match_name) return temp1;
temp1=temp1->next;
}
return 0;
} /*找出名字匹配的结点*/
void del_node(name_ptr head,char * name)
{
name_ptr temp1,temp2;
temp1=find_prev_name(head,name);
if(temp1)
{
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}
}
int main()
{
name_ptr head;
char choice;
char *del_name;
/*head=(name_ptr)malloc(sizeof(name_struct));*/
head->next=0;
printf("\n please input your choice:\n");
printf(" 1.A Add a new name;\n");
printf(" 2.D Delete a name;\n");
printf(" 3.S Show the list;\n");
printf(" 4.E End the program\n");
scanf("%c",&choice);
while(choice != 'E' || 'e')
{
switch(choice)
{
case 'a':
case 'A':
{
addname(head);
break;
}
case 'd':
case 'D':
{
printf("\nplease input the name you want to delete!");
scanf("%s",del_name);
del_node(head,del_name);
break;
}
case 's':
case 'S':
{
show_link(head);
break;
}
case 'e':
case 'E':
return(0);
default:
break;
}
printf("\nplease input your choice!");
scanf("%c",&choice);
}
}