• JAVA数据结构链表


    由于JAVA中没有结构体,所以必须用类来模拟,如下所示:

     1 package LinkedList;
     2 
     3 //linkList.java
     4 //demonstrates linked list
     5 //to run this program: C>java LinkListApp
     6 ////////////////////////////////////////////////////////////////
     7 class Link {
     8     public int iData; // data item
     9     public double dData; // data item
    10     public Link next; // next link in list
    11     // -------------------------------------------------------------
    12 
    13     public Link(int id, double dd) // constructor
    14     {
    15         iData = id; // initialize data
    16         dData = dd; // ('next' is automatically
    17     } // set to null)
    18     // -------------------------------------------------------------
    19 
    20     public void displayLink() // display ourself
    21     {
    22         System.out.print("{" + iData + ", " + dData + "} ");
    23     }
    24 } // end class Link
    25 // //////////////////////////////////////////////////////////////
    26 
    27 class LinkList {
    28     private Link first; // ref to first link on list
    29 
    30     // -------------------------------------------------------------
    31     public LinkList() // constructor
    32     {
    33         first = null; // no links on list yet
    34     }
    35 
    36     // -------------------------------------------------------------
    37     public boolean isEmpty() // true if list is empty
    38     {
    39         return (first == null);
    40     }
    41 
    42     // -------------------------------------------------------------
    43     // insert at start of list
    44     public void insertFirst(int id, double dd) { // make new link
    45         Link newLink = new Link(id, dd);
    46         newLink.next = first; // newLink --> old first
    47         first = newLink; // first --> newLink
    48     }
    49 
    50     // -------------------------------------------------------------
    51     public Link deleteFirst() // delete first item
    52     { // (assumes list not empty)
    53         Link temp = first; // save reference to link
    54         first = first.next; // delete it: first-->old next
    55         return temp; // return deleted link
    56     }
    57 
    58     // -------------------------------------------------------------
    59     public void displayList() {
    60         System.out.print("List (first-->last): ");
    61         Link current = first; // start at beginning of list
    62         while (current != null) // until end of list,
    63         {
    64             current.displayLink(); // print data
    65             current = current.next; // move to next link
    66         }
    67         System.out.println("");
    68     }
    69     // -------------------------------------------------------------
    70 } // end class LinkList
    71 // //////////////////////////////////////////////////////////////
    72 
    73 public class LinkListApp {
    74     public static void main(String[] args) {
    75         LinkList theList = new LinkList(); // make new list
    76 
    77         theList.insertFirst(22, 2.99); // insert four items
    78         theList.insertFirst(44, 4.99);
    79         theList.insertFirst(66, 6.99);
    80         theList.insertFirst(88, 8.99);
    81 
    82         theList.displayList(); // display list
    83 
    84         while (!theList.isEmpty()) // until it's empty,
    85         {
    86             Link aLink = theList.deleteFirst(); // delete link
    87             System.out.print("Deleted "); // display it
    88             aLink.displayLink();
    89             System.out.println("");
    90         }
    91         theList.displayList(); // display list
    92     } // end main()
    93 } // end class LinkListApp
    94 // //////////////////////////////////////////////////////////////
  • 相关阅读:
    李超线段树 [Heoi2013]Segment
    [置顶] 九月半集训总结
    [置顶] 我想学学
    图论+前缀和 任(duty)
    模拟 飞(fly)
    入坑 可持久化线段树——主席树
    一次爆炸的联考
    HASH+平衡树 [JSOI2008]火星人prefix
    乱搞+STL平衡树 序列
    数学+图论 建造游乐场
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2485072.html
Copyright © 2020-2023  润新知