码迷,mamicode.com
首页 > 其他好文 > 详细

2.1顺序表(链表)

时间:2016-07-19 09:20:10      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

2-4 ChainList.h

 1 #include <stdlib.h>
 2 typedef struct Node
 3 {
 4     DATA data;
 5     struct Node *next;
 6 }ChainListType;
 7 ChainListType *ChainListAddEnd(ChainListType *head,DATA data);  //添加结点到链表末尾 
 8 ChainListType *ChainListAddFirst(ChainListType *head,DATA data);  //添加结点到链表首部 
 9 ChainListType *ChainListFind(ChainListType *head,char *key); //按关键字在链表中查找内容 
10 ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data);  //插入结点到链表指定位置 
11 int ChainListDelete(ChainListType *head,char *key);//删除指定关键字的结点 
12 int ChainListLength(ChainListType *head);//获取链表结点数量

2-5 ChainList.c

 1 #include <string.h>
 2 ChainListType *ChainListAddEnd(ChainListType *head,DATA data)  //添加结点到链表结尾 
 3 {
 4     ChainListType *node,*h;
 5     if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
 6     {
 7         printf("为保存结点数据申请内存失败!\n"); 
 8         return NULL;  //分配内存失败 
 9     }
10     node->data=data; //保存数据 
11     node->next=NULL;  //设置结点指针为空,即为表尾 
12     if(head==NULL)  //是头指针 
13     {
14         head=node;
15         return head;
16     }
17     h=head;
18     while(h->next!=NULL) //查找链表的末尾 
19         h=h->next ;
20     h->next=node;
21     return head;
22 }
23 ChainListType *ChainListAddFirst(ChainListType *head,DATA data) 
24 {
25     ChainListType *node,*h;
26     if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
27     {
28         printf("为保存结点数据申请内存失败!\n"); 
29         return NULL;  //分配内存失败 
30     }
31     node->data=data; //保存数据 
32     node->next=head;  //指向头指针所指结点 
33     head=node;        //头指针指向新增结点
34     return head; 
35 }
36 ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data)  //插入结点到链表指定位置 
37 {
38     ChainListType *node,*node1;    
39     if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) //分配保存结点的内容 
40     {
41         printf("为保存结点数据申请内存失败!\n"); 
42         return 0;  //分配内存失败 
43     }
44     node->data=data;  //保存结点中的数据 
45     node1=ChainListFind(head,findkey);
46     if(node1)  //若找到要插入的结点 
47     {
48         node->next=node1->next;  //新插入结点指向关键结点的下一结点 
49         node1->next=node;    //设置关键结点指向新插入结点 
50     }else{
51         free(node);//释放内存
52         printf("未找到插入位置!\n"); 
53     }
54     return head;//返回头指针
55 }
56 ChainListType *ChainListFind(ChainListType *head,char *key) //按关键字在链表中查找内容 
57 {
58     ChainListType *h;
59     h=head;       //保存链表头指针 
60     while(h)      //若结点有效,则进行查找 
61     {
62         if(strcmp(h->data.key,key)==0) //若结点关键字与传入关键字相同 
63             return h;  //返回该结点指针 
64         h=h->next; //处理下一结点 
65     }
66     return NULL; //返回空指针 
67 }
68 int ChainListDelete(ChainListType *head,char *key)
69 {
70     ChainListType *node,*h; //node保存删除结点的前一结点 
71     node=h=head;    
72     while(h)
73     {
74         if(strcmp(h->data.key,key)==0) //找到关键字,执行删除操作 
75         {
76             node->next=h->next;  //使前一结点指向当前结点的下一结点
77             free(h);  //释放内存 
78             return 1;
79         }else{
80             node=h;  //指向当前结点 
81             h=h->next; //指向下一结点 
82         }
83      }
84      return 0;//未删除 
85 }
86 int ChainListLength(ChainListType *head)//获取链表结点数量 
87 {
88     ChainListType *h;
89     int i=0;
90     h=head;
91     while(h)      //遍历整个链表 
92     {
93         i++; //累加结点数量 
94         h=h->next;//处理下一结点 
95     }
96     return i;//返回结点数量 
97 }

2-6 ChainListTest.c

 1 #include <stdio.h>
 2 typedef struct
 3 {
 4     char key[15];    //关键字
 5     char name[20];
 6     int age;
 7 }DATA;     //数据结点类型 
 8 #include "2-4 ChainList.h"
 9 #include "2-5 ChainList.c"
