数据结构-单向链表(C语言)
2016-09-03 11:34
603 查看
ChainList.h
ChainList1.c
ChainListTest.c
例题:链表实现简单通讯录
AddressList.c
#include<stdio.h> #include<stdlib.h> typedef struct Node{ DATA data; struct Node *next; }ChainListType; ChainListType *ChainListAddEnd(ChainListType *head,DATA data);//添加节点到链表末尾 ChainListType *ChainListAddFirst(ChainListType *head,DATA data);// 添加节点到链表首部 ChainListType *ChainListFind(ChainListType *head,char *key);//按关键字在链表中查找内容 ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data); //插入节点到链表指定位置 int ChainListDelete(ChainListType *head,char *key);//删除指定关键字的节点 int ChainListLength(ChainListType *head);//获取链表节点数量
ChainList1.c
ChainListType *ChainListAddEnd(ChainListType *head,DATA data)//添加节点到链表末尾 { ChainListType *node,*h; if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) { printf("为保存节点数据申请失败!\n"); return NULL; } node->data=data; node->next=NULL; if(head==NULL)//链表为空时 { head=node; return head; } h=head; while(h->next!=NULL)h=h->next; h->next=node; return head; } ChainListType *ChainListAddFirst(ChainListType *head,DATA data)// 添加节点到链表首部 { ChainListType *node,*h; if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) { printf("为保存节点数据申请失败!\n"); return NULL; } node->data=data; node->next=head; head=node; return head; } ChainListType *ChainListFind(ChainListType *head,char *key)//按关键字在链表中查找内容 { ChainListType *h; h=head; while(h){ if(strcmp(h->data.key,key)==0) return h; h=h->next; } return NULL; } ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data) //插入节点到链表指定位置 { ChainListType *node,*node1; if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) { printf("为保存节点数据申请失败!\n"); return NULL; } node->data=data; node1=ChainListFind(head,findkey); if(node1){ node->next=node1->next; node1->next=node; } else{ free(node); printf("未找到位置!\n"); } return head; } int ChainListDelete(ChainListType *head,char *key)//删除指定关键字的节点 { ChainListType *node,*h; h=head; node=h; // node=h=head; while(h){ if(strcmp(h->data.key,key)==0){ node->next=h->next; free(h); return 1; } else{ node=h; h=h->next;////////////////////// } } return 0;//未找到 } int ChainListLength(ChainListType *head)//获取链表节点数量 { int i=0; ChainListType *h; h=head; while(h){ i++; h=h->next; } return i; }
ChainListTest.c
#include<stdio.h> typedef struct{ char key[15]; char name[20]; int age; }DATA; #include"ChainList.h" #include"ChainList1.c" void ChainListAll(ChainListType *head)//遍历链表 { ChainListType *h; h=head; DATA data; printf("所有链表如下:\n"); while(h){ data=h->data; printf("(%s,%s,%d)\n",data.key,data.name,data.age); h=h->next; } } int main() { ChainListType *node,*head=NULL; DATA data; char key[15],findkey[15]; printf("input data key,name,age(关键字为0时退出):\n"); do{ fflush(stdin); scanf("%s",data.key);//关键字为0时退出 if(strcmp(data.key,"0")==0)break; scanf("%s%d",data.name,&data.age); head=ChainListAddEnd(head,data); }while(1); printf("共有%d个节点\n",ChainListLength(head)); ChainListAll(head); printf("\n插入节点,输入插入位置关键字:\n"); fflush(stdin);///////////////////error1:删除后scanf跳过执行 scanf("%s",findkey); printf("输入插入节点的数据(key, name, age):"); scanf("%s%s%d",data.key,data.name,&data.age); head=ChainListInsert(head,findkey,data); ChainListAll(head); printf("\n在链表中查找关键字,输入关键字:"); fflush(stdin); scanf("%s",&key); node=ChainListFind(head,key); if(node){ data=node->data; printf("关键字对应的节点:(%s,%s,%d)\n",data.key,data.name,data.age); } else{ printf("没找到节点!\n"); } printf("\n在链表中删除节点,输入删除节点的关键字:"); fflush(stdin); scanf("%s",key); ChainListDelete(head,key); ChainListAll(head); printf("\n在链表中删除结点,输入要删除的关键字:"); fflush(stdin);//清空输入缓冲区 scanf("%s",key);//输入删除结点关键字 ChainListDelete(head,key); //调用删除结点函数 ChainListAll(head); //显示所有结点 getch(); return 0; }
例题:链表实现简单通讯录
AddressList.c
#include<stdio.h> typedef struct{ char key[15]; char addr[20]; char telephone[15]; char mobile[12]; }DATA; #include"ChainList.h" #include"ChainList1.c" void ChainListAll(ChainListType *head)//显示联系人 { ChainListType *h; DATA data; h=head; printf("all data:\n"); printf("name address telephonr mobile\n"); while(h){ data=h->data; printf("%s\t %s\t %s\t %s\n",data.key,data.addr,data.telephone,data.mobile); h=h->next; } } ChainListType *input(ChainListType *head)//添加联系人 { DATA data; ChainListType *h=head; printf("input Contacts:\n"); printf("name:"); scanf("%s",data.key); printf("address:"); scanf("%s",data.addr); printf("telephone:"); scanf("%s",data.telephone); printf("mobile:"); scanf("%s",data.mobile); return ChainListAddFirst(head,data); } void find(ChainListType *head)//查找联系人 { ChainListType *h; DATA data; char name[15]; printf("input name:"); scanf("%s",name); h=ChainListFind(head,name); if(h) { data=h->data; printf("%s,%s,%s,%s",data.key,data.addr,data.telephone,data.mobile) ; } } void deletec(ChainListType *head)//删除联系人 { ChainListType *h=head; char name[15]; printf("input delete name:"); // fflush(stdin); scanf("%s",name); ChainListDelete(head,name); } int main() { ChainListType *head=NULL; int select; printf("-----------------------------\n"); printf("1.add contacts\n"); printf("2.find contacts\n"); printf("3.delete contacts\n"); printf("4.show all contacts\n"); printf("0.quit\n"); printf("-----------------------------\n"); do{ select=getch(); switch(select) { case '1': printf("\nadd contacts \n"); head=input(head);break; case '2': printf("\nfind contacts\n"); find(head);break; case '3': printf("\ndelete contacts\n"); deletec(head);break; case '4': printf("\nshow all contacts\n"); ChainListAll(head);break; case '0': return 0; } }while(select!='0'); }
相关文章推荐
- 数据结构——单向链表的基本操作C语言描述(克服对头结点和尾结点的操作)
- 算法与数据结构-单向链表的基本操作C语言实现
- C语言数据结构:单向链表
- 数据结构:单向链表的C语言实现
- (单向链表)数据结构的创建和功能函数(C语言)
- c语言数据结构之单向循环链表约瑟夫问题
- C语言数据结构之单向链表(已经调试可以实现相应的功能了,可是还是有几个问题现在还是不大理解,希望大家能够一起探讨)
- 数据结构模版----单链表SimpleLinkList[不带头结点&&伪OO](C语言实现)
- 数据结构Java实现03----单向链表的插入和删除
- list.c - A linked list by C --- C语言实现的单向链表
- 数据结构-单向链表(一)(JAVA)
- C语言实现链表之单向链表(一)头文件
- C语言实现链表之单向链表(六)删除头结点
- C语言实现链表之单向链表(十五)测试用例
- 数据结构----链表----单向链表排序
- C语言学习历程(十六)数据结构-单双向链表
- c语言单向链表的基本功能的实现
- C语言实现单向链表
- 数据结构学习系列一--链表的建立(c语言实现)
- 重温数据结构之链表二——C语言实现