int main(){ ChainListType *head; char key[15]={0}; DATA data; //Initialize the ChainList head = ChainListInit(); ChainListTraverse(head); ///Find out if the is a Node that matches the key printf("\n"); printf("Please input the key of Node you want to find: "); scanf("%s", key); ChainListFind(head, key); //Insert a Node //Use key to find where to insert the Node //Then input the Data of that Node printf("\n"); printf("\nInput key to insert a Node:"); scanf("%s",key); if(ChainListFind(head, key)) { printf("OK! Input the Data of Node (key name age): "); scanf("%s%s%d", data.key, data.name, &data.age); printf("%s\t%s\t%s\t%d\n", key, data.key, data.name, data.age); head = ChainListInsert(head, data, key); ChainListTraverse(head); }else{ printf("Node not found!"); } return 0; }
int main() { ChainListType *node, *head = NULL; DATA data; char key[15], findkey[15]; printf("Input data in chainlist, include keyword, name, age, if keyword is 0, then exit:\n"); do{ fflush(stdin); scanf("%s", data.key); if(strcmp(data.key, "0") == 0) break; scanf("%s%d", data.name, &data.age); head = ChainListAddEnd(head, data); }while(1); printf("The Chainlist has %d points\n", ChainListLength(head)); ChainListAll(head); printf("\nInsert point, input the place keyword:\n"); scanf("%s", findkey); printf("Input insert data(keyword name age):"); scanf("%s%s%d", data.key, data.name, &data.age); ChainListAll(head); printf("\nFind in the chain,input keyword to find"); fflush(stdin); scanf("%s", key); node = ChainListFind(head, key); if(node) { data = node->data; printf("keyword %s data is (%s, %s, %d)\n", key, data.key, data.name, data.age); }else printf("Can't not find point about the keyword %s!\n", key); printf("\nDelete point in the chain,input keyword to delete:"); fflush(stdin); scanf("%s", key); ChainListDelete(head, key); ChainListAll(head); return 0; }
ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data) //插入结点到链表指定位置 { ChainListType *node,*node1; if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) //分配保存结点的内容 { printf("为保存结点数据申请内存失败!\n"); return 0; //分配内存失败 } node->data=data; //保存结点中的数据 node1=ChainListFind(head,findkey); if(node1) //若找到要插入的结点 { node->next=node1->next; //新插入结点指向关键结点的下一结点 node1->next=node; //设置关键结点指向新插入结点 }else{ free(node);//释放内存 printf("未找到插入位置!\n"); } return head;//返回头指针 }