10 void ChainListAll(ChainListType *head) //遍历链表 
11 {
12     ChainListType *h;
13     DATA data;
14     h=head;
15     printf("链表所有数据如下:\n"); 
16     while(h) //循环处理链表每个结点 
17     {
18         data=h->data;//获取结点数据 
19         printf("(%s,%s,%d)\n",data.key,data.name,data.age); 
20         h=h->next;//处理下一结点 
21     }
22     return;
23 }
24 int main()
25 {
26     ChainListType *node, *head=NULL;
27     DATA data;
28     char key[15],findkey[15];
29     
30     printf("输入链表中的数据,包括关键字、姓名、年龄,关键字输入0,则退出:\n"); 
31     do{
32         fflush(stdin);  //清空输入缓冲区 
33         scanf("%s",data.key);
34         if(strcmp(data.key,"0")==0) break; //若输入0,则退出
35         scanf("%s%d",data.name,&data.age);
36         head=ChainListAddEnd(head,data);//在链表尾部添加结点数据 
37     }while(1);
38     
39     printf("该链表共有%d个结点。\n",ChainListLength(head)); //返回结点数量 
40     ChainListAll(head); //显示所有结点
41  
42     printf("\n插入结点,输入插入位置的关键字:") ;
43     scanf("%s",&findkey);//输入插入位置关键字 
44     printf("输入插入结点的数据(关键字 姓名 年龄):");
45     scanf("%s%s%d",data.key,data.name,&data.age);//输入插入结点数据 
46     head=ChainListInsert(head,findkey,data);//调用插入函数 
47     
48     ChainListAll(head); //显示所有结点
49     
50     printf("\n在链表中查找,输入查找关键字:");
51     fflush(stdin);//清空输入缓冲区 
52     scanf("%s",key);//输入查找关键字 
53     node=ChainListFind(head,key);//调用查找函数,返回结点指针 
54     if(node)//若返回结点指针有效 
55     {
56         data=node->data;//获取结点的数据 
57         printf("关键字%s对应的结点数据为(%s,%s,%d)\n" ,key,data.key,data.name,data.age);        
58     }else//若结点指针无效 
59         printf("在链表中未找到关键字为%s的结点!\n",key); 
60     
61     printf("\n在链表中删除结点,输入要删除的关键字:");
62     fflush(stdin);//清空输入缓冲区 
63     scanf("%s",key);//输入删除结点关键字 
64     ChainListDelete(head,key); //调用删除结点函数 
65     ChainListAll(head); //显示所有结点   
66     getch();
67     return 0;
68 }

2-7 AddressList.c

  1 typedef struct
  2 {
  3     char key[15];    //关键字(设置姓名为关键字)
  4     char addr[20];
  5     char telephone[15];
  6     char mobile[12];
  7 }DATA;     //数据结点类型 
  8 #include <stdio.h>
  9 #include "2-4 ChainList.h"
 10 #include "2-5 ChainList.c"
 11 void ChainListAll(ChainListType *head) //遍历链表 
 12 {
 13     ChainListType *h;
 14     DATA data;
 15     h=head;
 16     printf("链表所有数据如下:\n"); 
 17     while(h) //循环处理链表每个结点 
 18     {
 19         data=h->data;//获取结点数据 
 20         printf("姓名:%s\n",data.key);
 21         printf("地址:%s\n",data.addr);
 22         printf("电话:%s\n",data.telephone);
 23         printf("手机:%s\n",data.mobile);
 24         h=h->next;//处理下一结点 
 25     }
 26     return;
 27 }
 28 ChainListType *input(ChainListType *head) //向通讯录中输入的信息
 29 {
 30     DATA data;
 31     printf("请输入联系人信息\n");
 32     printf("姓名:");
 33     scanf("%s",data.key);
 34     printf("地址:");
 35     scanf("%s",data.addr);
 36     printf("电话:");
 37     scanf("%s",data.telephone);
 38     printf("手机:");
 39     scanf("%s",data.mobile);
 40     return ChainListAddFirst(head,data); //调用添加函数 
 41 }
 42 void find(ChainListType *head)
 43 {
 44     ChainListType *h;
 45     DATA data;
 46     char name[15];
 47     printf("请输入查找姓名:");
 48     scanf("%s",name);
 49     h=ChainListFind(head,name);
 50     if(h)//查找结点指针有效
 51     { 
 52         data=h->data;//获取结点数据 
 53         printf("姓名:%s\n",data.key);
 54         printf("地址:%s\n",data.addr);
 55         printf("电话:%s\n",data.telephone);
 56         printf("手机:%s\n",data.mobile);    
 57     }
 58 }
 59 void delete(ChainListType *head)
 60 {
 61     ChainListType *h=head;
 62     char name[15];
 63     printf("请输入要删除的姓名:");
 64     scanf("%s",name);
 65     ChainListDelete(head,name); 
 66 }
 67 int main()
 68 {
 69     ChainListType *node, *head=NULL;
 70     int select;//选择菜单的序号 
 71     do{
 72         printf("\n_____________________\n");
 73         printf("1.添加联系人\n");
 74         printf("2.查找联系人\n");
 75         printf("3.删除联系人\n"); 
 76         printf("4.显示所有联系人\n"); 
 77         printf("0.退出\n");
 78         printf("_____________________\n");
 79         select=getch();
 80         switch(select) 
 81         {
 82             case 1:
 83                 printf("\n添加联系人\n"); 
 84                 head=input(head);
 85                 break;
 86             case 2:
 87                  printf("\n查找联系人\n"); 
 88                  find(head);
 89                  break;
 90            case 3:
 91                 printf("\n删除联系人\n"); 
 92                 delete(head);
 93                 break;
 94            case 4:
 95                 printf("\n显示联系人\n"); 
 96                 ChainListAll(head);
 97                 break;
 98            case 0:
 99                 return 0;
100         }
101     }while(select != 0);
102 }

 

2.1顺序表(链表)

标签:

原文地址:http://www.cnblogs.com/wozixiaoyao/p/5683113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!