自己编的C语言单链表的实现
2013-04-24 20:32
330 查看
看了下C primer plus的最后一章以及c和指针中讲解链表的部分,就动手写了个链表的功能实现程序,用纯C编写,在vc环境下,因此文件后缀为cpp。
运行结果:
关注我的新浪博客:http://blog.sina.com.cn/u/1921993171
关注我的新浪微博:http://weibo.com/u/1921993171?wvr=5&wvr=5&lf=reg
头文件sll_node.h #include<stdio.h> #include<stdlib.h> struct NODE { int value; struct NODE *next; }; typedef struct NODE Node; //这里的root是在link_main文件中的定义的,同时应用于input.cpp文件, //该文件中没有定义指针root, //其结果是在调用过input函数之后,root值改变,不再为NULL; extern Node *root; bool sell_insert(Node **,int ); //插入节点函数 void show(Node *); //显示链表数据函数 void input_data(Node **); //输入链表数据函数 void free_list(Node **); //释放链表内存函数 unsigned int count(Node *); //链表节点计数函数 bool check(Node *,int); //检查输入的数据是否在聊表中已经存在的函数 //链表排序函数——冒泡法,不改变节点位置,只改变节点中数据的存储位置 void sort(Node *); void delete_data(Node **,int); //删除节点函数 子函数文件node.c #include "sll_node.h" //遍历链表——检查输入的数据是否与链表中的数据重复 bool check(Node *root,int value) { Node *current; current = root; while(current != NULL) { if(current->value != value) current = current->next; else break; } if(current == NULL) return true; //输入的数据没有与链表中的数据重复,返回true else return false; } //删除节点函数 void delete_data(Node **phead,int delete_value) { Node *current; Node *previous; Node *next; Node *pt; current = *phead; previous = NULL; while(current->value != delete_value) { previous = current; current = current->next; next = current->next; } if(previous == NULL) *phead = current->next; else previous->next = next; pt = current; free(pt); } //输入函数 void input_data(Node **phead) { int value; Node *current,*prev; root = *phead; puts("Enter the first value(q to end):"); while(scanf("%d",&value)==1) { while(getchar() != '\n') continue; current = (Node *)malloc(sizeof(Node)); if(root == NULL) root = current; else prev->next = current; current->next = NULL; current->value = value; puts("Enter the next value(q to end):"); prev = current; } } //插入节点函数 bool sell_insert(Node **phead,int new_value) { Node *current; Node *previous; Node *newn; current = *phead; previous = NULL; while(current != NULL && current->value < new_value) { previous = current; current = current->next; } newn = (Node *)malloc(sizeof(Node)); if(newn == NULL) return false; newn->value = new_value; newn->next = current; if(previous==NULL) *phead = newn; else previous->next = newn; return true; } //节点计数函数 unsigned int count(Node *root) { unsigned int count = 0; while(root != NULL) { ++count; root = root->next; } return count; } //显示函数 void show(Node *root) { Node *current; if(root == NULL) printf("No data entered."); current = root; while(current != NULL) { printf(" %d ",current->value); current = current->next; } } //冒泡法排序链表函数——不改变节点位置,只改变节点中数据的存储位置void sort(Node *root) { Node *current; Node *s; for(current = root;current->next != NULL;current = current->next) { for(s = root;s->next != NULL;s = s->next) { if(s->value > s->next->value) { int temp; temp = s->value; s->value = s->next->value; s->next->value = temp; } } } } //释放节点内存函数 void free_list(Node **phead) { Node *psave; while(*phead != NULL) { psave = (*phead)->next; free(*phead); *phead = psave; } } 主函数实现文件link_main.c #include "sll_node.h" Node *root = NULL; int main() { //输入数据,分配内存,并输入原始序列和排序后的序列 input_data(&root); if(root== NULL) printf("No data entered."); else { printf("Here is the list:\n"); show(root); printf("\nThe sorted list is:\n"); sort(root); show(root); printf("\nTotal itms of the list is:\n %d",count(root)); } //输入需要添加的数据 while(getchar() != '\n') continue; //这两句不可少,防止scanf因最后一次读取到的是字母而永远不会越过它 int new_value; printf("\nNow enter the new value:\n"); scanf("%d",&new_value); while(getchar() != '\n') continue; //检查待添加的数据是否在链表中存在,若存在,提示重新输入,直到不存在为止 while(!check(root,new_value) ) { printf("Existed in the list ,please input another data:"); scanf("%d",&new_value); while(getchar() != '\n') continue; } //将待添加的数据加入新建的节点,并分配内存,插入链表,显示插入后的序列 if(sell_insert(&root,new_value)) { printf("\nNow the list is:\n"); show(root); printf("\nNow total itms of the list is:\n %d",count(root)); } else printf("insert error\n"); printf("\n"); //输入待删除的数据 int delete_value; printf("\nNow enter the deleted value:\n"); scanf("%d",&delete_value); while(getchar() != '\n') continue; //如果待删除的数据不在链表中,提示重新输入,知道存在为止 while(check(root,delete_value) ) { printf("Not exist in the list ,please input a data in the list:"); scanf("%d",&delete_value); while(getchar() != '\n') continue; } //将待删除的数据所在的节点所占用的内存释放掉,即删除掉,重新链接链表,并显示删除数据后的序列 delete_data(&root,delete_value); printf("\nNow the list is:\n"); show(root); printf("\nNow total itms of the list is:\n %d",count(root)); printf("\n"); //释放链表所占用的内存 free_list(&root); return 0; }
运行结果:
关注我的新浪博客:http://blog.sina.com.cn/u/1921993171
关注我的新浪微博:http://weibo.com/u/1921993171?wvr=5&wvr=5&lf=reg
相关文章推荐
- C语言实现单链表面试题(基础篇)
- C语言实现单链表的节点插入(带头结点)
- C语言实现单链表的简单操作【完整版】
- 赛码网,将功赎过问题,用C语言自己实现的一个程序,仅供参考
- 动态单链表的传统存储方式和10种常见操作-C语言实现
- 单链表实现多项式的相乘-c语言
- 【C语言】自己编写程序实现strchr函数。即在给定字符串中找特定的字符并返回该处指针。
- 自己动手写字符串库函数 一(C语言实现)
- c语言实现单链表面试题
- 数据结构与算法分析 C语言描述 单链表的实现
- 自己实现的C语言string.h 头文件的字符串函数与几个内存操作函数
- C语言面向对象编程(五):单链表实现
- C语言实现创建单链表及求长度
- C语言动手实现一个自己的HttP服务
- C语言实现单链表的逆置
- 自己实现集合框架(八):可排序单链表的实现
- C语言——把自己编程实现字符串函数strstr
- 自己按照书上实现的c语言的单链表
- C语言实现单链表面试题---进阶
- C语言字符切割函数的实现——不需要自己指定分配多少个字符串分组