java實現鏈表,java實現鏈表和c實現鏈表哪個簡單一點

本文目錄一覽:

在Java中如何實現雙向鏈表

雙向鏈表:就是有雙向指針,即雙向的鏈域。

鏈結點的結構:

┌────┬────┬────────┐

│ data │ next │ previous │

└────┴────┴────────┘

雙向鏈表不必是雙端鏈表(持有對最後一個鏈結點的引用),雙端鏈表插入時是雙向的。

有兩條鏈:一條從頭到尾,一條從尾到頭,刪除遍歷時也是雙向的。

/**

* 雙向鏈表

*/

public class DoublyLinkedListt {

private Linkt head; //首結點

private Linkt rear; //尾部指針

public DoublyLinkedList() { }

public T peekHead() {

if (head != null) {

return head.data;

}

return null;

}

public boolean isEmpty() {

return head == null;

}

public void insertFirst(T data) {// 插入 到 鏈頭

Linkt newLink = new Linkt(data);

if (isEmpty()) {//為空時,第1次插入的新結點為尾結點

rear = newLink;

} else {

head.previous = newLink; //舊頭結點的上結點等於新結點

}

newLink.next = head; //新結點的下結點舊頭結點

head = newLink; //賦值後,頭結點的下結點是舊頭結點,上結點null

}

public void insertLast(T data) {//在鏈尾 插入

Linkt newLink = new Linkt(data);

if (isEmpty()) {

head = newLink;

} else {

rear.next = newLink;

}

newLink.previous = rear;

rear = newLink; //賦值後,尾結點的上結點是舊尾結點,下結點null

}

public T deleteHead() {//刪除 鏈頭

if (isEmpty()) return null;

Linkt temp = head;

head = head.next; //變更首結點,為下一結點

if (head != null) {

head.previous = null;

} else {

rear = null;

}

return temp.data;

}

public T deleteRear() {//刪除 鏈尾

if (isEmpty()) return null;

Linkt temp = rear;

rear = rear.previous; //變更尾結點,為上一結點

if (rear != null) {

rear.next = null;

} else {

head = null;

}

return temp.data;

}

public T find(T t) {//從頭到尾find

if (isEmpty()) {

return null;

}

Linkt find = head;

while (find != null) {

if (!find.data.equals(t)) {

find = find.next;

} else {

break;

}

}

if (find == null) {

return null;

}

return find.data;

}

public T delete(T t) {

if (isEmpty()) {

return null;

}

Linkt current = head;

while (!current.data.equals(t)) {

current = current.next;

if (current == null) {

return null;

}

}

if (current == head) {

head = head.next;

if (head != null) {

head.previous = null;

}

} else if (current == rear) {

rear = rear.previous;

if (rear != null) {

rear.next = null;

}

} else {

//中間的非兩端的結點,要移除current

current.next.previous = current.previous;

current.previous.next = current.next;

}

return current.data;

}

public boolean insertAfter(T key, T data) {//插入在key之後, key不存在return false

if (isEmpty()) {

return false;

}

Linkt current = head;

while (!current.data.equals(key)) {

current = current.next;

if (current == null) {

return false;

}

}

Linkt newLink = new Linkt(data);

if (current == rear) {

rear = newLink;

} else {

newLink.next = current.next;

current.next.previous = newLink;

}

current.next = newLink;

newLink.previous = current;

return true;

}

public void displayList4Head() {//從頭開始遍歷

System.out.println(“List (first–last):”);

Linkt current = head;

while (current != null) {

current.displayLink();

current = current.next;

}

}

public void displayList4Rear() {//從尾開始遍歷

System.out.println(“List (last–first):”);

Linkt current = rear;

while (current != null) {

current.displayLink();

current = current.previous;

}

}

class Linkt {//鏈結點

T data; //數據域

Linkt next; //後繼指針,結點 鏈域

Linkt previous; //前驅指針,結點 鏈域

Link(T data) {

this.data = data;

}

void displayLink() {

System.out.println(“the data is ” + data.toString());

}

}

public static void main(String[] args) {

DoublyLinkedListinteger list = new DoublyLinkedListinteger();

list.insertLast(1);

list.insertFirst(2);

list.insertLast(3);

list.insertFirst(4);

list.insertLast(5);

list.displayList4Head();

Integer deleteHead = list.deleteHead();

System.out.println(“deleteHead:” + deleteHead);

list.displayList4Head();

Integer deleteRear = list.deleteRear();

System.out.println(“deleteRear:” + deleteRear);

list.displayList4Rear();

System.out.println(“find:” + list.find(6));

System.out.println(“find:” + list.find(3));

System.out.println(“delete find:” + list.delete(6));

System.out.println(“delete find:” + list.delete(1));

list.displayList4Head();

System.out.println(“—-在指定key後插入—-“);

list.insertAfter(2, 8);

list.insertAfter(2, 9);

list.insertAfter(9, 10);

list.displayList4Head();

}

}

