单链表的几类操作介绍(头结点没有数据)

2023-05-16

1.定义一个单链表的结构体

typedef struct _Node
{
  int data;
  struct _Node *next; 
}node;

2.创建一个链表,这里分为头插法和尾插法

node *CreatNode_Head(int n)
{
   int i=0;
   node *head,*p;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=NULL;
   srand(time(0));
   for(int i=0;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=rand()%100+1;
	  printf("p->data:%d\t",p->data);
   	  p->next=head->next;
   	  head->next=p;
   }
   return head;
}
node *CreatNode_Tail(int n)
{
   int i=0;
   node *head,*p,*q;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=NULL;
   q=head;
   srand(time(0));
   for(int i=0;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=rand()%100+1;
   	  p->next=NULL;
	  printf("p->data:%d\t",p->data);
   	  q->next=p;
   	  q=p;
   }
   return head;
}

3.获取链表的长度

由于头结点没有数据,所以循环从头结点->next开始遍历,直到遇到NULL为止。最后返回链表的长度

int length(node *p)
{
	if(p==NULL)
	   return -1;
    int len=0;
    while(p->next!=NULL)
    {
       len++;
       p=p->next;
    }
    return len;
}

4.打印链表中存储的数据

这个其实和获取链表的长度一样的道理,都是需要遍历链表,但是在遍历前需要判断链表是否为空,若为空,立即返回,不为空开始遍历,直到遇到NULL为止。

void printnode(node *p)
{
  if(p==NULL)
     return;
  node *q;
  if(p->next==NULL)
  {
      printf("the node is empty:\n");
      return;
  }
  else
  {
      q=p->next;
      while(q!=NULL)
      {
          printf("%d\t",q->data);
          q=q->next; 
     }
  }
  printf("\n"); 
}

5.链表的定位

   链表的定位返回值分两种,一种可以返回定位到某个节点的数据,另一种是返回以此节点的地址

   两个函数传入的参数都是一样,一个参数是需要操作的链表,另一个参数是节点在链表中的位置

