C语言实现常用数据结构——链表
#include<stdio.h>
#include<stdlib.h> typedef struct Node {
int data;
struct Node *next;
} node; /*初始化链表:
1.首先给头指针分配空间,将头指针赋给temp
2.其次给每一个元素分配空间
3.将内容赋给当前节点的data,NULL赋给当前节点的next指针
4.把当前节点赋给(头指针)上一个节点的next指针
5.上一节点指针后移,准备初始化下个元素
6.最后返回当前链表的头指针*/
node *initnode() {
int i;
node *p = (node*)malloc(sizeof(node));
node *temp = p;
for (i = ; i < ; i++) {
node *a = (node*)malloc(sizeof(node));
a->data = i;
a->next = NULL;
temp->next = a;
temp = temp->next;
}
return p;
} /*指定位置插入元素:
1.将头指针赋给temp
2.temp一直遍历直到指定位置
3.给需要插入的元素分配空间,并将内容赋给分配好空间的元素
4.将插入位置的后一元素赋给要插入元素的next指针
5.将插入元素赋给temp的next指针
6.最后返回当前链表的头指针*/
node *insertElem(node *p, int elem, int pos) {
int i;
node *temp = p;
for ( i = ; i < pos; i++) {
temp = temp->next;
}
node *c = (node*)malloc(sizeof(node));
c->data = elem;
c->next = temp->next;
temp->next = c;
return p;
} /*删除指定位置的元素:
1.将头指针赋给temp
2.temp一直遍历直到指定位置
3.声明一个变量代表待删除节点
4.将待删除节点的下一节点赋给待删除节点的上一节点的next指针
5.释放待删除节点空间
6.最后返回当前链表的头指针*/
node *delElem(node *p, int pos) {
int i;
node *temp = p;
for ( i = ; i < pos; i++) {
temp = temp->next;
}
node *c = temp->next;
temp->next = c->next;
free(c);
return p;
} /*查询指定元素的位置:
1.将头指针赋给temp
2.temp一直遍历直到temp节点的值等于要查询的元素
3.返回当前节点的index
4.如果链表遍历结束也未找到指定节点,返回-1
*/
int selectElem(node *p, int elem) {
node *temp = p;
int i = ;
while (temp->next) {
temp = temp->next;
if (temp->data == elem) {
return i;
}
i++;
}
return -;
} /*更新链表指定节点的值*/
node *amendElem(node *p, int pos, int newElem) {
int i;
node *temp = p;
for ( i = ; i < pos; i++) {
temp = temp->next;
}
node *amend = temp->next;
amend->data = newElem;
return p;
} /*遍历链表*/
void display(node *p) {
node *temp = p;
while (temp->next) {
temp = temp->next;
printf("%d ", temp->data);
}
printf("\n");
} //以下内容来自《数据结构与算法-C语言描述》
int IsEmpty(node *p) {
return p->next==NULL;
} int IsLast(node *pos,node *p) {
return pos->next==NULL;
} //删除指定位置的元素
node *Delete(node *p,int elem) {
node *temp=p;
node *tmp;
while(temp->next) {
if(temp->next->data==elem) {
tmp=temp->next;
temp->next=tmp->next;
free(tmp);
}
temp=temp->next;
}
return p;
} //在指定位置插入元素
node *Insert(node *p,int pos,int elem) {
node *temp=p;
int i;
for(i=; i<pos; i++) {
temp=temp->next;
}
node *cell =(node*)malloc(sizeof(node));
cell->data=elem;
cell->next=temp->next;
temp->next=cell;
return p;
}
//删除list
void DeleteList(node *p) {
node *temp=p;
node *tmp;
p->next=NULL;
while(temp->next) {
tmp=temp->next;
free(tmp);
temp=temp->next;
}
} int main() {
node *p = initnode();
display(p);
printf("在第4的位置插入元素5:\n");
p = Insert(p, , );
display(p);
printf("删除第3个元素:\n");
p = delElem(p, );
display(p);
printf("查找元素2的位置为:\n");
int address = selectElem(p, );
if (address == -) {
printf("没有该元素\n");
} else {
printf("元素2的位子为:%d\n", address);
}
printf("更改第3的位置的数为7:\n");
p = amendElem(p, , );
display(p);
printf("delete7\n");
p=Delete(p,);
display(p);
printf("删除\n");
DeleteList(p);
display(p);
}
C语言实现常用数据结构——链表的更多相关文章
- C语言实现常用数据结构——图
#include<stdio.h> #include<stdlib.h> #define SIZE 20 #define LENGTH(a) (sizeof(a)/sizeof ...
- C语言实现常用数据结构——栈
#include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...
- 吴裕雄--天生自然数据结构与算法:java代码实现常用数据结构——链表Linked List
class Node{ // 定义节点类 private String data ; // 保存节点内容 private Node next ; // 表示保存下一个节点 public Node(St ...
- C语言实现常用数据结构——堆
#include<stdio.h> #include<stdlib.h> #define CAPACITY 20 /*堆有两个性质: * 1.结构性:堆必须是一颗完全二叉树 * ...
- C语言实现常用数据结构——二叉树
#include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...
- C语言实现常用数据结构——队列
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 10 /* 用一个动态数组来实现队列 */ typedef stru ...
- 1. C语言中的数据结构.md
C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...
- Java 集合框架(常用数据结构)
早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...
- C++常用数据结构的实现
常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...
随机推荐
- MongoDB小结
教程 MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的 ...
- wxWidgets谁刚开始学习指南(5)——使用wxSmith可视化设计
wxWidgets谁刚开始学习的整个文件夹指南 PDF版及附件下载 1 前言2 下载.安装wxWidgets3 wxWidgets应用程序初体验4 wxWidgets学习资料及利用方法指导5 用w ...
- spring boot——结合docker
spring boot——结合docker 前言 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 liunx机器上,也可以实现虚 ...
- Python属性和方法
关键字:Python 属性 方法原文:http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and ...
- Netty In Action中文版 - 第六章:ChannelHandler
本章介绍 ChannelPipeline ChannelHandlerContext ChannelHandler Inbound vs outbound(入站和出站) 接受连接或创建他们仅仅是你的应 ...
- 回调函数实现类似QT中信号机制
1. 定义回调接口类: class UIcallBack { public: virtual void onAppActivated() = 0; virtual void onShowMore() ...
- 零元学Expression Design 4 - Chapter 6 教你如何在5分钟内做出文字立体感效果
原文:零元学Expression Design 4 - Chapter 6 教你如何在5分钟内做出文字立体感效果 又来一篇五分钟做设计啦~ 本篇将教大家如何运用Design内建工具Blend Path ...
- 【Java】【Flume】Flume-NG阅读源代码AvroSink
org.apache.flume.sink.AvroSink是用来通过网络来数据传输的.能够将event发送到RPCserver(比方AvroSource),使用AvroSink和AvroSource ...
- Angular路由守卫 canActivate
作用 canActivate 控制是否允许进入路由. canActivateChild 等同 canActivate,只不过针对是所有子路由. 关键代码 创建路由守卫 import { Injecta ...
- WPF ListView 居中显示
原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...