链表的基本操作:创建、插入、删除操作对应c/c++代码
链表的创建
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{int data;//数据域node *next;//指针域
};
node* create(int array[])
{node *head,*p,*pre;head=new node;//头结点head->next=NULL;pre=head;for(int i=0;i<5;i++){p=new node;//新结点p->data=array[i];p->next=NULL;pre->next=p;pre=p;}return head;
}
int main()
{int array[5]={1,2,3,4,5};node *l=create(array);l=l->next;//第一个结点开始才有数据域while(l!=NULL){printf("%d ",l->data);l=l->next;}return 0;
}
输出:1 2 3 4 5
插入元素
如在第三个位置插入9(从1开始数,因为【0,pos-1))
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{int data;//数据域node *next;//指针域
};
node* create(int array[])
{node *head,*p,*pre;head=new node;//头结点head->next=NULL;pre=head;for(int i=0;i<5;i++){p=new node;//新结点p->data=array[i];p->next=NULL;pre->next=p;pre=p;}return head;
}
void insert(node *head,int pos,int x)
{node *p=head;for(int i=0;i<pos-1;i++)p=p->next;//找到插入位置的前一个结点node *q=new node;//新建结点q->data=x;q->next=p->next;p->next=q;
}
int main()
{int array[5]={1,2,3,4,5};node *l=create(array);insert(l,3,9);l=l->next;//第一个结点开始才有数据域while(l!=NULL){printf("%d ",l->data);l=l->next;}return 0;
}
输出:1 2 9 3 4 5
删除元素
删除链表上所有值为x的结点
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{int data;//数据域node *next;//指针域
};
node* create(int array[])
{node *head,*p,*pre;head=new node;//头结点head->next=NULL;pre=head;for(int i=0;i<5;i++){p=new node;//新结点p->data=array[i];p->next=NULL;pre->next=p;pre=p;}return head;
}
void del(node *head,int x)
{node *p=head->next;//从第一个结点开始node *pre=head;//pre为p的前驱结点while(p!=NULL){if(p->data==x){pre->next=p->next;delete(p);p=pre->next;}else{pre=p;p=p->next;}}
}
int main()
{int array[5]={1,2,3,4,5};node *l=create(array);del(l,3);l=l->next;//第一个结点开始才有数据域while(l!=NULL){printf("%d ",l->data);l=l->next;}return 0;
}
输出:1 2 4 5
链表的基本操作:创建、插入、删除操作对应c/c++代码相关推荐
- 顺序表和单链表的插入删除操作时间复杂度的区别
顺序表和单链表的插入删除操作时间复杂度的区别 最近在学习数据结构,看到如果需要用到大量的插入和删除操作,单链表的效率会高于顺序表.看到这里时内有有个疑惑,这两种数据结构的插入和删除操作的时间复杂度不都 ...
- c语言用链表对学生成绩排序,学生成绩排序和平均分计算利用c语言链表的创建插入删除.doc...
#define NULL 0 #define LEN sizeof(struct student) struct student { long num; float score; struct stu ...
- (图解)循环队列的三种判断队空、队满操作(附带源码和插入删除操作等一些基本操作)
目录 一.普通的顺序存储队列 二.循环队列 (1)少用一个元素空间 i.初始化队列操作: iii.入队操作: iv.出队操作: (2)设置flag标志 i.初始化队列操作: ii.判断队空操作: ii ...
- 浅析B树、B+树插入删除操作(附代码实现)
首先自平衡树是为了解决二叉搜索树在有序数据中退化为链表的问题(即查找时间退化为 O(n) 级别). 自平衡树中,B树.B+树可以说是最简单的,没有旋转.变色等操作.我们可以拿多路平衡查找树和同样是自平 ...
- mysql 插入删除操作_MySQL——增删改操作
插入语句 一次插入操作只插入一行数据 insert into [tablename](listname1,listname2,......) values (value1,value2,......) ...
- 数据结构-----AVL树的插入删除操作
对于AVL的插入和删除,主要利用的就是上篇文章所述的四种旋转操作,根据插入后不同的结构选用不同的方式复原平衡. 再次声明一下,http://www.cnblogs.com/QG-whz/p/51672 ...
- 链表(创建,插入,删除和打印输出
http://www.bianceng.cn/Programming/C/200705/327.htm (以下不全,去此网址看) 数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了 ...
- 二叉搜索树(创建,插入,删除):基础篇,适合新手观看。
1.1 二叉搜索树的插入 二叉搜索树的概念相信大家都很清楚,无非就是左小右大 创建二叉搜索树,其实就是多次调用二叉搜索树的插入方法,所以首先我们来讲讲如何插入节点到二叉搜索树里,假设一颗二叉搜索树如下 ...
- 链表的基本操作——反转与删除
引言 链表相关的问题几乎都是coding问题,以下是两个简单的链表问题. 一.单链表或双链表如何反转 1.1 单链表的反转操作 给定一个 Node 结构: public static class No ...
- 链栈常规插入删除操作
#include <stdio.h> #include <stdlib.h> typedef int DataType; struct Node {DataType data; ...
最新文章
- Django博客系统(写博客页面展示)
- [从0到1编写服务器]准备知识
- 设计模式 — 结构型模式 — 享元模式
- 这个男人让你的爬虫开发效率提升8倍
- java生成验证码工具类_Java生成图形验证码工具类
- 重学java基础第七课:什么是计算机
- PHP常见缓存技术分析,让重复的调用缓存以加快速度
- 2.15_graph_图
- 项目中的设计模式【工厂方法模式】
- BLOB存储图片文件二进制数据是非对错
- 令牌环访问控制的原理_通过Keycloak进行访问控制的级别,第1部分:令牌认证
- 阿里云移动推送iOS
- AngularJS【初体验】-02
- 用delete带where条件删除特定行部分列(属性)的数据,可能活在梦里
- Fine Dining G
- 为什么?为什么?Java处理排序后的数组比没有排序的快?想过没有?
- 计算方法(四):插值与拟合
- 台式计算机机箱的作用,如何选购台式电脑机箱?小白装机选购电脑机箱知识指南...
- 腾讯安全联合发布《2022游戏安全白皮书》:外挂对抗仍然激烈
- 属性动画-Property Animation之ViewPropertyAnimator 你应该知道的一切
热门文章
- rhel5编译安装2.6.29.2内核
- 点击场景中的物件无法定位到Hierarchy
- java call.invoke_java invoke 以及 webservice详解,求助
- 属性子集选择的基本启发方法_2017.06.29数据挖掘基础概念第二.三章
- java关闭流方法,Java关闭流方法总结
- 能够编辑excel的python 软件有哪些_生产管理系统有哪些
- Android项目实战之高仿网易云音乐创建项目和配置
- Java基础系列—字符串
- Uber花了21亿元入驻上海自贸区 不叫优步叫雾博
- SQL Server 2012内存