单向链表简单实现
2017-10-16 14:14
274 查看
// MyList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; // data typedef struct { char key[10]; char name[20]; int age; }Data; typedef struct Node { Data nodeData; struct Node *nextNode; }CLType; // AddTail CLType *CLAddEnd(CLType *head, Data nodeData) { CLType *node,*htemp; if(!(node = (CLType*)malloc(sizeof(CLType)))) { printf("malloc false!\n"); return NULL; } else { node->nodeData = nodeData; node->nextNode = NULL; if(head == NULL) { head = node; return head; } htemp = head; while(htemp->nextNode != NULL) { htemp=htemp->nextNode; } htemp->nextNode = node; return head; } } // InsertFirst CLType *CLAddFirst(CLType *head, Data nodeData) { CLType *node = NULL; // init if(!(node == (CLType *)malloc(sizeof(CLType)))) { printf("AddFirst malloc false\n"); return NULL; } else { node->nodeData = nodeData; node->nextNode = head; head = node; return head; } } // Find Key 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; } // Insert Node CLType *CLInsertNode(CLType *head, char *findkey, Data nodeData) { CLType *node = NULL, *nodetemp = NULL; if(!(node = (CLType*)malloc(sizeof(CLType)))) { printf("malloc false!\n"); return NULL; } node->nodeData = nodeData; nodetemp = CLFindNode(head, findkey); if(nodetemp) { node->nextNode = nodetemp->nextNode; nodetemp->nextNode = node; } else { printf("%s Not Found\n", findkey); free(node); } return head; } // DelNode 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; } // CL length int CLLength(CLType *head) { CLType *htemp = NULL; int Len = 0; htemp = head; while(htemp) { Len++; htemp = htemp->nextNode; } return Len; } // Show ALL void CLAllNode(CLType *head) { CLType *htemp; Data nodeData; htemp = head; printf("having %d node show All \n", CLLength(head)); while(htemp) { nodeData = htemp->nodeData; printf("Node(%s,%s,%d)\n", nodeData.key, nodeData.name, nodeData.age); htemp = htemp->nextNode; } } int main() { CLType *node, *head = NULL; Data nodeData; char key[10], findkey[10]; cout<<"test CList enter list init data format like: key name age"<<endl; do { fflush(stdin); scanf("%s", nodeData.key); if(strcmp(nodeData.key, "0") == 0) { break; } else { scanf("%s%d", nodeData.name, &nodeData.age); head = CLAddEnd(head, nodeData); } }while(1); CLAllNode(head); printf("inset Data, enter insert find key\n"); fflush(stdin); //scanf("%s", findkey); cin>>findkey; cout<<"findkey"<<findkey<<endl; cout<<"enter insert data format like: key name age"<<endl; scanf("%s%s%d", nodeData.key, nodeData.name, &nodeData.age); //cin>>nodeData.key>>nodeData.name>>nodeData.age; cout<<nodeData.key<<nodeData.name<<nodeData.age<<endl; head = CLInsertNode(head, findkey, nodeData); CLAllNode(head); cout<<"delete enter you want delete key: "<<endl; scanf("%s", key); //cin>>key; CLDeleteNode(head, key); CLAllNode(head); cout<<"find enter you want find key:"<<endl; scanf("%s", key); //cin>>key; node = CLFindNode(head, key); if(node) { nodeData = node->nodeData; cout<<"key"<<nodeData.key<<"name"<<nodeData.name<<"age"<<nodeData.age<<endl; } else { cout<<"CList not found"<<endl; } return 0; }
test:
test CList enter list init data format like: key name age 1 test 1 2 test 2 3 test 3 4 tom 4 0 having 4 node show All Node(1,test,1) Node(2,test,2) Node(3,test,3) Node(4,tom,4) inset Data, enter insert find key 3 findkey3 enter insert data format like: key name age 6 test 6 6test6 having 5 node show All Node(1,test,1) Node(2,test,2) Node(3,test,3) Node(6,test,6) Node(4,tom,4) delete enter you want delete key: 3 having 4 node show All Node(1,test,1) Node(2,test,2) Node(6,test,6) Node(4,tom,4) find enter you want find key: 4 key4nametomage4 Press any key to continue
相关文章推荐
- 简单单向链表实现
- C++,数据结构,单向链表的实现及简单运用,运用模板
- C语言实现一个简单的单向链表list
- C语言实现简单的单向链表
- C语言实现一个简单的单向链表list
- C实现简单单向链表,一次遍历查找倒数第k个节点的值
- (C语言版)链表(一)——实现单向链表创建、插入、删除等简单操作(包含个人理解说明及注释,新手跟着写代码)
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 单向链表的简单实现(C语言版)
- 单向链表的简单实现
- java单向链表基本操作简单实现
- 单向循环链表的简单实现--数据结构学习(二)
- 单向链表的简单实现
- 使用java实现的超简单的单向链表 头插法
- C++中实现一个简单的单向链表
- 转载的标准文档:C语言实现一个简单的单向链表list
- 数据结构与算法学习之路:简单的电话簿实现——单向链表
- c语言单向链表的简单实现,隐藏head节点
- C语言-------实现一个简单的单向链表
- 单向链表的java简单实现