java建立线性表的链式结构_Java实现线性表的链式存储

本文实例为大家分享了Java实现线性表的链式存储,供大家参考,具体内容如下

链表:一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

package algorithm.datastructure.linklist;

import java.util.NoSuchElementException;

/*

* 链表

* 物理存储上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现

*

* */

public class LinkedList {

private Node head;//头节点

private int size;//链表长度

static private class Node{

private int data;

private Node next;

public Node(){

}

private Node(int data,Node next){

this.data=data;

this.next=next;

}

}

//初始化空链表

public LinkedList(){

//head=null;

}

//添加元素

public Boolean add(int element){

linkLast(element);

return true;

}

//在某个位置之前添加元素

public Boolean add(int index,Integer element){

checkPositionIndex(index);

if (index==size){

linkLast(element);

} else {

linkBefore(element,node(index));

}

return true;

}

//根据下标获取元素

public int get(int index){

checkElementIndex(index);

return node(index).data;

}

//获取第一个元素

public Integer getFirst(){

Node f=head;

if (f==null){

throw new NoSuchElementException();

} else {

return f.data;

}

}

//获取最后一个元素

public Integer getLast(){

if (size==0){

throw new NoSuchElementException();

}

int index=size-1;

return node(index).data;

}

//删除第一个元素

public Integer removeFirst(){

Node f=head;

if (f==null){

throw new NoSuchElementException();

} else {

return unlink(head);

}

}

//删除最后一个元素

public Integer removeLast(){

if (size==0){

throw new NoSuchElementException();

}

int index=size-1;

return unlink(node(index));

}

//根据索引删除元素

public Integer remove(int index){

checkElementIndex(index);

return unlink(node(index));

}

//销毁链表

public void destroyList(){

clearList();

}

//将链表置为空表

public void clearList() {

for (Node p=head;p!=null;){

Node next=p.next;//记录下一个结点

p=null;//删除当前结点

p=next;//指向下一个结点

}

size=0;

head=null;

}

//遍历链表

public void traverseList(){

for (Node p=head;p!=null;){

System.out.println(p.data);

p=p.next;

}

}

//返回链表元素个数

public int size(){

return size;

}

//尾部添加结点

private void linkLast(int element){

Node cur =null,p;

if (size==0){//没有结点时

head=new Node(element,null);

size++;

return;

}

for (p=head;p!=null;){//有结点时候

cur=p;

p=cur.next;

}

cur.next= new Node(element,null);//尾部添加元素

size++;

}

//在某结点之前插入结点

private void linkBefore(int element,Node node){

if (node==null){

linkLast(element);

} else {

Node p=head,q=p.next;

if (node.data==p.data){//node为结点时候

head= new Node(element, p);//在头部插入元素

size++;

} else {

while (p!=null){

if (q.data==node.data) {

p.next= new Node(element,q);//在q之前(p之后)插入一个元素

size++;

break;

}

p=p.next;

q=p.next;

}

}

}

}

//删除结点

private Integer unlink(Node node){

Integer deleteNodeData=null;

Node p=null;

deleteNodeData=node.data;

if (node.data==head.data){//该节点为头结点

p=head;

head=p.next;

p=null;

size--;

} else {

Node q=head;

for (p=q.next;p!=null;){//使用两个指针,p,q

if (p.data==node.data){

break;

}

q=q.next;//p始终为q的next结点

p=q.next;

}

q.next=p.next;

p=null;//删除p

size--;

}

return deleteNodeData;

}

//数组下标是否越界

private Boolean isElementIndex(int index){

return index>=0&&index

}

//插入位置是否越界

public Boolean isPositionIndex(int index){

return index>=0&&index<=size;

}

//检验下标是否越界,抛出异常

private void checkElementIndex(int index){

if(!isElementIndex(index)){

try {

throw new IndexOutOfBoundsException("下标越界");

} catch (Exception e) {

e.printStackTrace();

}

}

}

//检验插入下标是否越界,抛出异常

private void checkPositionIndex(int index){

if(!isPositionIndex(index)){

try {

throw new IndexOutOfBoundsException("下标越界");

} catch (Exception e) {

e.printStackTrace();

}

}

}

//返回指定位置的元素

private Node node(int index){

int nowIndex = 0;

if(size>0){

for (Node p=head;p!=null;){

if (nowIndex==index){

return p;

}

p=p.next;

nowIndex++;

}

}

return null;

}

public static void main(String[] args) {

java.util.LinkedList linkedList0=new java.util.LinkedList();

linkedList0.add(6);

linkedList0.remove(0);

linkedList0.size();

linkedList0.peek();

//linkedList0.getFirst();

linkedList0.clear();

//测试

LinkedList linkedList=new LinkedList();

linkedList.add(2);

linkedList.add(6);

linkedList.add(0);

linkedList.add(3);

linkedList.add(8);

linkedList.add(10);

System.out.println(linkedList.get(0));

System.out.println(linkedList.getFirst());

System.out.println(linkedList.getLast());

System.out.println(linkedList.get(5));

System.out.println(linkedList.remove(5));

System.out.println(linkedList.remove(4));

linkedList.remove(2);

linkedList.remove(0);

linkedList.remove(0);

linkedList.remove(0);

linkedList.add(2);

linkedList.add(6);

linkedList.add(0);

linkedList.add(3);

linkedList.add(8);

linkedList.add(10);

linkedList.removeFirst();

linkedList.removeFirst();

linkedList.removeLast();

System.out.println(linkedList.size());

System.out.println("遍历链表");

linkedList.traverseList();

linkedList.add(0,4);

linkedList.add(0,5);

linkedList.add(2,5);

linkedList.add(4,5);

linkedList.add(6,9);

linkedList.add(8,11);

linkedList.add(9,222);

// linkedList.linkBefore(3,new Node(3,null));

// linkedList.linkBefore(3,new Node(2,null));

// linkedList.linkBefore(3,new Node(2,null));

// linkedList.linkBefore(7,new Node(2,null));

// linkedList.linkBefore(9,new Node(7,null));

// linkedList.linkBefore(9,new Node(8,null));

System.out.println("遍历链表");

linkedList.traverseList();

linkedList.clearList();

}

}

