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

简介:

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

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

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

只是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 }
复制代码

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): 

目录
相关文章
|
2月前
|
存储
【数据结构入门指南】单链表
【数据结构入门指南】单链表
43 0
|
2月前
|
存储 算法 索引
数据结构与算法:单链表
朋友们大家好,本节来到数据结构与算法的新内容:单链表 在上篇文章中,我们知道顺序表通常需要预分配一个固定大小的内存空间, 通常以二倍的大小进行增容,可能会造成空间的浪费,本篇文章我们介绍的链表可以解决这个问题
|
2月前
|
存储 人工智能 算法
【数据结构-算法】:数据结构和算法的一些个人总结(Java实现)
【数据结构-算法】:数据结构和算法的一些个人总结(Java实现)
57 0
|
2月前
|
存储 算法 Java
Java数据结构与算法-java数据结构与算法(二)
Java数据结构与算法-java数据结构与算法
110 1
|
10天前
|
存储 安全 Java
Java程序员必须掌握的数据结构:HashMap
HashMap底层原理实现是每个Java Boy必须掌握的基本技能,HashMap也是业务开发每天都需要遇到的好伙伴。如此基础且核心的底层数据结构,JDK也给其赋予了线程安全的功能,我们来看看~
24 1
Java程序员必须掌握的数据结构:HashMap
|
10天前
|
存储 安全 Java
Java并发编程中的高效数据结构:ConcurrentHashMap解析
【4月更文挑战第25天】在多线程环境下,高效的数据访问和管理是至关重要的。Java提供了多种并发集合来处理这种情境,其中ConcurrentHashMap是最广泛使用的一个。本文将深入分析ConcurrentHashMap的内部工作原理、性能特点以及它如何在保证线程安全的同时提供高并发性,最后将展示其在实际开发中的应用示例。
|
16天前
|
存储 供应链 Java
《Java 简易速速上手小册》第3章:Java 数据结构(2024 最新版)
《Java 简易速速上手小册》第3章:Java 数据结构(2024 最新版)
8 1
|
18天前
|
存储 算法
单链表——“数据结构与算法”
单链表——“数据结构与算法”
|
23天前
|
Java API
编码的奇迹:Java 21引入有序集合,数据结构再进化
编码的奇迹:Java 21引入有序集合,数据结构再进化
16 0
|
1月前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解