JAVA数据结构之单链表操作简单实现

和C的语法明显不一样哈。

C的链表里,绝对离不开的是*---指针。

其实JAVA和代码思路和C的完全一样呀,

只是JAVA用数据类型的传值(简单类型,结构)和传引用(数组,对象)来区别指针和非指针。

栈数据和堆数据,其实一样。。。只是看自动化程度的高低以及执行效率的高低互有分别。。

下面代码实现的是前插式,也就是插入和删除节点都是从第一个开始的。

我加了输出语句,显示更明显。

JAVA数据结构之单链表操作简单实现
 1 class Link
 2 {
 3     public int iData;
 4     public double dData;
 5     public Link next;
 6     
 7     public Link(int id, double dd)
 8     {
 9         iData = id;
10         dData = dd;
11     }
12     public void displayLink()
13     {
14         System.out.print("{" + iData + ", " + dData + "}");
15     }
16 }
17 
18 class LinkList
19 {
20     private Link first;
21     
22     public LinkList()
23     {
24         first = null;
25     }
26     public boolean isEmpty()
27     {
28         return (first == null);
29     }
30     public void insertFirst(int id, double dd)
31     {
32         Link newLink = new Link(id, dd);
33         newLink.next = first;
34         first = newLink;
35         System.out.println("Insert " + id + ", " + dd + " to LinkList!");
36     }
37     public Link deleteFirst()
38     {
39         Link temp = first;
40         first = first.next;
41         return temp;
42     }
43     public void displayList()
44     {
45         System.out.println("List (first-->last): ");
46         Link current = first;
47         while(current != null)
48         {
49             current.displayLink();
50             current = current.next;
51         }
52         System.out.println(" ");
53     }
54 }
55 
56 public class LinkListApp {
57 
58     /**
59      * @param args
60      */
61     public static void main(String[] args) {
62         LinkList theList = new LinkList();
63         
64         theList.insertFirst(22, 2.99);
65         theList.displayList();
66         theList.insertFirst(44, 4.99);
67         theList.displayList();
68         theList.insertFirst(66, 6.99);
69         theList.displayList();
70         theList.insertFirst(88, 8.99);
71         theList.displayList();
72         theList.insertFirst(99, 9.99);
73         
74         theList.displayList();
75         
76         while(!theList.isEmpty())
77         {
78             Link aLink = theList.deleteFirst();
79             System.out.println("Deleted!");
80             aLink.displayLink();
81             System.out.println(" ");
82             theList.displayList();
83             
84         }
85 
86     }
87 
88 }
JAVA数据结构之单链表操作简单实现

Insert 22, 2.99 to LinkList!
List (first-->last):
{22, 2.99}
Insert 44, 4.99 to LinkList!
List (first-->last):
{44, 4.99}{22, 2.99}
Insert 66, 6.99 to LinkList!
List (first-->last):
{66, 6.99}{44, 4.99}{22, 2.99}
Insert 88, 8.99 to LinkList!
List (first-->last):
{88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
Insert 99, 9.99 to LinkList!
List (first-->last):
{99, 9.99}{88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
Deleted!
{99, 9.99}
List (first-->last):
{88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99}
Deleted!
{88, 8.99}
List (first-->last):
{66, 6.99}{44, 4.99}{22, 2.99}
Deleted!
{66, 6.99}
List (first-->last):
{44, 4.99}{22, 2.99}
Deleted!
{44, 4.99}
List (first-->last):
{22, 2.99}
Deleted!
{22, 2.99}
List (first-->last): 

上一篇:如何在 Python 中搜索和替换文件中的文本?(1)


下一篇:入门级别的 JS 基本概念详细介绍