以上就是Java简单实现线性表的链式存储,更多功能可参考Java集合中的LinkedList实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/329170.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dotnet core 开发体验之Routing

开始 回顾上一篇文章&#xff1a;dotnet core开发体验之开始MVC 里面体验了一把mvc,然后我们知道了aspnet mvc是靠Routing来驱动起来的&#xff0c;所以感觉需要研究一下Routing是什么鬼。 Routing简单使用体验 首先我们用命令yo aspnet创建一个新的空web项目。&#xff08;Yeo…

线程间协作的两种方式:wait、notify、notifyAll和Condition

转载自 线程间协作的两种方式&#xff1a;wait、notify、notifyAll和Condition在前面我们将了很多关于同步的问题&#xff0c;然而在现实中&#xff0c;需要线程之间的协作。比如说最经典的生产者-消费者模型&#xff1a;当队列满时&#xff0c;生产者需要等待队列有空间才能继…

格密码基础:q-ary格

目录 一. 格密码的重要性 二. 格密码基础 2.1 格点的另一种理解方式 三. q-ary格 3.1 q-ary垂直格 3.2 q-ary格 3.3 二者结合 四. 论文中的q-ary格 4.1 定理1 4.2 定理2 4.3 定理3 一. 格密码的重要性 格密码的基础是研究格点上的困难问题&#xff0c;这种格点使用…

java动脑公开课_java课堂动手动脑

实验任务一&#xff1a;阅读并运行示例PassArray.java.1)源代码&#xff1a;package demo;//PassArray.java//Passing arrays and individual array elements to methodspublic class PassArray {public static void main(String[] args) {int a[] { 1, 2, 3, 4, 5 };String o…

dotnet core开发体验之开始MVC

开始 在上一篇文章&#xff1a;dotnet core多平台开发体验 &#xff0c;体验了一把dotnet core 之后&#xff0c;现在想对之前做的例子进行改造&#xff0c;想看看加上mvc框架是一种什么样的体验&#xff0c;于是我就要开始诞生今天的这篇文章来分享我的感受了。 一、项目改造加…

Java时间处理第三方包:Joda-Time

转载自 Java时间处理第三方包&#xff1a;Joda-TimeJoda-Time provides a quality replacement for the Java date and time classes.Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (…

Visual Studio Code五月版本更新

开源项目、跨平台代码编辑器Visual Studio Code刚发布了其1.2版本&#xff08;虽然说是2016年5月发布&#xff0c;但其实是在6月交付的&#xff09;。和往常一样&#xff0c;这次发布的版本中对于很大一部分功能都进行了改善&#xff0c;其中最值得关注的可能是一种全新整合的终…

关于SimpleDateFormat时间格式化线程安全问题

转载自 关于SimpleDateFormat时间格式化线程安全问题昨天推送的文章《关于创建和销毁对象》一文中&#xff0c;2.1重复利用对象这一小节所举的SimpleDateFormat格式化时间的例子是不合适的&#xff0c;因为多线程场景下&#xff0c;SimpleDateFormat存在线程安全问题。在此&am…

聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer

跨平台是ASP.NET Core一个显著的特性&#xff0c;而KestrelServer是目前微软推出了唯一一个能够真正跨平台的Server。KestrelServer利用一个名为KestrelEngine的网络引擎实现对请求的监听、接收和响应。KetrelServer之所以具有跨平台的特质&#xff0c;源于KestrelEngine是在一…

