本文目录一览:
java用node还是自己实现链表
用node。
javaListNode链表就是用Java自定义实现的链表结构。
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点组成,结点可以在运行时动态生成。
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中如何实现双向链表
双向链表:就是有双向指针,即双向的链域。
链结点的结构:
┌────┬────┬────────┐
│ 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怎么创建链表
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语言实现单向链表
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));
}
}