• 链表的创建

#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++代码相关推荐

  1. 顺序表和单链表的插入删除操作时间复杂度的区别

    顺序表和单链表的插入删除操作时间复杂度的区别 最近在学习数据结构,看到如果需要用到大量的插入和删除操作,单链表的效率会高于顺序表.看到这里时内有有个疑惑,这两种数据结构的插入和删除操作的时间复杂度不都 ...

  2. c语言用链表对学生成绩排序,学生成绩排序和平均分计算利用c语言链表的创建插入删除.doc...

    #define NULL 0 #define LEN sizeof(struct student) struct student { long num; float score; struct stu ...

  3. (图解)循环队列的三种判断队空、队满操作(附带源码和插入删除操作等一些基本操作)

    目录 一.普通的顺序存储队列 二.循环队列 (1)少用一个元素空间 i.初始化队列操作: iii.入队操作: iv.出队操作: (2)设置flag标志 i.初始化队列操作: ii.判断队空操作: ii ...

  4. 浅析B树、B+树插入删除操作(附代码实现)

    首先自平衡树是为了解决二叉搜索树在有序数据中退化为链表的问题(即查找时间退化为 O(n) 级别). 自平衡树中,B树.B+树可以说是最简单的,没有旋转.变色等操作.我们可以拿多路平衡查找树和同样是自平 ...

  5. mysql 插入删除操作_MySQL——增删改操作

    插入语句 一次插入操作只插入一行数据 insert into [tablename](listname1,listname2,......) values (value1,value2,......) ...

  6. 数据结构-----AVL树的插入删除操作

    对于AVL的插入和删除,主要利用的就是上篇文章所述的四种旋转操作,根据插入后不同的结构选用不同的方式复原平衡. 再次声明一下,http://www.cnblogs.com/QG-whz/p/51672 ...

  7. 链表(创建,插入,删除和打印输出

    http://www.bianceng.cn/Programming/C/200705/327.htm  (以下不全,去此网址看) 数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了 ...

  8. 二叉搜索树(创建,插入,删除):基础篇,适合新手观看。

    1.1 二叉搜索树的插入 二叉搜索树的概念相信大家都很清楚,无非就是左小右大 创建二叉搜索树,其实就是多次调用二叉搜索树的插入方法,所以首先我们来讲讲如何插入节点到二叉搜索树里,假设一颗二叉搜索树如下 ...

  9. 链表的基本操作——反转与删除

    引言 链表相关的问题几乎都是coding问题,以下是两个简单的链表问题. 一.单链表或双链表如何反转 1.1 单链表的反转操作 给定一个 Node 结构: public static class No ...

  10. 链栈常规插入删除操作

    #include <stdio.h> #include <stdlib.h> typedef int DataType; struct Node {DataType data; ...

最新文章

  1. net-tools 要被 iproute2 取代了
  2. java操作dom节点的添加_java操作DOM节点的添加,删除,修改
  3. 扩展KMP --- HDU 3613 Best Reward
  4. python zipfile_Python中的zipfile模块使用详解
  5. python sys模块
  6. java实体设置扩展属性setextattributes_transactionAttributes各属性意义及配置
  7. 【ES6(2015)】Number
  8. HLSL编译工具—FXC
  9. 华为鸿蒙商标被驳回复审:易造成混淆
  10. python获取列表序号_确定列表中的序列号(Python)
  11. linux下,保存退出vim编辑器(转)
  12. kerberos验证_SQL Server中的服务主体名称和Kerberos身份验证概述
  13. 个人收藏的移动端网页布局rem解决方案
  14. 通过LINQ表达式树动态构建查询条件
  15. 关于web页面中mata各种标签的解释
  16. 【基于遥感解译与GIS技术】土地利用图、植被类型图、植被覆盖度图、土壤侵蚀制图
  17. win10 安装redis 解压版
  18. 拼音加加符号编码列表
  19. while用法小简介(涉及EOF用法)
  20. html 字体图标转换工具,HTML5 webfont字体图标的使用

热门文章

  1. LFM算法详解和实战
  2. java8 stream中 forEach和 forEachOrdered 当parallel时候执行过程安全问题深入理解
  3. django mysql 登陆界面_django 简单实现登录验证给你 Django用户登录验证跳转问题
  4. 【转】C#中Json和类的相互转化
  5. SQLServer如何在批量插入后,获取批量插入的自增列的值
  6. Python连接SQLite
  7. Python与模块--01sys
  8. Linux网卡名改eth0方法
  9. jQuery源码分析系列(31) : Ajax deferred实现
  10. FindBugs插件的安装与使用