pdfbox java.lang.outofmemoryerror_Apache PDFBox 1.8.11 发布,Java 的 PDF 处理类

Apache PDFBox 1.8.11 发布&#xff0c;此版本是个增量 bug 修复版本&#xff0c;包括大量 bug 修复和改进。现已提供下载&#xff1a;主要改进内容&#xff1a;Bug 修复[PDFBOX-962] - All sort of Problems when importing Xfdf files into PDFs ->damaged pdfs and NPEs[…

Java8 Striped64 和 LongAdder

转载自 Java8 Striped64 和 LongAdder 数据 STRIPING 根据维基百科的这段说明&#xff1a;In computer data storage, data striping is the technique of segmenting logically sequential data, such as a file, so that consecutive segments are stored on different phys…

Roslyn项目系统简介

发布15年后&#xff0c;Microsoft终于开始替换Visual Studio中基于COM的C#和Visual Basic项目系统。Microsoft谈及很多有必要放弃目前所用系统的原因&#xff1a; 原生且基于COM单线程并与UI线程绑定难以通过和&#xff08;不同用途的&#xff09;子类型类扩展到聚合之外与Visu…

fastdfs 集群 java_FastDFS集群部署(转载 写的比较好)

之前介绍过关于FastDFS单机部署&#xff0c;详见博文&#xff1a;FastDFSNginx(单点部署)事例下面来玩下FastDFS集群部署&#xff0c;实现高可用(HA)服务器规划&#xff1a;跟踪服务器1【主机】(Tracker Server)&#xff1a;192.100.139.121跟踪服务器2【备机】(Tracker Server…

简析.NET Core 以及与 .NET Framework的关系

至2002微软公司推出.NET平台已近15年&#xff0c;在互联网快速迭代的浪潮中&#xff0c;许多语言已被淘汰&#xff0c;同时也有更多新的语言涌现&#xff0c;但 .Net 依然坚挺的站在系统开发平台的一线阵营中&#xff0c;并且随着.NET Core 即将到来(2016年6月27日)的正式版&am…

Jdk1.8 JUC源码增量解析(2)-atomic-LongAdder和LongAccumulator

转载自 Jdk1.8 JUC源码增量解析(2)-atomic-LongAdder和LongAccumulator功能简介&#xff1a;LongAdder是jdk1.8提供的累加器&#xff0c;基于Striped64实现。它常用于状态采集、统计等场景。AtomicLong也可以用于这种场景&#xff0c;但在线程竞争激烈的情况下&#xff0c;Long…

mysql 密码hash算法_如何用hash创建一个mySQL用户(‘sha256’,$salt.$password)?

我肯定错过了什么.我想为select-only事务设置数据库用户帐户,但mysql不允许我在创建用户帐户时选择密码的哈希方法.这失败了&#xff1a;GRANT SELECT ON myDB.* TO selectuserlocalhostIDENTIFIED BY hash(sha256, salted-myfakelongrandompasswordstring);错误1064(42000)&am…

为什么微软逐步转变为开源公司

微软目前拥有自己的 BSD Unix 操作系统&#xff0c;支持 Ubuntu 作为 Windows 10 的一个子系统&#xff0c;最近又将 Xamarin 软件开发工具包开源&#xff0c;所有这些意味着微软已不再是比尔盖茨和史蒂夫鲍尔默的微软了。 我知道这很难令人相信&#xff0c;但微软确实正大步走…

Jdk1.8 JUC源码增量解析(1)-atomic-Striped64

转载自 Jdk1.8 JUC源码增量解析(1)-atomic-Striped64功能简介&#xff1a;Striped64是jdk1.8提供的用于支持如Long累加器&#xff0c;Double累加器这样机制的基础类。Striped64的设计核心思路就是通过内部的分散计算来避免竞争(比如多线程CAS操作时的竞争)。Striped64内部包含…

java内部类选择题_java内部类详解(附相关面试题)

说起内部类这个词&#xff0c;想必很多人都不陌生&#xff0c;但是又会觉得不熟悉。原因是平时编写代码时可能用到的场景不多&#xff0c;用得最多的是在有事件监听的情况下&#xff0c;并且即使用到也很少去总结内部类的用法。今天我们就来一探究竟。一.内部类基础在Java中&am…

开源,新的平台之战

近日&#xff0c;OpenDaylight项目的执行总监Neela Jacques在文章《开源的转变&#xff1a;一种新的平台战争》 中提到&#xff1a;开源已经成为软件公司业务战略的关键&#xff0c;是一种新的平台之战。 多年来&#xff0c;开源软件似乎处于技术产业的边缘。而如今&#xff0c…