java数据结构之带头结点的单链表
2015-05-22 20:18
567 查看
带头结点的单链表插入的数据和打印的数据是相反的,例如从左到右插入1-10的数据,打印显示的是10-1.下面是单链表的存储结构的代码如下:
List:first----------------last
10 9 8 7 6 5 4 3 2 1
在链表的1位置插入值为12的节点
List:first----------------last
12 10 9 8 7 6 5 4 3 2 1
在链表的8位置插入值为20的节点
List:first----------------last
12 10 9 8 7 6 5 20 4 3 2 1
删除第一个位置的节点,其值为12
List:first----------------last
10 9 8 7 6 5 20 4 3 2 1
删除第3个位置的节点,其值为8
List:first----------------last
10 9 7 6 5 20 4 3 2 1
链表的长度为10
class LNode { // 单链表的存储结构 int value; LNode next; public LNode() { } public LNode(int value) { this.value = value; this.next = null; } public LNode(int value, LNode next) { this.value = value; this.next = next; } }对单链表的基本操作:
public class HeadList { public LNode head = new LNode();// 创建空的头节点 public void insertHead(int value) { // 插入节点,从头开始 LNode link = new LNode(value); // 创建一个带value值的节点 link.next = head.next; head.next = link; } public void insertList(int i, int value) { System.out.println("在链表的" + i + "位置插入值为" + value + "的节点"); LNode node = head; int j = 0; while (node.next != null && j < i - 1) {// 定位到第i位置前面的那个节点node node = node.next; j++; } LNode newnode = new LNode(value);// 创建值为value的节点 newnode.next = node.next; node.next = newnode; } public int deleteFirstNode() { // 删除第一个节点 if (head.next == null) return 0; LNode link = head.next; head = head.next; return link.value; } public int deleteKeyNode(int i) { // 删除第i个元素,并返回删除节点的值 LNode node = head; if (head.next == null) return 0; int j = 0; while (node.next != null && j < i - 1)// 寻找第i个节点,并命node指向其前驱 { node = node.next; j++; } if (node.next == null || j > i - 1) return 0; LNode q = node.next; // 找到第i个节点q,并删除q node.next = q.next; int value = q.value; return value; } public void display() { if (head.next == null) { System.out.println("this List is null"); return; } else { System.out.println("List:first----------------last"); LNode node = head.next; while (node != null) { System.out.print(node.value + " "); node = node.next; } } System.out.println(); } public boolean isEmpty() {// 判断链表是否为空 return (head.next == null); } public int length() { if (head.next == null) return 0; int i = 0; LNode node = head.next; while (node != null) { i++; node = node.next; } return i; } public void clear() { // 清空链表 if (head.next == null) { head = null; return; } while (head.next != null) head = head.next; head = null; } public static void main(String[] args) { HeadList list = new HeadList(); // 创建链表1-10 for (int i = 1; i <= 10; i++) { list.insertHead(i); } list.display();// 打印链表 list.insertList(1, 12); list.display(); list.insertList(8, 20); list.display(); int value = list.deleteFirstNode(); System.out.println("删除第一个位置的节点,其值为" + value); list.display(); int value2 = list.deleteKeyNode(3); System.out.println("删除第3个位置的节点,其值为" + value2); list.display(); int length = list.length(); System.out.println("链表的长度为" + length); list.clear();// 清空链表 } }结果显示为:
List:first----------------last
10 9 8 7 6 5 4 3 2 1
在链表的1位置插入值为12的节点
List:first----------------last
12 10 9 8 7 6 5 4 3 2 1
在链表的8位置插入值为20的节点
List:first----------------last
12 10 9 8 7 6 5 20 4 3 2 1
删除第一个位置的节点,其值为12
List:first----------------last
10 9 8 7 6 5 20 4 3 2 1
删除第3个位置的节点,其值为8
List:first----------------last
10 9 7 6 5 20 4 3 2 1
链表的长度为10
相关文章推荐
- 数据结构——带头结点链表和不带头结点链表操作比较
- 数据结构模版----单链表SimpleLinkList[不带头结点](C语言实现)
- 数据结构——带头结点的单链表
- 数据结构基础3_带头结点的单链表
- 数据结构(5)线性表之链表C++实现带头结点的单链表合并
- 数据结构实验3-带头结点的单链表
- 数据结构(二):Java实现:链表实现增删查操作(具有头结点)
- 数据结构:不带头结点的单链表
- 数据结构与算法(4)---Java语言实现:队列的单链表定义
- 数据结构_单链表(带头节点)
- (学习java)带有头结点的单链表结构
- 【数据结构作业二】写出单链表结点的结构体类型定义及查找、插入、删除算法,并以单链表作存储结构,实现有序表的合并
- 数据结构与算法(Java描述)-5、循环链表仿真链表以及循环链表应用
- 【数据结构与算法】链表1:单向链表(Java实现)
- 数据结构之--链表(Java代码)
- 数据结构——建立单向链表(java实现)
- 数据结构——双端链表(java实现)
- 数据结构——用(单端)链表实现栈(java实现)
- 数据结构——使用双端链表实现队列(java实现)
- 数据结构及算法——带头结点的链式表操作集(C语言)