int SearchNode(node *head,int pos)
{
    if(head==NULL)
        return -1;
   if(pos<=0)
   {
        printf("the pos can not smaller than 0\n");
        return -1;
   }
   else if(head->next==NULL)
   {
      printf("the node is empty\n");
      return -1;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return -1;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head->data; 
}
node * SearchNode1(node *head,int pos)
{
   if(pos<0)
   {
        printf("the pos can not smaller than 0\n");
        return NULL;
   }
   else if(head->next==NULL)
   {
      printf("the node is empty\n");
      return NULL;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return NULL;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head; 
}

6.删除链表中的节点

   先找到要删除节点的前一个节点,如果已经到了尾节点,则不能删除

bool DeleteNode(node *head,int pos,int &data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if((!q)||(!q->next))
   {
   	   printf("delete failed:\n");
   	   return false;
   }
   item=q->next;
   data=item->data;
   q->next=item->next;
   free(item);
   item=NULL;
   printf("Delete success:\n");
   return true;
}

7.插入节点

先找到要插入节点的前一个节点

bool InsertNode(node *head,int pos,int data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if(!q)
   {
   	   printf("Insert failed:\n");
   	   return false;
   }
   item=(node*)malloc(sizeof(node));
   item->data=data;
   item->next=q->next;
   q->next=item;
   printf("Insert success:\n");
   return true;
}

8.逆置操作

void ReverseNode(node *head)
{
	if(head==NULL)
	   return ;
    if(head->next==NULL)
       return ; 
	node *p,*q,*r;
	p=head->next;
	q=p->next;
	p->next=NULL;
	while(q!=NULL)
	{
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	head->next=p;
}

完整程序如下:

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
typedef struct _Node
{
  int data;
  struct _Node *next; 
}node;
node *CreatNode_Head(int n)
{
   int i=0;
   node *head,*p;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=NULL;
   srand(time(0));
   for(int i=0;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=rand()%100+1;
	  printf("p->data:%d\t",p->data);
   	  p->next=head->next;
   	  head->next=p;
   }
   return head;
}
node *CreatNode_Tail(int n)
{
   int i=0;
   node *head,*p,*q;
   head=(node*)malloc(sizeof(node));
   if(head==NULL)
        return NULL;
   head->next=NULL;
   q=head;
   srand(time(0));
   for(int i=0;i<n;++i)
   {
   	  p=(node*)malloc(sizeof(node));
   	  p->data=rand()%100+1;
   	  p->next=NULL;
	  printf("p->data:%d\t",p->data);
   	  q->next=p;
   	  q=p;
   }
   return head;
}
int length(node *p)
{
	if(p==NULL)
	   return -1;
    int len=0;
    while(p->next!=NULL)
    {
       len++;
       p=p->next;
    }
    return len;
}
void printnode(node *p)
{
  if(p==NULL)
     return;
  node *q;
  if(p->next==NULL)
  {
      printf("the node is empty:\n");
      return;
  }
  else
  {
      q=p->next;
      while(q!=NULL)
      {
          printf("%d\t",q->data);
          q=q->next; 
     }
  }
  printf("\n"); 
}

int SearchNode(node *head,int pos)
{
    if(head==NULL)
        return -1;
   if(pos<=0)
   {
        printf("the pos can not smaller than 0\n");
        return -1;
   }
   else if(head->next==NULL)
   {
      printf("the node is empty\n");
      return -1;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return -1;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head->data; 
}
node * SearchNode1(node *head,int pos)
{
   if(pos<0)
   {
        printf("the pos can not smaller than 0\n");
        return NULL;
   }
   else if(head->next==NULL)
   {
      printf("the node is empty\n");
      return NULL;
   }
   else if(pos>length(head))
   {
      printf("the pos over the length of the node\n");
      return NULL;
   }
   else
   {
      while(pos--)
      {
        head=head->next;
      }
   }
   return head; 
}

bool DeleteNode(node *head,int pos,int &data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if((!q)||(!q->next))
   {
   	   printf("delete failed:\n");
   	   return false;
   }
   item=q->next;
   data=item->data;
   q->next=item->next;
   free(item);
   item=NULL;
   printf("Delete success:\n");
   return true;
}

bool InsertNode(node *head,int pos,int data)
{
   if(head==NULL)
     return false;
   node *item=NULL;
   node *p=head;
   node *q=SearchNode1(head,pos-1);
   if(!q)
   {
   	   printf("Insert failed:\n");
   	   return false;
   }
   item=(node*)malloc(sizeof(node));
   item->data=data;
   item->next=q->next;
   q->next=item;
   printf("Insert success:\n");
   return true;
}

void ReverseNode(node *head)
{
	if(head==NULL)
	   return ;
    if(head->next==NULL)
       return ; 
	node *p,*q,*r;
	p=head->next;
	q=p->next;
	p->next=NULL;
	while(q!=NULL)
	{
		r=q->next;
		q->next=p;
		p=q;
		q=r;
	}
	head->next=p;
}

int main()
{
  int len=0;
  node *head,*head1;
  node *head2=NULL;
  printf("please input some date and creat a node\n");
  head=CreatNode_Head(10);
  printnode(head);
  printf("the length of the list is:%d\n",length(head));
  head1=CreatNode_Tail(10);
  printnode(head1);
  printf("the length of the list is:%d\n",length(head1));
  
  
  for(int i=0;i<length(head1);i++)
  {
  	  printf("SearchNode:index:%d,data:%d\n",i+1,SearchNode(head1,i+1));
  	  printnode(SearchNode1(head1,i+1));
  }
  InsertNode(head1,1,101); 
  printnode(head1);
  InsertNode(head1,13,102); 
  printnode(head1);
  
  int data=0;
  DeleteNode(head1,1,data);
  printf("the data is:%d\n",data);
  printnode(head1);
  
  DeleteNode(head1,12,data);
  printf("the data is:%d\n",data);
  printnode(head1);

  ReverseNode(head1);
  printnode(head1);

  return 0;
} 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单链表的几类操作介绍(头结点没有数据) 的相关文章

  • 2014阿里巴巴面试总结

    刚结束的一面 xff0c 可能昨天笔试题目做得还行 xff0c 今天中午电话我叫我1 30去面试 xff0c 时间紧急 xff0c 我吃完饭赶紧回宿舍小休息一会儿 xff0c 然后奔赴文三路的华星时代大厦 人太多了 xff0c 等到了2 2
  • 基类指针,子类指针,虚函数,override与final

    一 xff1a 基类指针与子类指针 span class token macro property span class token directive keyword include span span class token strin
  • web开发中实现页面记忆的几种方式

    一 前言 在前段时间公司有个需求是对前一个页面的操作进行记忆 xff0c 例如分页的样式 xff0c 选中的条件等 之前是用的session去存储记忆数据 xff0c 老大让我调研一下目前比较合理的方式然后分析一下 xff0c 这里以本篇博
  • 基于VINS与FastPlanner的无人机自主飞行Gazebo仿真

    项目来源及展示 xff1a https www bilibili com video BV1WK4y1V7um from 61 search amp seid 61 12548150687335659873 基本思路 xff1a 采用Gaz
  • RGB-D SLAM 相关总结

    目录 一 RGB D SLAM是什么 xff1f 二 D435i说明 三 RGB D SLAM研究现状 1 现有的RGB D SLAM方法 1 1 前端 1 2 后端 1 3 闭环检测 1 4 制图 2 优秀RGB D SLAM介绍 2 1
  • VINS-Mono学习(一)——数据预处理

    void push back double dt const Eigen Vector3d amp acc const Eigen Vector3d amp gyr dt buf push back dt acc buf push back
  • VINS-Mono学习(四)——回环检测与重定位

    目录 1 闭环检测常用方法有哪些 xff1f 2 ORB SLAM2中Loop Closing的具体实现流程是怎样的 xff1f 3 VINS回环检测与重定位 四自由度位姿图优化 3 1 第一部分 xff1a 回环检测与重定位 3 1 1
  • LADRC的学习——总概

    作者 xff1a 墨心 日期 xff1a 2019 7 25 xff1b 学习LADRC结构 xff1a 1 学习PID的相关知识 xff0c 作为学习ADRC的基础铺垫 xff0c 在simulink中搭建模块 xff0c 通过调节参数
  • LADRC的学习——PID的学习

    PID部分的学习 上文介绍了ADRC的理论 xff0c 并试着按照自己的理解用Matab编程实现韩老师论文中的算法 xff0c 但是对调节参数和一些地方还不太懂 xff0c 因此我打算从头开始理解 xff0c 从PID的好坏开始学习理解 x
  • LADRC的学习——换被控对象进行仿真测试

    LADRC控制器的检验 用不同的被控对象测验 一 前文总结 这篇文章主要根据清华大学的硕士陈星写的论文 xff1a 自抗扰控制器参数整定方法及其热工过程中的应用 进行学习 参考文献为 xff1a 1 Zhiqiang Gao Scaling
  • ROS-3DSLAM(二)lvi-sam项目认识

    2021SC 64 SDUSC xff08 二 xff09 lvi sam项目认识 一 SLAM简介 SLAM是Simultaneous Localization and Mapping xff08 同时定位 43 建图 xff09 独立的
  • KMP算法——字符串匹配问题

    贴上原址 xff1a http www ruanyifeng com blog 2013 05 Knuth E2 80 93Morris E2 80 93Pratt algorithm html 感觉这篇文章讲得很不错 xff0c 很容易懂
  • ROS-3DSLAM(十六)lvi-sam项目总结

    2021SC 64 SDUSC 学习内容概览 本次的项目lvi sam主要分为两个大的模块 xff1a lidar模块和visual模块 我们小组学习先进行了lidar模块的学习 xff0c 然后进行的visual模块 每个模块都分成了若干
  • 项目实训 - 智能车系统 - 第八周记录

    项目实训 智能车系统 第八周记录 日期 xff1a 4 11 4 17 项目进度 本周工作进展 xff1a 完成了雷达驱动的编写 xff08 未测试 xff09 完成imu驱动的编写 xff08 未测试 xff09 与可视化部分进行对接 1
  • IIC详细解答+ 面试 + 代码

    目录 IIC背景提炼部分 xff08 面试 xff09 xff08 详解 43 代码 xff09 协议部分IIC部分初始化 IIC 的 IO 口IIC 开始信号IIC发送一个字节IIC 读一个字节响应ACK和非响应NACKIIC 停止信号
  • FreeRTOS内核学习高级篇-调度器使用

    学习资料链接 http wiki csie ncku edu tw embedded freertos https freertos blog csdn net article details 51190095 介绍 调度器是FreeRTO
  • ArduPilot日志系统探索(一)

    先把官方网站上日志相关的说明翻译下来 xff1a ArduPilot Documentation ArduPilot documentation 页面 xff1a Logs Copter documentation 与日志记录和分析相关的主
  • 暗影精灵4 安装双系统方法:win10 + ubuntu16.04 LTS

    准备工作 1 重要 xff1a 备份文件 xff0c 安装双系统有成功的 xff0c 也有失败的 xff0c 做好备份工作更保险 xff01 2 需要一个制作启动盘的U盘 gt 61 8G xff0c UltralSO刻录软件 xff0c
  • [现代控制理论]9_状态观测器设计_龙伯格观测器

    现代控制理论 11 现代控制理论串讲 完结 pdf获取 现代控制理论 10 可观测性与分离原理 观测器与控制器 现代控制理论 9 状态观测器设计 龙伯格观测器 现代控制理论 8 5 线性控制器设计 轨迹跟踪simulink 现代控制理论 8
  • [非线性控制理论]9_非线性控制理论串讲

    非线性控制理论 1 Lyapunov直接方法 非线性控制理论 2 不变性原理 非线性控制理论 3 基础反馈稳定控制器设计 非线性控制理论 4 反馈线性化 反步法 非线性控制理论 5 自适应控制器 Adaptive controller 非线

随机推荐

  • ubuntu16.04安装realsenseD435i sdk

    此处安装的intel realsense sdk2 0 xff0c 官方安装 xff0c 若从源码自行编译 xff0c 不可参考本教程 github原网址https github com IntelRealSense librealsens
  • C++中map用法

    一 头文件 include lt map gt map是一种以键 值 key value 存储的数据类型 map中的数据默认按照key的值从小到大排序 value若为Int类型 xff0c 默认为0 map不允许容器中有重复key值元素 m
  • 二维数组的子数组之和的最大值

    对于一维的数组 xff0c 要求其子数组之和的最大值很简单 xff0c 动态规划轻松解决 xff0c 复杂度O N span style background color rgb 255 255 255 font family none f
  • gazebo save world as 之后卡死问题的解决方法

    最近在学习ROS xff0c 后面在用gazebo做仿真的时候 xff0c 在gazebo中加入任意模型 xff0c 然后点击save world as然后卡死的问题一直无法解决 尝试过的思路比如更换版本 xff0c 下载源代码编译后安装
  • 这才是企业级的oss-spring-boot-starter

    本文主要讲解企业级OSS对象存储服务Spring Boot Starter制作 xff0c 开箱即用 xff0c 为项目进行赋能 基于AmazonS3协议 xff0c 适配市面上的对象存储服务如 xff1a 阿里云OSS 腾讯COS 七牛云
  • 项目管理-项目相关方管理

    识别相关方 相关方分析 会产生相关方清单和关于相关方的各种信息 xff0c 例如 xff0c 在组织内的位置 在项目中的角色 与项目的厉害关系 期望 态度 xff08 对项目的支持程度 xff09 xff0c 以及对项目信息的兴趣 权力 利
  • 分享关于AI的那些事儿

    机器人很厉害 给人治病的ibm 的Watson 沃森 击败世界围棋冠军的AlphaGo阿尔法狗 陪你聊天的机器人 数据标注 木马识别 恶意访问拦截 智能家居 但是17年首次出现了机器人获得国籍 这个机器人叫做索菲亚 这是一个类似人类的机器人
  • 408 知识点笔记——计组(总线、输入/输出系统)

    6 总线 总线的基本概念 分时和共享是总线的两个特点 xff0c 分时是指同一时刻只允许有一个部件向总线发送信息 xff0c 共享可以允许多个部件同时从总线上接收信息 总线特性 物理特性 xff1a 如插头与插座的几何尺寸 形状 引脚个数及
  • Ubuntu18.04安装Kalibr各种问题总结

    近期需要作相机与IMU的联合标定 xff0c 安装Kallibr过程遇到好多问题 xff0c 前前后后折腾了3天 xff0c 终于可以标定了 这里记录一下问题 xff0c 希望可以帮助更多人 1 catkin make过程中下载SuiteS
  • 01-搭建Vue脚手架(vue-cli)

    一 那么我们就从最简单的环境搭建开始 xff1a 安装node js xff0c 从node js官网下载并安装node xff0c 安装过程很简单 xff0c 一路 下一步 就可以了 xff08 傻瓜式安装 xff09 安装完成之后 xf
  • vscode打开终端的快捷键是啥? VScode打开终端的三种方法

    方法1 xff1a 打开终端的快捷方法 打开VScode后 xff0c 鼠标左键单击窗口顶部的 帮助 xff08 如下图红圈标注 xff09 xff0c 在下拉列表中找到 键盘快捷方式参考 xff08 如下图红框标注 xff09 鼠标左键点
  • VS Code保存后自动格式化Vue代码---Vetur

    在VS Code里面编辑Vue代码 xff0c 通常我们会安装插件Vetur xff0c 本次介绍的格式化代码也依赖于Vetur插件 具体见一下步骤 注 xff1a VS Code版本为1 74 3 1 安装插件Vetur 2 配置自动格式
  • vscode中怎样格式化js代码_vscode如何格式化代码

    vs code格式化代码的快捷键如下 xff1a 在Windows上 Shift 43 Alt 43 F 推荐学习 xff1a vscode入门教程 在Mac上 Shift 43 Option 43 F 在Ubuntu上 Ctrl 43 S
  • VsCode使用Ctrl+S保存代码自动格式化Html/Css/JS

    第一步 xff1a 点击文件 首选项 设置 xff08 快捷键 xff1a Ctrl 43 xff09 第二步 xff1a 在搜索框里面输入emmet xff0c 选择工作区 点击 在settings json 中编辑 xff08 红色框的
  • 百度面试基础问题

    上午百度面试 xff0c 我投的测试 xff0c 文三路伊美大酒店 xff0c 面了接近一个小时 xff0c 问了很多基础的东西 xff0c 我有些混淆也有些回答得不全面 xff0c 可能跪了 xff0c 记录一下面试题吧 xff0c 权当
  • vscode使用git

    1 vscode配置git 一 VS code 配置git 1 下载安装git 2 如果要在VS Code里面使用Git则需要在编辑器内配置git path xff08 1 xff09 windows系统 xff0c 打开cmd xff0c
  • 后台管理系统的权限控制与管理

    此文章根据视频教程进行整理前端面试官必问系列 后台系统的权限控制与管理 xff0c 建议搭配视频教程一起食用效果更佳 https www bilibili com video BV15Q4y1K79c 在Web 系统中 xff0c 权限很久
  • 笔试题 11

    1 通过css控制 xff0c 是页面的一个div不可见的方法有哪些 xff1f 1 使用display none来隐藏div 我们可以使用display none属性来隐藏所有的信息 xff0c 包括文本和图片 xff0c 语法为 xff
  • 路由传参的三种方式

    带参数 xff1a 传参方式可划分为 params 传参和 query 传参 xff0c 而 params 传参又可分为在 url 中显示参数和不显示参数两种方式 1 params 传参 xff08 显示参数 xff09 又可分为 声明式
  • 单链表的几类操作介绍(头结点没有数据)

    1 定义一个单链表的结构体 typedef struct Node int data struct Node next node 2 创建一个链表 xff0c 这里分为头插法和尾插法 node CreatNode Head int n in