博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表(list)--c实现
阅读量:7013 次
发布时间:2019-06-28

本文共 3887 字,大约阅读时间需要 12 分钟。

    做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人。o(^▽^)o

    to better myself.

#include 
#include
typedef struct node{ int data; struct node* next;}Node;typedef Node* List;void pInitList(List*);List initList();void deleteList(List);int isNull(List);void insertNode(Node*, int);void deleteNode(List, Node*);Node* findLast(List);Node* findValue(List, int);void printList(List);void printList(List list){ if(list == NULL) { printf("empty list. printf nothing.\n"); return; } Node* curNode = list->next; while(curNode != NULL ) { if(curNode->next != NULL) { printf("%d ->", curNode->data); }else{ printf("%d \n", curNode->data); } curNode = curNode->next; }}Node* findValue(List list, int tarVal){ if(list == NULL) { printf("empty list.\n"); return NULL; } Node* curNode = list->next; while((curNode->data != tarVal ) &&(curNode != NULL)) { curNode = curNode->next; } if(curNode == NULL) { printf("not find the value %d\n", tarVal); free(curNode); return NULL; } printf(" find the value %d\n", tarVal); return curNode;}Node* findLast(List list){ if(list == NULL) { printf("empty list.\n"); return NULL; } Node* curNode = list->next; while(curNode->next != NULL) { curNode = curNode->next; } printf("find the last node value is %d.\n", curNode->data); return curNode;}void deleteNode(List list, Node* delNode){ Node* curNode = (Node*)malloc(sizeof(Node)); Node* freeNode = NULL; if(curNode == NULL) { printf("malloc error at line %d.", __LINE__); return; } curNode = list->next; if(curNode->data == delNode->data) { list->next = NULL; free(curNode); return ; } while((curNode != NULL) && (curNode->next->data != delNode->data) && (curNode->next != NULL)) { curNode=curNode->next; } if(curNode->next == NULL || (curNode ==NULL)) { printf("can not find the given node.\n"); return ; }else{ freeNode = curNode->next; curNode->next = freeNode->next; free(freeNode); }}void insertNode(Node* curNode, int newValue){ Node *newNode = (Node*) malloc(sizeof(Node)); if(newNode == NULL) { printf("malloc error at line %d.", __LINE__); return; } newNode->data = newValue; newNode->next = curNode->next; curNode->next = newNode;}/*return 0 means is not NULL*/int isNull(List list ){ //printf("next value %d\n", list->next->data); return (list->next == NULL);}#if 1void pInitList(List* list){ //*list = (Node*) malloc(sizeof(Node)); (*list)->next = NULL; return;}#endifList initList(){ List list; list = (Node*) malloc(sizeof(Node)); list->next = NULL; return list;}void deleteList(List list){ Node* freeNode = NULL; if(list == NULL) return; while(list->next != NULL) { //get the free node freeNode = list->next; list->next = freeNode->next; free(freeNode); } return ;}int main(){ printf("hello.\n");#if 0 List newList = NULL; newList = initList();#else List *list = NULL; //这里我第一次就没分配1级指针的内存,导致出错,感谢帮助我指出的人。@2016-07-26 00:54:22 list = (List*)malloc(sizeof(List)); *list = (Node*)malloc(sizeof(Node)); pInitList(list); insertNode((*list), 1); insertNode((*list)->next, 2); insertNode((*list)->next->next, 3); findValue(*list, 2); findLast(*list); printList(*list); Node *delNode = (Node*)malloc(sizeof(Node)); delNode->data = 2; deleteNode(*list, delNode); printList(*list); if(isNull(*list)){ printf("list is empty.\n"); }else{ printf("list is not empty.\n"); }#endif return 0;}

 

转载于:https://www.cnblogs.com/ashen/p/5705954.html

你可能感兴趣的文章
【Windows Server 2019】AD批量添加用戶
查看>>
LoadRunner中winsocket协议脚本回放时的mismatch问题处理方法
查看>>
Eclipse常用的优秀插件在线更新地址,包括MyEclipse10.0
查看>>
21个非常有用的.htaccess 提示和技巧
查看>>
6220: Permanent data partition free space insufficient to allocate 33296 bytes of memory
查看>>
Hp Web Jetadmin 8.1的常规使用
查看>>
***笔记(二)
查看>>
awk详解
查看>>
hadoop之CombineFileInputFormat篇
查看>>
希望在软件开发生涯初期就知道的 4 件事
查看>>
java实现交换排序
查看>>
【51CTO学院三周年】我收获许多。
查看>>
(二)AJAX基本介绍和简单实例02
查看>>
Catalyst 12.8 ‘do_mmap’ 补丁
查看>>
MySQL 修改用户密码及重置root密码
查看>>
解决网卡硬件损坏造成的局域网无法连通
查看>>
数据库主键生成方式总结
查看>>
JDK8新特性总结
查看>>
安装zabbix时遇到的问题(一)
查看>>
wiki 的搭建和使用
查看>>