java實現鏈表求救

import java.util.Scanner;

public class Node {

private int data;

private Node next;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

String[] numStr = s.split(“,”);

Node head = buildList(numStr);

printList(head);

Node node1 = new Node();

node1.setData(100);

node1.setNext(null);

Node node2 = new Node();

node2.setData(100);

node2.setNext(null);

Node temp = head;

node1.setNext(temp);

head = node1;

printList(head);

temp = head;

while(temp.getNext() != null) {

temp = temp.getNext();

}

temp.setNext(node2);

printList(head);

}

//構建鏈表

public static Node buildList(String[] numStr) {

if(0 == numStr.length) {

return null;

}

Node head = new Node();

head.setData(Integer.parseInt(numStr[0]));

head.setNext(null);

Node temp = head;

for(int i = 1; i  numStr.length; i++) {

Node node = new Node();

node.setData(Integer.parseInt(numStr[i]));

node.setNext(null);

temp.setNext(node);

temp = node;

}

return head;

}

//輸出鏈表

public static void printList(Node head) {

Node temp = head;

while(temp != null) {

System.out.print(temp.getData() + “–“);

temp = temp.getNext();

}

System.out.println();

}

public int getData() {

return data;

}

public void setData(int data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

用Java語言實現單向鏈表

1.先定義一個節點類

package com.buren;

public class IntNode {

//定義一個節點類

int

info;

//定義屬性,節點中的值

IntNode next;

//定義指向下一個節點的屬性

public IntNode(int

i){ //構造一個next為空的節點

this(i,null);

}

public IntNode(int i,IntNode

n){ //構造值為i指向n的節點

info=i;

next=n;

}

}

2.再定義一個鏈表類,這是主要部分

package com.buren;

public class IntSLList {

private IntNode head,tail;

//定義指向頭結點和尾結點的指針,

//如果大家看着這個不像指針的話,那就需要對指針有更深刻的了解

public

IntSLList(){

//定義一個空節點

head=tail=null;

}

public boolean

isEmpty(){

//判斷節點是否為空

return

head==null;

//這行代碼看起來似乎很神奇,其實真的很神奇,偶是服了

}

public void addToHead(int el){

//將el插入到頭結點前

head=new

IntNode(el,head);

//將節點插入到頭結點前,作為新的投節點

if(head==tail){

//給空鏈表插入節點時

tail=head;

//頭結點和尾結點指向同一個節點

}

}

public void addToTail(int

el){

//向鏈表的尾部增加結點

if(!isEmpty()){

//判斷鏈表是否為空

tail.next=new

IntNode(el);

//新建立一個值為el的節點,將鏈表的尾結點指向新節點

tail=tail.next;

//更新尾指針的指向

}else{

head=tail=new

IntNode(el);

//如果鏈表為空,新建立一個節點,將頭尾指針同時指向這個節點

}

}

public int

deleteFromHead(){

//刪除頭結點,將節點信息返回

int

el=head.info;

//取出節點信息

if(head==tail){

//如果鏈表中只有一個節點

head=tail=null;

//刪除這一個節點

}else{

head=head.next;

//如果鏈表中不止一個節點,將頭結點的下一個節點作為頭結點

}

return

el;

//返回原頭結點的值

}

public int

deleteFromTail(){

//刪除尾結點,返回尾結點的信息

int

el=tail.info;

//取出尾結點的值

if(head==tail){

// 如果鏈表中只有一個節點

head=tail=null;

//刪除這個節點

}else{

IntNode

temp;

//定義中間變量

for(temp=head;temp.next!=tail;temp=temp.next);

//找出尾結點的前一個節點,注意最後的分號,

//這個for循環是沒有循環體的,目的在於找出尾結點的前一個節點

//在整個程序中用了很多次這樣的寫法,相當經典啊

tail=temp;

//將找出來的節點作為尾結點,刪除原來的尾結點

tail.next=null;

//將新尾結點的指向設為空

}

return

el;

//返回原尾結點的信息

}

public void

printAll(){

//打印鏈表中所有節點的信息

if(isEmpty()){

//如果鏈表為空

System.out.println(“This

list is

empty!”);

//輸出提示信息

return;

//返回到調用的地方

}

if(head==tail){

//當鏈表中只有一個節點時

System.out.println(head.info);

//輸出這個節點的信息,就是頭結點的信息

return;

}

IntNode

temp;

//定義一個中間變量

for(temp=head;temp!=null;temp=temp.next){

//遍歷整個鏈表

System.out.print(temp.info+”

“);

//輸出每個節點的信息

}

System.out.println();

//輸出一個換行,可以沒有這一行

}

public boolean isInList(int

el){

//判斷el是否存在於鏈表中

IntNode

temp;

//定義一個中間變量

for(temp=head;temp!=null

temp.info!=el;temp=temp.next);

//將el找出來,注意最後的分

return

temp!=null;

// 如果存在返回true,否則返回flase,這兩行代碼很有思想

}

public void delete(int

el){

//刪除鏈表中值為el的節點

if(head.info==el

head==tail){

//如果只有一個節點,並且節點的值為el

head=tail=null;

//刪除這個節點

}else

if(head.info==el){

// 不止一個節點,而頭結點的值就是el

head=head.next;

//刪除頭結點

}else{

IntNode

pred,temp;

//定義兩個中間變量

for(pred=head,temp=head.next;temp.info!=el

temp.next!=null;pred=pred.next,temp=temp.next);

//跟上面的類似,自己琢磨吧,也是要注意最後的分號

pred.next=temp.next;

//將temp指向的節點刪除,最好畫一個鏈表的圖,有助於理解

if(temp==tail){

//如果temp指向的節點是尾結點

tail=pred;

//將pred指向的節點設為尾結點,

}

}

}

//下面這個方法是在鏈表中值為el1的節點前面插入一個值為el2的節點,

//用類似的思想可以再寫一個在鏈表中值為el1的節點後面插入一個值為el2的節點

public boolean insertToList(int el1,int

el2){

//定義一個插入節點的方法,插入成功返回true,否則返回false

IntNode

pred,temp; //定義兩個中間變量

if(isEmpty()){

//判斷鏈表是否為空

return

false;

//如果鏈表為空就直接返回false

}

if(head.info==el1

head==tail){

//如果鏈表中只有一個節點,並且這個節點的值是el1

head=new

IntNode(el2,head);

//新建立一個節點

return

true;

}else if(head.info==el1){

IntNode t=new

IntNode(el2);

t.next=head;

head=t;

return

true;

}else{

for(pred=head,temp=head.next;temp!=null

temp.info!=el1;pred=pred.next,temp=temp.next);

if(temp!=null){

IntNode

a=new IntNode(el2);

pred.next=a;

a.next=temp;

return

true;

}else{

System.out.println(el1+”

NOT EXEISTS!”);

return

false;

}

}

}

3.下面是測試代碼

public static void main(String[] args){

IntSLList test=new

IntSLList();

//test.addToHead(7);

test.addToTail(7);

System.out.println(test.insertToList(7,5));

test.printAll();

System.out.println(test.isInList(123));

}

}

在Java中如何實現雙向鏈表?

雙向鏈表:就是有雙向指針,即雙向的鏈域。\x0d\x0a鏈結點的結構:\x0d\x0a┌────┬────┬────────┐\x0d\x0a│ data │ next │ previous │\x0d\x0a└────┴────┴────────┘\x0d\x0a雙向鏈表不必是雙端鏈表(持有對最後一個鏈結點的引用),雙端鏈表插入時是雙向的。\x0d\x0a有兩條鏈:一條從頭到尾,一條從尾到頭,刪除遍歷時也是雙向的。\x0d\x0a/**\x0d\x0a * 雙向鏈表\x0d\x0a */\x0d\x0apublic class DoublyLinkedList {\x0d\x0a private Link head; //首結點\x0d\x0a private Link rear; //尾部指針\x0d\x0a public DoublyLinkedList() { }\x0d\x0a public T peekHead() {\x0d\x0a if (head != null) {\x0d\x0a return head.data;\x0d\x0a }\x0d\x0a return null;\x0d\x0a }\x0d\x0a public boolean isEmpty() {\x0d\x0a return head == null;\x0d\x0a }\x0d\x0a public void insertFirst(T data) {// 插入 到 鏈頭\x0d\x0a Link newLink = new Link(data);\x0d\x0a if (isEmpty()) {//為空時,第1次插入的新結點為尾結點\x0d\x0a rear = newLink;\x0d\x0a } else {\x0d\x0a head.previous = newLink; //舊頭結點的上結點等於新結點\x0d\x0a }\x0d\x0a newLink.next = head; //新結點的下結點舊頭結點\x0d\x0a head = newLink; //賦值後,頭結點的下結點是舊頭結點,上結點null\x0d\x0a }\x0d\x0a public void insertLast(T data) {//在鏈尾 插入\x0d\x0a Link newLink = new Link(data);\x0d\x0a if (isEmpty()) {\x0d\x0a head = newLink;\x0d\x0a } else {\x0d\x0a rear.next = newLink;\x0d\x0a }\x0d\x0a newLink.previous = rear;\x0d\x0a rear = newLink; //賦值後,尾結點的上結點是舊尾結點,下結點null\x0d\x0a }\x0d\x0a public T deleteHead() {//刪除 鏈頭\x0d\x0a if (isEmpty()) return null;\x0d\x0a Link temp = head;\x0d\x0a head = head.next; //變更首結點,為下一結點\x0d\x0a if (head != null) {\x0d\x0a head.previous = null;\x0d\x0a } else {\x0d\x0a rear = null;\x0d\x0a }\x0d\x0a return temp.data;\x0d\x0a }\x0d\x0a public T deleteRear() {//刪除 鏈尾\x0d\x0a if (isEmpty()) return null;\x0d\x0a Link temp = rear;\x0d\x0a rear = rear.previous; //變更尾結點,為上一結點\x0d\x0a if (rear != null) {\x0d\x0a rear.next = null;\x0d\x0a } else {\x0d\x0a head = null;\x0d\x0a }\x0d\x0a return temp.data;\x0d\x0a }\x0d\x0a public T find(T t) {//從頭到尾find\x0d\x0a if (isEmpty()) {\x0d\x0a return null;\x0d\x0a }\x0d\x0a Link find = head;\x0d\x0a while (find != null) {\x0d\x0a if (!find.data.equals(t)) {\x0d\x0a find = find.next;\x0d\x0a } else {\x0d\x0a break;\x0d\x0a }\x0d\x0a }\x0d\x0a if (find == null) {\x0d\x0a return null;\x0d\x0a }\x0d\x0a return find.data;\x0d\x0a }\x0d\x0a public T delete(T t) {\x0d\x0a if (isEmpty()) {\x0d\x0a return null;\x0d\x0a }\x0d\x0a Link current = head;\x0d\x0a while (!current.data.equals(t)) {\x0d\x0a current = current.next;\x0d\x0a if (current == null) {\x0d\x0a return null;\x0d\x0a }\x0d\x0a }\x0d\x0a if (current == head) {\x0d\x0a head = head.next;\x0d\x0a if (head != null) {\x0d\x0a head.previous = null;\x0d\x0a }\x0d\x0a } else if (current == rear) {\x0d\x0a rear = rear.previous;\x0d\x0a if (rear != null) {\x0d\x0a rear.next = null;\x0d\x0a }\x0d\x0a } else {\x0d\x0a //中間的非兩端的結點,要移除current\x0d\x0a current.next.previous = current.previous;\x0d\x0a current.previous.next = current.next;\x0d\x0a }\x0d\x0a return current.data;\x0d\x0a }\x0d\x0a public boolean insertAfter(T key, T data) {//插入在key之後, key不存在return false\x0d\x0a if (isEmpty()) {\x0d\x0a return false;\x0d\x0a }\x0d\x0a Link current = head;\x0d\x0a while (!current.data.equals(key)) {\x0d\x0a current = current.next;\x0d\x0a if (current == null) {\x0d\x0a return false;\x0d\x0a }\x0d\x0a }\x0d\x0a Link newLink = new Link(data);\x0d\x0a if (current == rear) {\x0d\x0a rear = newLink;\x0d\x0a } else {\x0d\x0a newLink.next = current.next;\x0d\x0a current.next.previous = newLink;\x0d\x0a }\x0d\x0a current.next = newLink;\x0d\x0a newLink.previous = current;\x0d\x0a return true;\x0d\x0a }\x0d\x0a public void displayList4Head() {//從頭開始遍歷\x0d\x0a System.out.println(“List (first–last):”);\x0d\x0a Link current = head;\x0d\x0a while (current != null) {\x0d\x0a current.displayLink();\x0d\x0a current = current.next;\x0d\x0a }\x0d\x0a }\x0d\x0a public void displayList4Rear() {//從尾開始遍歷\x0d\x0a System.out.println(“List (last–first):”);\x0d\x0a Link current = rear;\x0d\x0a while (current != null) {\x0d\x0a current.displayLink();\x0d\x0a current = current.previous;\x0d\x0a }\x0d\x0a }\x0d\x0a\x0d\x0a class Link {//鏈結點\x0d\x0a T data; //數據域\x0d\x0a Link next; //後繼指針,結點 鏈域\x0d\x0a Link previous; //前驅指針,結點 鏈域\x0d\x0a Link(T data) {\x0d\x0a this.data = data;\x0d\x0a }\x0d\x0a void displayLink() {\x0d\x0a System.out.println(“the data is ” + data.toString());\x0d\x0a }\x0d\x0a }\x0d\x0a public static void main(String[] args) {\x0d\x0a DoublyLinkedList list = new DoublyLinkedList();\x0d\x0a list.insertLast(1);\x0d\x0a list.insertFirst(2);\x0d\x0a list.insertLast(3);\x0d\x0a list.insertFirst(4);\x0d\x0a list.insertLast(5);\x0d\x0a list.displayList4Head();\x0d\x0a Integer deleteHead = list.deleteHead();\x0d\x0a System.out.println(“deleteHead:” + deleteHead);\x0d\x0a list.displayList4Head();\x0d\x0a Integer deleteRear = list.deleteRear();\x0d\x0a System.out.println(“deleteRear:” + deleteRear);\x0d\x0a list.displayList4Rear();\x0d\x0a System.out.println(“find:” + list.find(6));\x0d\x0a System.out.println(“find:” + list.find(3));\x0d\x0a System.out.println(“delete find:” + list.delete(6));\x0d\x0a System.out.println(“delete find:” + list.delete(1));\x0d\x0a list.displayList4Head();\x0d\x0a System.out.println(“—-在指定key後插入—-“);\x0d\x0a list.insertAfter(2, 8);\x0d\x0a list.insertAfter(2, 9);\x0d\x0a list.insertAfter(9, 10);\x0d\x0a list.displayList4Head();\x0d\x0a }\x0d\x0a}

. java怎麼創建鏈表

java中創建鏈表的例子:

package zx;

class Link{

private Node root;

class Node{

private String name;

private Node Next;

public Node(String name){

this.name = name;

}

public String getName(){

return this.name;

}

public void addNode(Node newNode){

if(this.Next==null){

this.Next = newNode;

}else{

this.Next.addNode(newNode);

}

}

public void printNode(){

System.out.print(this.name + “–“);

if(this.Next!=null){

this.Next.printNode();

}

}

};

public void add(String name){

Node newNode = new Node(name);

if(this.root==null){

this.root = newNode;

}else{

this.root.addNode(newNode);

}

}

public void print(){

if(this.root!=null){

this.root.printNode();

}

}

};

public class LinkDemo {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Link link = new Link();

link.add(“根節點”);

link.add(“第一節點”);

link.add(“第二節點”);

link.add(“第三節點”);

link.add(“第四節點”);

link.print();

System.out.println(“null”);

}

}

java用node還是自己實現鏈表

用node。

javaListNode鏈表就是用Java自定義實現的鏈表結構。

鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點組成,結點可以在運行時動態生成。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/242153.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:47
下一篇 2024-12-12 12:47

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29

發表回復

登錄後才能評論