本文目錄一覽:
- 1、用Java語言實現單向鏈表
- 2、Java鏈表問題。
- 3、Java語言沒有指針,怎樣實現鏈表?
- 4、在Java中如何實現雙向鏈表?
- 5、. java怎麼創建鏈表
- 6、java里的鏈表指的是什麼?為什麼需要鏈表?
用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鏈表問題。
java中的鏈表是指linkedList
看名字就能知道,它實現了List介面。
這就說明了,java中的鏈表首先是一個List,其次才是一個鏈表。
因此linkedList既有一個List具有的基本方法(能通過下標獲取元素的get(index)方法),也有作為鏈表的方法(next()取得下一個元素)
綜上所述,如果你想完全把這個對象作為鏈表使用,那就不用get下標方法就可以了。
望採納
Java語言沒有指針,怎樣實現鏈表?
Java語言中的對象引用實際上是一個指針(這裡的指針均為概念上的意義,而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
private static class EntryE {
E element; // 當前存儲元素
EntryE next; // 下一個元素節點
EntryE previous; // 上一個元素節點
Entry(E element, EntryE next, EntryE previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便於在鏈表尾部增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。
鏈表的數據結構我們可以用類List來實現鏈表結構,用變數Head、Tail、Length、Pointer來實現表頭。
存儲當前結點的指針時有一定的技巧,Pointer並非存儲指向當前結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是第一個結點,因為當刪除當前結點後仍需保證剩下的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。如何得到當前結點呢?我們定義了一個方法cursor(),返回值是指向當前結點的指針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作我們可以對鏈表進行各種操作。
例如reset()方法使第一個結點成為當前結點。insert(Object d)方法在當前結點前插入一個結點,並使其成為當前結點。remove()方法刪除當前結點同時返回其內容,並使其後繼結點成為當前結點,如果刪除的是最後一個結點,則第一個結點變為當前結點。
在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里的鏈表指的是什麼?為什麼需要鏈表?
鏈表的確是一種數據結構.而數據結構就是一種存放數據的方式.
鏈表就是和鐵鏈相似的.一個接著一個.一個扣著一個.
比如:
1,後面接著是2,然後是3,是連續的.1,2,3,就是這個鏈表的節點,就是數據存放的地方
再通俗點.
大學的校園生活:
班級是這樣的.1年1班,1年2班,....1年10班.
班級就是節點,而班級里的學生,就是數據.他們是連續存儲的.但是內存分分配不是連續的.
有時間看下,<數據結構>書上寫的很好.我就說到這吧.
原創文章,作者:CGHP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/137292.html