链表
来源:互联网 发布:体质辨识软件app 编辑:程序博客网 时间:2024/06/03 14:42
链表在数据结构中不难但是用的很多也算是数据结构中必须掌握的内容;数据结构学了很久了,现在写一些也当做是回顾吧,都说数据结构重要;
每次都是,除了代码好像就么有什么可写的了,哎;
反正就是跟线性表类似,毕竟都是线性结构;
把原先连续的储存单元换成了分散的节点,每一个节点分为数据域和指针域;
数据域存放数据,指针域存放下一个节点的地址,注意下一个节点在物理存储上不一定挨着的;
我们在遍历一个链表的时候是靠节点的指针域找到下一个节点的,就这样遍历到了所有的节点;
很多时候我们喜欢设一个头结点(非首节点,首尾是相对的),头结点就在首节点前面一个节点;
这个头结点的数据域用来储存整个链表的节点个数,指针域存放首节点的地址,在很多时候很方便做一些操作,更多好处百度一下就会有;
下面这个代码我依然是加了一个头结点:
#include<stdio.h>#include<malloc.h>typedef struct Node{int data;struct Node* next;}*List;void Initialization(List p)//初始化{p->data=0;p->next=NULL;}void Add(List p)//添加{p->data++;while(p->next!=NULL)p=p->next;List pNext=new struct Node;//p=(List)malloc(sizeof(struct Node));scanf("%d",&pNext->data );pNext->next=NULL;p->next=pNext;}void Show(List p)//输出{p=p->next;while(p!=NULL){printf("%d\n",p->data);p=p->next;}}void Insert(List p,int index,int e)//插入{if(index>p->data){p->data++;while(p->next !=NULL)p=p->next;List pNext=new struct Node;pNext->data=e;pNext->next=NULL;p->next=pNext;}else{puts("2\n");p->data++;for(int i=0;i<index-1;++i)p=p->next;List NEW=new struct Node;NEW->data=e;NEW->next=p->next;p->next=NEW;}}void Sort(List p) //排序{List x,y;int len=p->data;for(int i=0;i<len;++i){p=p->next;x=p;y=p->next;for(int j=i+1;j<len;++j){if(x->data>y->data )x=y;y=y->next;}int tmp;if(x->data!=p->data ){tmp=p->data;p->data=x->data;x->data=tmp;}}}int main(){List pos;pos=new struct Node;Initialization(pos);for(int i=0;i<3;++i)Add(pos);Insert(pos,2,90);Show(pos);Sort(pos);Show(pos);return 0;}//比起上篇写的线性表加了一个排序,采用了选择排序,其他功能和线性表差不多,如果对照起来理解的话会跟清晰;
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- Oracle 10g Scheduler 特性
- android面试经验谈【转来的、没看过的看看、没坏处】
- 【hadoop】大规模中文网站聚类kmeans的mapreduce实现(上)
- unity3d联机调试
- poj3468(线段树成段更新)
- 链表
- struts上传下载
- activeMQ的简单案例
- C#的数据类型
- java任务调度
- Python 正则表达式 Howto(2)
- linux-0.11调试教程,fflush()函数实现分析和FILE类型定义
- Good Sentence
- Android开发: The type ClipboardManager is deprecated