java单链表的实现自己动手写一个单链表
单链表:单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
自己手动写一个单链表:
首先,定义一个节点类:
- package com.wei;
- public class Link {
- public int data;// 存放数据
- public Link next;// 存放下一个节点
- public Link(int data) {
- this.data = data;
- }
- public Link(int data, Link next) {
- this.data = data;
- this.next = next;
- }
- public Link() {
- }
- public void display() {
- System.out.println(data + " ");
- }
- }
第二部分是定义一个链表类:
- package com.wei;
- public class LinkList {
- public Link frist;// 定义一个头节点
- public Link last;//尾指针永远指向头节点
- public int size = 0;// 节点的位置
- public LinkList() {
- this.frist = null;//
- }
- /**
- * 判断链表是否为空
- *
- * @return
- */
- public boolean isis() {
- return size == 0;
- }
- /**
- * 头插法
- *
- * @param data
- */
- public void addfrist(int data) {
- Link L = new Link(data);
- L.next = frist;
- frist = L;
- size++;
- }
- /**
- * 尾插法
- *
- * @param data
- */
- public void addlast(int data) {
- if (frist == null) {
- frist = new Link(data);
- last = frist;
- } else {
- Link newL = new Link(data);
- last.next = newL;
- last = newL;
- }
- size++;
- }
- /**
- * 从头删除
- *
- * @return
- */
- public Link removefrist() {
- Link d = frist;
- frist = d.next;
- size--;
- return d;
- }
- /**
- * 删除最后一个
- */
- public void dellast() {
- Dell(size - 1);
- }
- /**
- * 获取链表长度
- */
- public void displayAllLink() {
- Link cure = frist;
- while (cure != null) {
- cure.display();
- cure = cure.next;
- }
- System.out.println("长度" + size);
- }
- /**
- * 获取指定位置的节点元素
- *
- * @param index
- * @return
- */
- public Link getData(int index) {
- if (index < 0 && index > size - 1) {
- throw new IndexOutOfBoundsException("越界");
- }
- Link count = frist;
- for (int i = 0; i < size && count != null; i++, count = count.next) {
- if (i == index) {
- return count;
- }
- }
- return null;
- }
- /**
- * 按值查找指定位置
- *
- * @param element
- * @return
- */
- public int selectIndex(int element) {
- Link current = frist;
- for (int i = 0; i < size && current != null; i++, current = current.next) {
- if (current.data == element) {
- return i;
- }
- }
- return -1;
- }
- /**
- * 删除链表中数值最大的元素
- */
- public void delMax() {
- // 要遍历的链表
- Link cu = frist;
- // 初始化一个节点,当中间变量
- Link cc = new Link(0);
- // 遍历
- for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
- if (cu.data > cc.data) {
- cc.data = cu.data;
- }
- }
- int data = cc.data;
- int number = selectIndex(data);
- Dell(number);
- }
- /**
- * 删除链表中数值最小的元素
- */
- public void delMin() {
- // 要遍历的链表
- Link cu = frist;
- // 初始化一个节点,当中间变量
- Link cc = new Link(0);
- // 遍历
- for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
- if (cu.data < cc.data) {
- cc.data = cu.data;
- }
- }
- int data = cc.data;
- int number = selectIndex(data);
- Dell(number);
- }
- /**
- * 从指定位置处插入数据
- *
- * @param t
- * @param index
- */
- public void insert(int t, int index) {
- if (index < 0 || index > size) {
- throw new IndexOutOfBoundsException("索引超出线性表范围");
- }
- if (frist == null) {
- addlast(t);
- } else {
- if (index == 0) {
- addfrist(t);
- } else {
- Link k = getData(index - 1);
- k.next = new Link(t, k.next);
- size++;
- }
- }
- }
- /**
- * 从指定位置处删除
- *
- * @param index
- */
- public void Dell(int index) {
- if (index < 0 || index > size) {
- throw new IndexOutOfBoundsException("索引超出线性表范围");
- }
- Link del = null;
- if (index == 0) {
- del = frist.next;
- frist = frist.next;
- } else {
- Link neL = getData(index - 1);
- del = neL.next;
- neL.next = del.next;
- del.next = null;
- }
- size--;
- }
- /**
- * 清空链表
- */
- public void clear() {
- frist = null;
- last = null;
- size = 0;
- }
- /**
- * 按从小到大排序
- */
- public void Min_to_Max() {
- // 要遍历的链表
- Link cu = frist;
- // 记录最小值
- int min;
- while (cu != null) {
- // 内重循环从当前节点的下一个节点循环到尾节点,找到和外重循环的值比较最小的那个,然后与外重循环进行交换
- Link nextLink = cu.next;
- while (nextLink != null) {
- // 比外循环小的值放在前面
- if (nextLink.data < cu.data) {
- min = nextLink.data;
- nextLink.data = cu.data;
- cu.data = min;
- }
- nextLink = nextLink.next;
- }
- cu = cu.next;
- }
- }
- /**
- * 按从大到大排序
- */
- public void Max_to_Min() {
- // 要遍历的链表
- Link cu = frist;
- // 记录最小值
- int min;
- while (cu != null) {
- // 内重循环从当前节点的下一个节点循环到尾节点
- //找到和外重循环的值比较最小的那个,然后与外重循环进行交换
- Link nextLink = cu.next;
- while (nextLink != null) {
- // 比外循环小的值放在前面
- if (nextLink.data > cu.data) {
- min = nextLink.data;
- nextLink.data = cu.data;
- cu.data = min;
- }
- nextLink = nextLink.next;
- }
- cu = cu.next;
- }
- }
- }
最后是测试类:
- package com.wei;
- public class Test {
- public static void main(String [] arr) {
- LinkList g = new LinkList();
- g.addlast(13);
- g.addlast(16);
- g.addlast(-3);
- g.addlast(8);
- g.addlast(5);
- g.addlast(22);
- g.Min_to_Max();
- g.displayAllLink();
- g.Max_to_Min();
- g.displayAllLink();
- }
- }
一条链表就这样写完了,需要什么功能可以自己扩展。
java单链表的实现自己动手写一个单链表的更多相关文章
- 死磕 java同步系列之自己动手写一个锁Lock
问题 (1)自己动手写一个锁需要哪些知识? (2)自己动手写一个锁到底有多简单? (3)自己能不能写出来一个完美的锁? 简介 本篇文章的目标一是自己动手写一个锁,这个锁的功能很简单,能进行正常的加锁. ...
- 死磕 java线程系列之自己动手写一个线程池
欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. (手机横屏看源码更方便) 问题 (1)自己动手写一个线程池需要考虑哪些因素? (2)自己动手写 ...
- 自己动手写一个服务网关-java
自己动手写一个服务网关 原文链接:https://www.cnblogs.com/bigben0123/p/9252444.html 引言 什么是网关?为什么需要使用网关? 如图所示,在不使用网关的情 ...
- 动手写一个简单版的谷歌TPU-指令集
系列目录 谷歌TPU概述和简化 基本单元-矩阵乘法阵列 基本单元-归一化和池化(待发布) TPU中的指令集 SimpleTPU实例: (计划中) 拓展 TPU的边界(规划中) 重新审视深度神经网络中的 ...
- 动手写一个简单版的谷歌TPU-矩阵乘法和卷积
谷歌TPU是一个设计良好的矩阵计算加速单元,可以很好的加速神经网络的计算.本系列文章将利用公开的TPU V1相关资料,对其进行一定的简化.推测和修改,来实际编写一个简单版本的谷歌TPU.计划实现到行为 ...
- 动手写一个简单的Web框架(模板渲染)
动手写一个简单的Web框架(模板渲染) 在百度上搜索jinja2,显示的大部分内容都是jinja2的渲染语法,这个不是Web框架需要做的事,最终,居然在Werkzeug的官方文档里找到模板渲染的代码. ...
- 动手写一个简单的Web框架(Werkzeug路由问题)
动手写一个简单的Web框架(Werkzeug路由问题) 继承上一篇博客,实现了HelloWorld,但是这并不是一个Web框架,只是自己手写的一个程序,别人是无法通过自己定义路由和返回文本,来使用的, ...
- 动手写一个简单的Web框架(HelloWorld的实现)
动手写一个简单的Web框架(HelloWorld的实现) 关于python的wsgi问题可以看这篇博客 我就不具体阐述了,简单来说,wsgi标准需要我们提供一个可以被调用的python程序,可以实函数 ...
- 教你如何使用Java手写一个基于链表的队列
在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...
随机推荐
- 爬虫笔记(一)——快速使用urllib库
本人以前用的都是python2.7,但看网上很多教程都是以python3为例的,所以便切换版本,导入urllib.lxml.beautifulsoup4等库. 下面介绍下两个版本对urllib库的区别 ...
- bzoj1396识别子串(SAM+线段树)
复习SAM板子啦!考前刷水有益身心健康当然这不是板子题/水题…… 很容易发现只在i位置出现的串一定是个前缀串.那么对答案的贡献分成两部分:一部分是len[x]-fa~len[x]的这部分贡献会是r-l ...
- Codeforces 558E A Simple Task(计数排序+线段树优化)
http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...
- Work Scheduling(带反悔的贪心)
https://www.luogu.org/problem/P2949 题目描述 Farmer John has so very many jobs to do! In order to run th ...
- ubantu中的mysql命令
查看mysql的安装目录:which mysql 进入mysql的运行状态:mysql -uroot -p 56..a_
- day31-hmac模块检测客户端是否合法
#如果客户端知道服务端的ip地址和端口,就可以连接服务端,信息不安全. #使用os.urandam随机生成32位bytes,然后hmac加密之后再发送给客户端. #server: import soc ...
- ioctl函数的使用之查看终端屏幕大小
要想查看一个终端屏幕的大小,可以使用ioctl()函数,步骤如下. 1.首先找到对应终端的文件号,一般在 /dev/pts/....(0,1,2..).具体几号需要自己验证.如在pts目录下使用命 ...
- Python 网站后台扫描
title date layout tags Python 网站后台扫描 2018-05-08 post Python #!/usr/bin/python # This was written for ...
- [LC] 426. Convert Binary Search Tree to Sorted Doubly Linked List
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...
- 84)PHP,SQL注入基础讲解
怎么预防: 填写防止SQL注入的代码: