链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成。

结点API设计

类名 Node
构造方法 Node(T t, Node next ):创建Node对象
成员变量

T element:存储数据

Node next:指向下一个结点

结点类实现:

//结点类
public class Node<T> {//存储元素public T element;//指向下一个节点public Node next;public Node(T element,Node next){this.element=element;this.next=next;}
}

生成链表

    public static void main(String[] args) {//构建结点Node<Integer> first=new Node<Integer>(5,null);Node<Integer> second=new Node<Integer>(6,null);Node<Integer> third=new Node<Integer>(7,null);Node<Integer> fourth=new Node<Integer>(7,null);Node<Integer> fifth=new Node<Integer>(7,null);//生成链表first.next=second;second.next=third;third.next=fourth;fourth.next=fifth;}

单向链表

单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据,指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。

单向链表API设计

类名 LinkList
构造方法 LinkList():创建LinkList对象
成员方法

1.public void clear():清空线性表

2.public boolean isEmpty():判断线性表是否为空

3.public int length():获取线性表中元素的个数

4.public  T get(int i)获取第i个元素的值

5.public void insert(T t):在线性表中添加一个元素

6.public void insert(int i,T t):在第i个元素之前插入一个值为t的元素

7.public T remove(int i):删除第i个数据元素

8.public int indexOf(T t):返回线性表中首次出现该元素的索引值

成员内部类 private class Node结点类
成员变量

1.private Node head:记录首结点

2.private int N:记录链表的长度

代码实现:

public class LinkList<T> implements Iterable<T> {//记录头结点private Node head;//记录链表的长度private int N;public LinkList(){//初始化头结点head=new Node(null,null);N=0;}//清空链表public void clear(){head.next=null;head.element=null;N=0;}//链表长度public int length(){return N;}//判断链表是否为空public boolean isEmpty(){return  N==0;}//获取i处的元素public T get(int i){if(i<0||i>=N){throw new RuntimeException("位置不合法");}Node n=head.next;for(int index=0;index<i;index++){n=n.next;}return n.element;}//向链表中添加元素tpublic void inset(T t){//找到最后一个结点Node n=head;while(n.next!=null){n=n.next;}Node newNode=new Node(t,null);n.next=newNode;//链表长度+1N++;}//向指定位置i处,添加元素tpublic void insert(int i,T t){if(i<0||i>N){throw new RuntimeException("位置不合法");}//寻找位置i之前的结点Node pre=head;for(int index=0;index<=i-1;index++){pre=pre.next;}//位置i的结点Node curr=pre.next;//构建新的结点,让新结点指向位置i的结点Node newNode=new Node(t,curr);//让之前的结点指向新结点pre.next=newNode;//长度+1N++;}//删除指定位置i处的元素,并返回被删除的元素public T remove(int i){if(i<0||i>=N){throw new RuntimeException("位置不合法");}Node pre=head;for(int index=0;index<=i-1;index++){pre=pre.next;}//当前i位置的结点Node curr=pre.next;//前一个结点指向下一个结点,删除当前结点pre.next=curr.next;//长度-1N--;return (T) curr.element;}//查找t元素在链表中第一次出现的位置public int indexOf(T t){Node n=head;for(int i=0;n.next!=null;i++){n=n.next;if(n.element.equals(t)){return i;}}return -1;}private class Node {//存储元素T element;//指向下一个节点Node next;public Node(T element, Node next) {this.element = element;this.next = next;}}@Overridepublic Iterator iterator(){return new LIterator();}private class LIterator implements Iterator<T>{private Node n;public LIterator(){this.n=head;}@Overridepublic  boolean hasNext(){return n.next!=null;}@Overridepublic T next(){n=n.next;return n.element;}}
}class Test{public static void main(String[] args) throws Exception{LinkList<String> list=new LinkList<>();list.insert(0,"龍龍");list.insert(1,"龍哥");list.insert(2,"龍");list.insert(3,"龍");//测试length方法for(String s: list){System.out.println(s);}System.out.println(list.length());System.out.println("------------");//测试get方法System.out.println(list.get(2));System.out.println("------------");//测试remove方法String remove=list.remove(1);System.out.println(remove);System.out.println("---------------------");for(String s:list){System.out.println(s);}}
}

数据结构——单向链表相关推荐

  1. 数据结构-单向链表解决学生录入问题

    数据结构-单向链表 单向链表实现学生录入程序 通过main函数实现一切功能 通过调用函数实现 传送门结束 单向链表实现学生录入程序 将用户输入的不定个数的学生成绩按顺序编号并保存,以用户输入0作为录入 ...

  2. 数据结构单向链表线性结构_线性数据结构链表为何以及如何解释

    数据结构单向链表线性结构 Imagine you have gone to a crowded place, say to a k-pop concert with your friends and ...

  3. 【单向链表】数据结构——单向链表的介绍与代码实现笔记

    从今天开始将修炼数据结构专栏,将持续更新,分模块学习. 数据结构--单向链表 一.数据结构 1.什么是数据结构? 2.逻辑结构和物理结构 二.链表--线性结构 1.首先介绍下链表和数组的区别 2.链表 ...

  4. 简单数据结构——单向链表

    恩,准备重最简单的东西复习和学习一遍.那就从数据结构开始吧. 我尽量用公司的代码标准来进行编码.希望对刚刚开始学习数据结构的有一些帮助吧. 使我的代码可以简单易懂. 我记得上数据结构的第一个程序就是链 ...

  5. 数据结构—单向链表(详解)

    一.链表基础 链表是一种常见的数据结构,其中运用到了结构体指针,链表可以实现动态存储分配,换而言之,链表是一个功能强大的数组,可以在某个节点定义多种数据类型,可以实现任意的添加,删除,插入节点等.链表 ...

  6. 数据结构 | 单向链表学习总结

    单向链表学习总结 简介 总结 链表介绍 链表定义 链表函数 计算链表的长度 将列表转换为链表 LeetCode:剑指 Offer 22. 链表中倒数第k个节点 双向链表学习总结:python | 双向 ...

  7. 数据结构--单向链表

    单向链表的一种Go语言实现 package mainimport "fmt"type Node struct {no intname stringnext *Node }//实现尾 ...

  8. 数据结构——单向链表-双向链表

    1.单向链表按位置修改 //按位置修改 int updata_pos(linklist *L,int pos,datatype new_e) {if(NULL==L||empty(L)||pos< ...

  9. 数据结构单向链表(C++)

    head文件 #include<iostream> #define status int #define OK 1 #define ERROR 0 using namespace std; ...

最新文章

  1. 华硕WL-500W无线路由器使用感受
  2. KeyError: [] not found in axis_高调又有质感,女星最爱的至IN单品原来是它!
  3. 【网络安全】NFS服务安全加固
  4. 云炬60s看世界20211128
  5. 十九、抓包利器Charles的使用
  6. clob和blob是不是可以进行模糊查询_为省几十元买假内存条?金士顿内存条真伪查询与辨别方法...
  7. 一,彻底理解第一个C语言程序 Hello World
  8. freecodecamp_为什么您一定要参与freeCodeCamp的一个研究小组
  9. NHibernate剖析:Mapping篇之Mapping-By-Code(1):概览
  10. 怎么改python系统路径_动态修改python系统路径
  11. php做异地登录验证,PHP实现用户异地登录提醒功能的方法【基于thinkPHP框架】
  12. 前端小白案例-爱新鲜抽屉式特效制作
  13. 使用socks5代理实现SSH安全登录
  14. Zortam Mp3 Media Studio pro 25.40破解版
  15. 设计模式之实验二: 创建型设计模式实验
  16. 东北大学金工实习考试题库
  17. [js]调用google,51ditu和mapbar的地图API
  18. CSS3的新特性以及IE下的实现
  19. linux修改只读文件
  20. overflow:auto

热门文章

  1. android arcgis使用自定义图片背景底图(tif图片制作)
  2. CentOS7(8)安装/卸载MySQL
  3. 通过网页直接打开微信关注页面方法
  4. No Babel config file detected for xxx(未检测到Babel配置文件)和Already included file name解决方案
  5. 2022.11.15【bug笔记】|Error in FASTQ file at line 55: Line expected to start with ‘+‘, but found ‘G‘
  6. 如何将钉钉集成到FineReport插件中
  7. python常用工具类
  8. MacBook Air响一声白屏故障情况说明及解决
  9. 苹果笔记本开机问号白色问号?
  10. 网易面试题——快速无条件分支的 RGB、HSV 互转【GLSL】