【数据结构】链表操作示例
2015-11-30 19:51
447 查看
#include<cstdlib> #include<cstdio> #include<cstring> #include<string> #include<iostream> #include<algorithm> using namespace std; typedef struct{ char key[10]; char name[20]; int age; }Data; typedef struct Node{ Data nodeData; struct Node * nextNode; }CLType; CLType * CLAddEnd(CLType * head, Data nodeData){ //追加结点 CLType * node, * htemp; if(!(node=(CLType*)malloc(sizeof(CLType)))){ cout<<"申请内存失败!\n"; return NULL; } else{ node->nodeData=nodeData; node->nextNode=NULL; if(head==NULL){ head=node; return node; } htemp=head; while(htemp->nextNode!=NULL){ htemp=htemp->nextNode; } htemp->nextNode=node; return head; } } CLType * CLAddFirst(CLType * head, Data nodeData){ CLType *node; if(!(node=(CLType*)malloc(sizeof(CLType)))){ cout<<"申请内存失败!\n"; return NULL; } else{ node->nodeData=nodeData; node->nextNode=head; head=node; } } CLType * CLFindNode(CLType * head, char *key){ CLType *htemp; htemp=head; while(htemp){ if(strcmp(htemp->nodeData.key, key)==0){ return htemp; } htemp=htemp->nextNode; } return NULL; } CLType * CLInsertNode(CLType * head, char * findkey, Data nodeData){ CLType * node, * nodetemp; if(!(node=(CLType *)malloc(sizeof(CLType)))){ cout<<"内存申请失败!\n"; return 0; } node->nodeData=nodeData; nodetemp=CLFindNode(head, findkey); if(nodetemp){ node->nextNode=nodetemp->nextNode; nodetemp->nextNode=node; } else{ cout<<"未找到正确的插入位置!\n"; free(node); } return head; } int CLDeleteNode(CLType * head, char * key){ CLType * node, * htemp; htemp=head; node=head; while(htemp){ if(strcmp(htemp->nodeData.key, key )==0){ node->nextNode=htemp->nextNode; free(htemp); return 1; } else{ node= htemp; htemp = htemp -> nextNode; } return 0; } } int CLLength(CLType * head){ CLType * htemp; int Len=0; htemp=head; while(htemp){ Len++; htemp=htemp->nextNode; } return Len; } void CLAllNode(CLType * head){ CLType * htemp; Data nodeData; htemp = head; cout<<"当前链表共有"<<CLLength(head)<<"个结点。链表所以数据如下:\n"; while(htemp){ nodeData=htemp->nodeData; cout<<"结点"<<nodeData.key<<nodeData.name<<nodeData.age<<endl; htemp=htemp->nextNode; } } int main(){ CLType *node, *head=NULL; Data nodeData; char key[10], findkey[10]; cout<<" 链表测试。请输入链表中的数据,格式为:关键字 姓名 年龄\n"; do{ fflush(stdin); cin>>nodeData.key; if(strcmp(nodeData.key, "0")==0){ break; } else{ cin>>nodeData.name>>nodeData.age; head=CLAddEnd(head, nodeData); } }while(1); CLAllNode(head); cout<<"\n 演示插入结点,输入插入结点的关键字: "; cin>>findkey; cout<<"输入插入结点的数据(关键字 姓名 年龄):"; cin>>nodeData.key>>nodeData.name>>nodeData.age; head=CLInsertNode(head, findkey, nodeData); CLAllNode(head); cout<<"\n演示删除结点,输入要删除的关键字:"; fflush(stdin); cin>>key; CLDeleteNode(head, key); CLAllNode(head); cout<<"\n 演示在链表中查找,输入查找关键字:"; fflush(stdin); cin>>key; node=(CLFindNode(head, key)); if(node){ nodeData=node->nodeData; cout<<"关键字"<<key<<"对应的结点为:"<<nodeData.key<<nodeData.name<<nodeData.age; } else{ cout<<"在链表中未找到关键字为"<<key<<"的结点!"<<endl; } }
相关文章推荐
- 【Educational Codeforces Round 2E】【STL-map 启发式合并 or 线段树动态开节点 】Lomsat gelral 一棵树每点一个颜色问每个节点子树的颜色众数之和
- 数据结构与算法-----队列-使用链表(链式结构)实现
- 第十二周--数据结构--非连通图的遍历之三
- 第九周 数据结构实践项目——数组和广义表【项目3.2-- 稀疏矩阵相加】
- 第十二周--数据结构--非连通图的遍历之二
- 【第14周-查找项目1-1——验证折半查找算法】
- 第9周SHH数据结构-【项目1--猴子选大王(数组版)】 .
- 第8周SHH数据结构-【项目5-计数的模式匹配 . 】
- 第十二周--数据结构--非连通图的遍历之一
- leetcode之Remove Duplicates from Sorted Array II
- 数据结构例程——哈希表及其运算的实现
- 第九周 数据结构实践项目——数组和广义表【项目3.1-稀疏矩阵的三元组表示的实现及应用】
- JavaScript数据结构 --- 字典
- 第13周SHH数据结构-【项目5-拓扑排序算法的验证 】
- 第十二周--数据结构--图的遍历
- 数据结构——双向链表(Java实现)
- 数据结构实践——Prim算法的验证
- OpenCv学习笔记(三)——openCv数据结构之间的转换
- 项目3 - 是否二叉排序树?
- 项目2 二叉树排序树中查找的路径