您的位置:首页 > 理论基础 > 数据结构算法

java数据结构之带头结点的单链表

2015-05-22 20:18 567 查看
  带头结点的单链表插入的数据和打印的数据是相反的,例如从左到右插入1-10的数据,打印显示的是10-1.下面是单链表的存储结构的代码如下: 

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: