線性表的鏈式儲存結構之單鏈表結點類的實現_Java

來源:互聯網
上載者:User

在前幾次的博文中我提到了——線性表的順序儲存結構之順序表類的實現,這幾次我們準備實現線性表的鏈式儲存結構,不過之前需要一些預備知識,那就是本文了。

由於單鏈表有一個個結點連結而成,以下定義單鏈表及地點類。

在C/C++語言中,採用指標類型儲存地址來實現鏈式儲存結構。Java語言不支援指標類型,提供引用方式儲存包括地址在內的結構化資訊。引用是比指標更健壯、更安全的連結方式,它不僅實現了指標的所有功能,而且避免了因指標使用不當產生的不安全性。因此,採用Java語言的參考型別可以很好地實現鏈式儲存結構。

單鏈表結點類Node聲明如下:

package dataStructure.linearList;public class Node<E>{    public E Data;    public Node<E> Next;    public Node(E data,Node<E> next)    {        this.Data = data;        this.Next = next;    }    public Node(E Data)    {        this(Data,null);    }    public Node()    {        this(null,null);    }}

Node<E>類有兩個成員變數,data表示結點的資料域,儲存資料元素本身,資料類型是E;next表示結點的地址域,儲存後繼結點的地址。Node類中成員變數next的資料類型是Node類自己,Node類是“自引用的類”。所謂自引用的類(self-referential calss),是指一個類聲明包含一個引用當前類的對象的成員變數。

建立並連結結點的語句如下:

import dataStructure.linearList.Node;public class MainForm{    public static void main(String args[])    {    Node<String> CC =new Node<String>("C");    Node<String> BB =new Node<String>("B",CC);    Node<String> AA =new Node<String>("A",BB);            System.out.println(AA.Data);        System.out.println(AA.Next.Data);        System.out.println(AA.Next.Next.Data);    }}

單鏈表的頭指標head也是一個結點引用,聲明如下:

Node<E> head = null;

當head ==null時,表示空單鏈表。

在下一篇博文中我將和大家討論有關單鏈表類的實現問題。敬請期待。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.