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

  带头结点的单链表插入的数据和打印的数据是相反的,例如从左到右插入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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值