链表基本操作复习
2016-03-30 12:08
295 查看
链表在数据结构里是种非常重要的,在这里我复习了下链表的基本操作
List init_list();//初始化链表
List list_insert(List list,int key);//向链表插入节点,尾插法
List list_insertBypos(List list,int pos,int key);//向链表指定位置插入结点
void print_list(List list);//打印链表
int cal_list_length(List list);//计算链表长度
node *find_node(List list,int pos);//寻找指定位置结点
List dele_node(List list,int pos);//删除指定位置结点
List sort_list(List list);//对链表排序
#include <stdio.h> #include <stdlib.h> #include <malloc.h> //定义结点 typedef struct NODE { int key; struct NODE * next; } node,*List; List init_list();//初始化链表 List list_insert(List list,int key);//向链表插入节点,尾插法 List list_insertBypos(List list,int pos,int key);//向链表指定位置插入结点 void print_list(List list);//打印链表 int cal_list_length(List list);//计算链表长度 node *find_node(List list,int pos);//寻找指定位置结点 List dele_node(List list,int pos);//删除指定位置结点 List sort_list(List list);//对链表排序 List init_list() { List list=(List)malloc(sizeof(node)); list->next=NULL; return list; } List list_insert(List list,int key) { node *temp; temp=list; node *tail=(node *)malloc(sizeof(node)); while(temp->next) { temp=temp->next; } tail=temp; node *p=(node *)malloc(sizeof(node)); p->key=key; p->next=NULL; tail->next=p; tail=p; return list; } int cal_list_length(List list) { node *p=list->next; int length=0; while(p) { p=p->next; length++; } return length; } void print_list(List list) { node *p=list->next; while(p) { printf("%d ",p->key); p=p->next; } } List list_insertBypos(List list,int pos,int key) { int i=0,list_length=0; node * p,* temp;//p指向要插入位置之前,temp指向要插入位置之后的链表 p=list->next; list_length=cal_list_length(list); if(pos<0||pos>list_length) { printf("插入位置不合法"); return list; } while(i<pos-1) { p=p->next;//将p指向要插入位置之前 i++; } node *q=(node *)malloc(sizeof(node)); q->key=key; temp=p->next; p->next=q; q->next=temp; return list; } node *find_node(List list,int pos) { int count=0; node *temp; temp=list->next; while(count<pos) { temp=temp->next; count++; } return temp; } List dele_node(List list,int pos) { List l=list; node *pre=find_node(l,pos-1);//找到待删除结点的前驱 node* temp; temp=pre->next; pre->next=pre->next->next; free(temp); return list; } List sort_list(List list)//采用最简答的排序,即将当前结点逐个和后面的结点比较大小 { int i=0,j=0; node* link=list->next; node* temp,* pointer; int length=cal_list_length(list); for( i=0;i<length;i++,link=link->next) { for( j=i+1,pointer=link->next;j<length;j++,pointer=pointer->next) { if(link->key>pointer->key) { temp->key=link->key; link->key=pointer->key; pointer->key=temp->key; } } } return list; } int main() { List list=init_list(); list=list_insert(list,1); list=list_insert(list,12); list=list_insert(list,6); list=list_insert(list,8); list=list_insert(list,4); list=list_insertBypos(list,2,10); //node *f_node=find_node(list,2); //printf("找到2号位置的结点值为:%d\n",f_node->key); int length=cal_list_length(list); printf("链表长度为:%d\n",length); print_list(list); printf("\n"); printf("删除2号位置结点\n"); list=dele_node(list,2); length=cal_list_length(list); printf("链表长度为:%d\n",length); print_list(list); printf("\n"); printf("排序后链表为"); list=sort_list(list); print_list(list); }
I have no secret of success but hard work
相关文章推荐
- squid源码分析3—purge流程分析
- iOS开发--面试
- oracle 一些用到过的表空间操作命令
- 针对异常java.lang.IllegalStateException onMeasure() did not set the measured dimension解决方法
- 异步通信与事件分发框架
- 测试MySQL性能的几款工具
- mAppWidget - 1. 初试mAppWidget:下载、运行、测试
- 疯子求助-梧桐雨解答的jsp proxy
- python学习4:获取豆瓣上映电影数据
- JVM调优经验分享
- JVM调优经验分享
- OpenGL立方体在世界坐标系中_缩放_旋转_平移_顶点片源着色器
- 课程练习一Problem F
- 并发编程经历:同步加锁之业务锁
- squid源码分析2—refresh_pattern机制分析
- 第一次面试实习生经历
- 隐藏导航栏最底下的线条
- 什么是环境变量?道理在这四个故事中
- 2016.3.29 360c++ 研发编程第一题
- 为MySQL选择合适的备份方式