Java實現(xiàn)線性表的鏈式存儲
本文實例為大家分享了Java實現(xiàn)線性表的鏈式存儲,供大家參考,具體內(nèi)容如下
鏈表:一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。
package algorithm.datastructure.linklist;
import java.util.NoSuchElementException;
/*
* 鏈表
* 物理存儲上非連續(xù)的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)
*
* */
public class LinkedList {
private Node head;//頭節(jié)點
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;
}
//根據(jù)下標獲取元素
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));
}
//根據(jù)索引刪除元素
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;//記錄下一個結(jié)點
p=null;//刪除當前結(jié)點
p=next;//指向下一個結(jié)點
}
size=0;
head=null;
}
//遍歷鏈表
public void traverseList(){
for (Node p=head;p!=null;){
System.out.println(p.data);
p=p.next;
}
}
//返回鏈表元素個數(shù)
public int size(){
return size;
}
//尾部添加結(jié)點
private void linkLast(int element){
Node cur =null,p;
if (size==0){//沒有結(jié)點時
head=new Node(element,null);
size++;
return;
}
for (p=head;p!=null;){//有結(jié)點時候
cur=p;
p=cur.next;
}
cur.next= new Node(element,null);//尾部添加元素
size++;
}
//在某結(jié)點之前插入結(jié)點
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為結(jié)點時候
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;
}
}
}
}
//刪除結(jié)點
private Integer unlink(Node node){
Integer deleteNodeData=null;
Node p=null;
deleteNodeData=node.data;
if (node.data==head.data){//該節(jié)點為頭結(jié)點
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結(jié)點
p=q.next;
}
q.next=p.next;
p=null;//刪除p
size--;
}
return deleteNodeData;
}
//數(shù)組下標是否越界
private Boolean isElementIndex(int index){
return index>=0&&index<size;
}
//插入位置是否越界
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簡單實現(xiàn)線性表的鏈式存儲,更多功能可參考Java集合中的LinkedList實現(xiàn)。
相關文章
Java中的synchronized和ReentrantLock的區(qū)別詳細解讀
這篇文章主要介紹了Java中的synchronized和ReentrantLock的區(qū)別詳細解讀,synchronized是Java內(nèi)建的同步機制,所以也有人稱其為 IntrinsicLocking,它提供了互斥的語義和可見性,當一個線程已經(jīng)獲取當前鎖時,其他試圖獲取的線程只能等待或者阻塞在那里,需要的朋友可以參考下2024-01-01
30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析
這篇文章主要為大家介紹了30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
Java基礎知識精通注釋與數(shù)據(jù)類型及常量與變量
本文給大家介紹了Java的注釋與數(shù)據(jù)類型和常量變量,這些都是最基礎的知識,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
Spring Boot高級教程之使用Redis實現(xiàn)session共享
這篇文章主要為大家詳細介紹了Spring Boot高級教程之使用Redis實現(xiàn)session共享,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
基于MybatisPlus插件TenantLineInnerInterceptor實現(xiàn)多租戶功能
這篇文章主要介紹了基于MybatisPlus插件TenantLineInnerInterceptor實現(xiàn)多租戶功能,需要的朋友可以參考下2021-11-11
javaweb用戶注銷后點擊瀏覽器返回刷新頁面重復登錄問題的解決方法
這篇文章主要為大家詳細介紹了javaweb用戶注銷后點擊瀏覽器返回刷新頁面重復登錄問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

