龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

自己写的一个链表综合程序

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
#include #include #include typedef strUCt elem/*定义接点*/ { char name[10]; struct elem *next; }create; create *head,*van,*cur,*temp;/*定义头接点(head),前驱接点(van),当前接点(cur),零时接点(temp)*/ void menu()/*创建菜单

#include

  #include

  #include

typedef strUCt elem/*定义接点*/

   {

   char name[10];

   struct elem *next;

   }create;

create *head,*van,*cur,*temp;/*定义头接点(head),前驱接点(van),当前接点(cur),零时接点(temp)*/

void menu()/*创建菜单*/

   {

   printf("1.创建新链表------[1]

");

   printf("2.插入新元素------[2]

");

   printf("3.删除旧元素------[3]

");

   printf("4.查找旧元素------[4]

");

   printf("5.倒置原链表------[5]

");

   printf("6.显示所有元素----[6]

");

   printf("7.退出------------[7]

");

   printf("请选择(1~7): ");

   }

  void new(int n)/*创建新链表,参数n为链表长度*/

   {

   int i;

   printf("

");

   if((head=(create *)malloc(sizeof(create)))==NULL)/*定义头接点*/

   {

   printf("

不能创建链表");

   exit(1);

   }

   van=head;/*将前驱接点指针指向头接点*/

   for(i=1;i<=n;i++)

   {

   if((cur=(create *)malloc(sizeof(create)))==NULL)/*定义新接点*/

   {

   printf("

不能创建链表");

   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("

");

   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("

不能创建链表");

   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("

请输入你要建立的链表的长度: ");

   scanf("%d",&length);

   new(length);

   printf("

链表已创建,按任意键返回

");

   getch();

   break;

   case 2:

   if(head==NULL)

   {

   printf("

请先建立链表,按任意键返回

");

   getch();

   break;

   }

   printf("

以下为链表中原有元素: ");

   print();

   printf("

请输入你要在哪个名字后面插入新名字: ");

   scanf("%s",&tempname);

   temp=research(tempname);

   if((temp=research(tempname))==NULL)

   {

   printf("

没有找到你要输入的名字,按任意键返回

");

   getch();

   }

   else

   {

   printf("

请输入你要插入的名字: ");

   scanf("%s",&tempname);

   insert(temp,tempname);

   printf("

  

  

精彩图集

赞助商链接