java链表(java链表实现)

本篇文章给大家谈谈java链表,以及java链表实现对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

. 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中如何实现双向链表?

双向链表:就是有双向指针,即双向的链域。\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单链表中结点类用private修饰,怎么用在链表类里?

在Java单链表中,节点类通常包含两个属性:一个存储数尘和据的变量和一个含差指向下一个节点的变量。为了保证数据的封装性,通常会将这两个属性都用private修饰,然后提供对应的getter和setter方法来访问和修改这些属性。

下面是一个简单的Java单链表节点类示例:

public class ListNode {

private int val;

private ListNode next;

public ListNode(int val) {

this.val = val;

}

public int getVal() {

return val;

}

public void setVal(int val) {

this.val = val;

}

public ListNode getNext() {

return next;

}

public void setNext(ListNode next) {

this.next = next;

}

}

在链表类中,我们需要创建一个头节点来表示整个链表的起始位置。可以将链表类的定义如下:

public class LinkedList {

private ListNode head;

public LinkedList() {

this.head = null;

}

// 添加节点到链表尾部

public void addNode(int val) {

ListNode newNode = new ListNode(val);

if (head == null) {

head = newNode;

} else {

ListNode cur = head;

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

cur = cur.getNext();

}

cur.setNext(newNode);

}

}

// 遍历链表并输出节点值

public void traverse() {

ListNode cur = head;

while (cur != null) {

System.out.print(cur.getVal() + " ");

cur = cur.getNext();

}

System.out.println();

}

}

在链表类中,我们将head属性也用private修饰,并提供对应的getter和setter方法来访问和修改head属性。在addNode方法中,我们首先判断链表是否为空,如果为空,直接将新节点作为头节点;否则,遍历链表找到尾节点并将新节点接在其后面。在traverse方法中,我们遍历整个链表并输出每个节点谈兄皮的值。

使用时,可以创建一个新的LinkedList对象,然后调用其addNode方法添加节点,最后调用traverse方法遍历链表并输出每个节点的值。例如:

public static void main(String[] args) {

LinkedList list = new LinkedList();

list.addNode(1);

list.addNode(2);

list.addNode(3);

list.traverse();

}

这段代码会输出:1 2 3。

[img]

关于java链表和java链表实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

标签列表