#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef strUCt elem/*定义接点*/
{
char name[10];
struct elem *next;
}create;
create *head,*van,*cur,*temp;/*定义头接点(head),前驱接点(van),当前接点(cur),零时接点(temp)*/
void menu()/*创建菜单*/
{
printf("\t\t\t1.创建新链表------[1]\n");
printf("\t\t\t2.插入新元素------[2]\n");
printf("\t\t\t3.删除旧元素------[3]\n");
printf("\t\t\t4.查找旧元素------[4]\n");
printf("\t\t\t5.倒置原链表------[5]\n");
printf("\t\t\t6.显示所有元素----[6]\n");
printf("\t\t\t7.退出------------[7]\n\n");
printf("请选择(1~7): ");
}
void new(int n)/*创建新链表,参数n为链表长度*/
{
int i;
printf("\n");
if((head=(create *)malloc(sizeof(create)))==NULL)/*定义头接点*/
{
printf("\n不能创建链表");
exit(1);
}
van=head;/*将前驱接点指针指向头接点*/
for(i=1;i<=n;i++)
{
if((cur=(create *)malloc(sizeof(create)))==NULL)/*定义新接点*/
{
printf("\n不能创建链表");
exit(1);
}
cur->next=NULL;/*将当前接点的后继指针置空*/
van->next=cur;/*连接接点*/
printf("输入第%d个人的名字: ",i);
scanf("%s",&cur->name);/*输入当前接点的数据域内容*/
van=cur;
}
}
create *research(char searchname[10])/*查找接点函数*/
{
van=head;
temp=head->next;
while(temp!=NULL)
{
if(strcmp(temp->name,searchname)==0)
{
return(temp);
}
else
{
van=temp;
temp=temp->next;
}
}
return(temp);
}
void print()/*显示链表函数*/
{
temp=head->next;
printf("\n");
while(temp!=NULL)
{
printf("%s ",temp->name);
temp=temp->next;
}
}
void insert(create *insert_point,char insert_name[10])/*插入接点函数*/
{
if((cur=(create *)malloc(sizeof(create)))==NULL)
{
printf("\n不能创建链表");
exit(1);
}
stpcpy(cur->name,insert_name);
cur->next=insert_point->next;
insert_point->next=cur;
}
void delete(create *delete_point)/*删除接点函数*/
{
van->next=delete_point->next;
free(delete_point);
}
int turnlist()/*倒置链表函数*/
{
van=head->next;
cur=van->next;
van->next=NULL;
while(cur!=NULL)
{
temp=cur->next;
cur->next=van;
van=cur;
cur=temp;
}
head->next=van;
}
main()
{
int select,length;
char tempname[10];
head=NULL;
while(1)
{
clrscr();
menu();
scanf("%d",&select);
switch(select)
{
case 1:
printf("\n请输入你要建立的链表的长度: ");
scanf("%d",&length);
new(length);
printf("\n链表已创建,按任意键返回\n");
getch();
break;
case 2:
if(head==NULL)
{
printf("\n请先建立链表,按任意键返回 \n");
getch();
break;
}
printf("\n以下为链表中原有元素: ");
print();
printf("\n请输入你要在哪个名字后面插入新名字: ");
scanf("%s",&tempname);
temp=research(tempname);
if((temp=research(tempname))==NULL)
{
printf("\n没有找到你要输入的名字,按任意键返回\n");
getch();
}
else
{
printf("\n请输入你要插入的名字: ");
scanf("%s",&tempname);
insert(temp,tempname);
printf("\