/*以头插法,创建长度为n的单链表,并实现对其的增、删、改、查*/

#include<stdio.h>
#include<stdlib.h>struct node
{int data;struct node *next;
};struct node *Creat_List(struct node *head, int n)    //创建链表
{struct node *p;for(int i=0;i<n;i++){p = (struct node *)malloc(sizeof(struct node));scanf("%d",&p->data);p->next = head;head = p;}return head;
}void Put(struct node *head)      //打印链表
{struct node *p;p = head;while(p != NULL){printf("%d ",p->data);p = p->next;}printf("\n");
}struct node* Insert(struct node *head,int k,int num)          //在k处增加元素num
{struct node *p;p = head;for(int i=0;i<k;i++){p = p->next;}struct node *temp;temp = (struct node *)malloc(sizeof(struct node));temp->data = num;temp->next = p->next;p->next = temp;return head;
}struct node *Delete(struct node *head, int k)
{struct node *p;p = head;for(int i=1;i<k-1;i++){p = p->next;}struct node *temp;temp = p->next;p->next = temp->next;return head;
}bool Find(struct node *head,int num)
{struct node *p;p = head;while(p != NULL){if(p->data == num){return true;}p = p->next;}return false;
}struct node *Change(struct node *head,int k,int num) //将数字k改为num
{struct node *p;p = head;while(p!=NULL){if(p->data == k){p->data = num;}p = p->next;}return head;
}int main()
{int n;                  //链表长度scanf("%d",&n);struct node *head=NULL;head = Creat_List(head, n);Put(head);head = Insert(head,2,1000);//Put(head);head = Delete(head,4);//Put(head);head = Change(head,4,6);//Put(head);bool ok = Find(head,7);if(ok)printf("yes\n");elseprintf("no\n");return 0;
}

View Code

转载于:https://www.cnblogs.com/alan-W/p/5887002.html

单链表的几个基本操作相关推荐

  1. 单链表实现队列的基本操作(入队,出队)

    单链表实现队列的基本操作(包括初始化队列,入队,出队) 构造队列结构体 struct node {int data;node *next; };struct queue {node *head, *t ...

  2. 单链表的十三个基本操作(全)

    目录 一.初始化单链表 二.插入 三.删除 四.查找 4.1 按位查找 4.2 按数查找

  3. 单链表——单链表的定义及基本操作(初始化、头插法尾插法建表、查找、插入、删除、判空等)

    文章目录 单链表的定义 单链表上的操作 初始化 建立单链表 头插法建立单链表 尾插法建立单链表 遍历单链表 求单链表的长度 查找操作 按值查找 按位查找 插入操作 删除操作 判空操作 完整代码及实例 ...

  4. (数据结构与算法)数组和单链表实现栈的基本操作

    数组实现栈 栈接口 public interface Stack<E> {int getSize();boolean isEmpty();void push(E e);E pop();E ...

  5. 【数据结构】循环单链表的实现(C语言)

    循环单链表应掌握以下基本操作: 1.建立一个空的循环单链表. 2.获得循环单链表的最后一个结点的位置. 3.输出循环单链表中各结点的值. 4.在循环单链表中查找值为x的结点. 5.在循环单链表中第i个 ...

  6. 基于Python的数据结构实验——顺序表与单链表建立与操作(附详细代码和注释)

    1.创建一个名为 prac02_01.py 的文件,在其中编写一个顺序表的类,该类必须包含顺 序表的定义及基本操作,并通过以下步骤测试基本操作的实现是否正确,给出运行结果. (1)初始化一个顺序表 S ...

  7. c语言单链表设计报告,单链表实验报告

    <数据结构>实验报告二 分校: 学号: 日期: 班级: 姓名: 程序名: L2311.CPP 一.上机实验的问题和要求: 单链表的查找.插入与删除.设计算法,实现线性结构上的单链表的产生以 ...

  8. 数据结构个人笔记 第三课 顺序表和单链表

    数据结构个人笔记 第三课 顺序表和单链表 顺序表的基本操作 插入元素 删除元素 顺序表查找元素 顺序表更改元素 本节总结代码 单链表 链表的节点 头节点.头指针和首元结点 链表的创建(初始化) 本节总 ...

  9. C语言-线性表基本操作之单链表

    下面是单链表的基本操作: #include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct L ...

最新文章

  1. 简述DataInputStream和DataOuputStream
  2. java操作跨页的word cell,利用itext 生成pdf,处理cell 跨页问题 [转]
  3. 2018冬令营模拟测试赛(十八)
  4. python装饰器实例-python装饰器实例大详解
  5. 【杂谈】您想要的docker环境来了!
  6. 20个常用的正则表达式
  7. PyCharm 中选中一个变量/函数后,所有用到这个变量/函数的地方高亮显示,改配色方案
  8. openwrt 编译c语言,编译一个可以运行在openwrt上的c程序
  9. 【双路E5装机】2016.7.31整机升级计划
  10. Golang 大杀器之性能剖析 PProf
  11. python京东笔试题象棋马走到指定位置方法数
  12. 自学python在电脑上安装什么杀毒软件好_电脑上装那个杀毒软件比较好?
  13. Python监控股价并通过微信提醒
  14. pecl 安装指定版本swoole
  15. 暴力破解:利用正则功能自动提取爆破结果中的特征值
  16. TextSeek使用教程(初级篇) - 文件搜索软件
  17. matlab零序五次谐波,基于5次谐波的小电流接地系统故障选线方法仿真与分析.docx...
  18. 字体凹陷效果html,在PS中,想做凹进去的效果,怎么做?例如文字凹进木板中?...
  19. Python+Vue计算机毕业设计报刊征订管理系统uu609(源码+程序+LW+部署)
  20. Aurora8B10B IP使用 -01- 简介与端口描述

热门文章

  1. 在 Delphi 下使用 DirectSound (9): 效果器初步及 IDirectSoundFXGargle8 效果器
  2. 简明深度学习方法概述 Deep Learning:Methods and Application
  3. dsoframer控件注册,解注册和检查注册情况
  4. Error:No resource identifier found for attribute 'appComponentFactory' in packag
  5. linux下编译动态和静态链接库
  6. 【MVC】ASP.Net MVC 4项目升级MVC 5的方法
  7. 《VMware vSphere企业运维实战》——2.6 使用vCenter Converter迁移物理机到虚拟机
  8. 使用OpenGL一年后
  9. AppServ安装到一半卡住的问题
  10. CentOS开启与关闭防火墙