Java数据结构——Java语言下的链表

1. Java语言下的链表

1.1 内部类定义结点

	class Node {
		int data;
		Node next;
		
		Node(int elem) {
			data = elem;
			next = null;
		}
	}

1.2 初始链表,只有头结点

class LinkList {
	...
	Node head;
	LinkList() {
	head = new Node(0);
	}
}

1.3 清空链表

	void reset() {
		head.next = null;
	}

1.4 返回元素的位置(在第几个结点)

	public int locate(int elem) {
		int location = -1;
		Node tempNode = head.next;
		int TempLocation = 0;
		while (tempNode != null) {
			if (tempNode.data == elem) {
				location = TempLocation;
				break;
			}
			tempNode = tempNode.next;
			TempLocation++;
		} 
		return location;
	}

1.5 在某个结点处插入元素

	public boolean Insert_L(int location, int elem) {
		Node tempNode = head;
		Node NewNode;
		for(int i=0; i<location; i++) {
			if(tempNode.next==null) {
				System.out.println("ERROR");
				return false;
			}
			tempNode = tempNode.next;
		}
		NewNode = new Node(location);
		NewNode.next = tempNode.next;
		tempNode.next = NewNode;
		return true;
	}

1.5 删除某个结点的元素

在这里插入图片描述

//数据结构原型
Status Delete_L(LinkList &L, int i, ElemType &e) {
	LinkList p; int j;
	p = L; j = 0;
	while(p->next&&j<i-1) {
		p = p->next;
		++j;
	}
	if(!(p->next)||j>i-1) return ERROR;
	LinkList q;
	q = p->next; p->next = q->next;
	e = q->data;
	free(q);
	return OK;
}

//	public boolean delete_L(int location) {
//		if(head.next==null) {
//			System.out.println("Empty");
//			return false;
//		}
//		Node tempNode = head;
//		for(int i=0; i<location-1; i++) {
//			if(tempNode.next==null) {
//				System.out.print("ERROR");
//				return false;
//			}
//			tempNode = tempNode.next;
//		}
//		tempNode.next = tempNode.next.next;
//		return true;
//	}
	public boolean Delete_L(int location) {
		if(head.next==null) {
			System.out.println("Empty");
			return false;
		}
		Node p = head; //用p来找前驱
		int j = 0;
//		while(p.next!=null&&j<location-1) {
//			p = p.next;
//			++j;
//		}
		for(int i=0; i<location-1; i++) {
			if(p.next==null) {
				System.out.print("ERROR");
				return false;
			}
			p = p.next;
		}
		if(p.next==null||j>location-1) {
			System.out.println("ERROR");
			return false;
		}
		Node q;
		q = p.next; p.next = q.next;
		return true;
	}

1.6 便历链表

	public String toString() {
		String resulting = null;
		if(head.next==null) {
			return "Empty";
		}
		Node NewNode = head.next;
		while(NewNode!=null) {
			resulting += NewNode.data+",";
			NewNode = NewNode.next;
		}
		return resulting;
	}

2. Java源码

2.1 LinkList类

package datastructure.linearlist;

/**
 * 链表
 * 
 * @author NoBug
 * @time 2022/3/9
 *
 */
public class LinkList {

	/**
	 * 内部类作为链表结点
	 * 
	 * @author NoBug
	 *
	 */
	class Node {

		/** 数据域 */
		int data;

		/** 链域 */
		Node next;

		Node(int elem) {
			data = elem;
			next = null;
		}
	}

	/** 头结点 */
	Node head;

	/**
	 * 构造函数,初始头结点
	 */
	public LinkList() {
		head = new Node(0);
	}

	/**
	 * 链表清空
	 */
	void reset() {
		head.next = null;
	}

	/**
	 * 得到结点在第几个结点
	 * 
	 * @param elem 元素
	 * @return
	 */
	public int getLocation(int elem) {
		int location = -1;
		Node tempNode = head.next;
		int tempLocation = 0;
		int t = 0;

		while (tempNode != null) {
			if (tempNode.data == elem) {
				location = tempLocation;
				t++;
				break;
			}
			tempNode = tempNode.next;
			tempLocation++;
		}

		if (t == 0) {
			return -1;
		}

		return location + 1;
	}

	/**
	 * 链表的插入
	 * 
	 * @param location
	 * @param elem
	 * @return
	 */
	public boolean insert(int location, int elem) {
		Node tempNode = head;
		Node NewNode;
		for (int i = 1; i < location; i++) {
			if (tempNode.next == null) {
				System.out.println("ERROR");
				return false;
			}
			tempNode = tempNode.next;
		}
		NewNode = new Node(location);
		NewNode.next = tempNode.next;
		tempNode.next = NewNode;
		return true;
	}

	/**
	 * 链表的删除
	 * 
	 * @param location
	 * @return
	 */
	public boolean delete(int location) {
		if (head.next == null) {
			System.out.println("ERROR");
			return false;
		}
		Node tempNode = head;
		for (int i = 0; i < location - 1; i++) {
			if (tempNode.next.next == null) {
				System.out.print("ERROR");
				return false;
			}
			tempNode = tempNode.next;
		}
		tempNode.next = tempNode.next.next;
		return true;
	}

	public String toString() {
		String resultString = " ";
		if (head.next == null) {
			return "Empty";
		}
		Node NewNode = head.next;
		while (NewNode != null) {
			resultString += NewNode.data + " ";
			NewNode = NewNode.next;
		}
		return resultString;
	}

	public static void main(String[] args) {
		LinkList linklist = new LinkList();
		int[] elem = { 1, 2, 3 };
		int[] location = { 1, 2, 3 };

		// 插入
		linklist.insert(location[0], elem[0]);
		linklist.insert(location[1], elem[1]);
		linklist.insert(location[2], elem[2]);
		System.out.println("打印链表:" + linklist.toString());

		// 查寻
		System.out.println(elem[2] + "->这个元素在第几个结点:" + linklist.getLocation(elem[2]));

		// 删除
		if (linklist.delete(location[2]))
			System.out.println("删除第" + location[2] + "个结点后,打印链表:" + linklist.toString());
		else
			System.out.println(",删除不成功");

		// 清空
		linklist.reset();
		System.out.println("清空链表后,打印链表:" + linklist.toString());
	}

} // LinkList

2.2 运行结果

在这里插入图片描述

2.3 注意

  • 我的源码的结点个数从1开始,而没有按照数组的下标从0开始。
  • 因为这样便于理解:头结点 -> 第1个结点 -> 第2个结点 -> … …
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜满月

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值