孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

由于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 // //////////////////////////////////////////////////////////////

 

posted on 2012-05-05 20:36  孤独的猫  阅读(974)  评论(0编辑  收藏  举报