JAVA学习(小白向)—链表—2021-06-01

本文是Java小白向的链表学习内容。介绍了链表是物理存储非连续、非顺序的结构,分单链表和双向链表,阐述其优缺点、具体操作,对比了链表与线性表在插入、删除时的不同,还提及Java中LinkedList与ArrayList的适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA学习(小白向)—链表—2021-06-01

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的

大致分为单链表和双向链表
1.单链表:每个节点包含两部分,一部分存放数据变量的data,另一部分是指向下一节点的next指针
2.双向链表:除了包含单链表的部分,还增加的pre前一个节点的指针,链表的优点在于,不需要连续的存储单元,修改链表的复杂度为O(1) (在不考虑查找时)
但是缺点也很明显:无法直接找到指定节点,只能从头节点一步一步寻找复杂度为O(n)

具体操作

  1. 支持与顺序表相同的操作: 初始化、插入、删除等.
  2. 为节点建一个类.
  3. 引用与指针的异同. 前者只能使用; 后者可以支持 p ++危险操作.
  4. 引用数据类型的赋值, 都不会产生新的对象空间.
  5. 链表与线性表在插入、删除时的不同: 前者不移动元素, 只改变引用 (指针).

代码

package a12;

/**
 * ******************
 * Linked List.
 * @author hengyuzuo
 * ******************
 */

public class LinkedList {

	/**
	 * **************
	 * An inner class.
	 * **************
	 */
	class Node {
		/**
		 * *************
		 * The data.
		 * *************
		 */
		int data;
		
		/**
		 * *************
		 * The reference to the next node.
		 * *************
		 */
		Node next;
		
		/**
		 * ****************
		 * The constructor
		 * @param paraValue
		 * ****************
		 */
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}// Of the constructor
	}// Of class Node 
	
	/**
	 * ********************
	 * The header node. The data is never used.
	 * ********************
	 */
	Node header;
	
	/**
	 * *************************
	 * Constructor an empty sequential list.
	 * *************************
	 */
	public LinkedList () {
		header = new Node(0);
		//header.next = null
	}// Of the first constructor
	
	/**
	 * *************************
	 * Overrides the method claimed in object, the supperclass of any class.
	 * *************************
	 */
	public String toString() { 
		String resultString = "";
		
		if (header.next == null) {
			return "empty";
		}// Of if
		
		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		}// Of whlie
		
		return resultString;
	}// Of toString
	
	/**
	 * **********************
	 * Reset to empty. Free the space through garbage collection.
	 * **********************
	 */
	public void reset() {
		header.next = null;
	}// Of reset
	
	/**
	 * ***********************
	 * Locate the given value. If it appears in multiple positions, simply return the first one.
	 * @param paraValue
	 * @return The position -1 for not found.
	 * ***********************
	 */
	public int locate(int paraValue) {
		int tempPosition = -1;
		
		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		while (tempNode != null){
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			}// Of if
			
			tempNode = tempNode.next;
			tempCurrentPosition++;
		}// Of while
		
		return tempPosition;
	}// Of locate
	
	/**
	 * *******************
	 * Insert a value to a position. If the list is already full , do nothing.
	 * @param paraPosition
	 * @param paraValue
	 * @return Success or not.
	 * *******************
	 */
	public boolean insert (int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position" + paraPosition + " is illegal.");
				return false;
			}// Of if
			tempNode = tempNode.next;
		}// Of for i 
		
		//Construct a new node.
		tempNewNode = new Node(paraValue);
		
		//Now link them.
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}// Of insert
	
	/**
	 * ******************
	 * Delete a value at a position.
	 * @param paraPositon
	 * @return Success or not.
	 * ******************
	 */
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		}// Of if
		
		Node tempNode = header;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The positon" + paraPosition + " is illeagal.");
				return false;
			}// Of if
			
			tempNode = tempNode.next;
		}// Of for i
		
		 tempNode.next = tempNode.next.next;
		 return true;
	}// Of delete
	
	/**
	 * ******************
	 * The entrance of the program.
	 * @param args not used now.
	 * ******************
	 */
	public static void main(String args[]) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		
		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0,i);
		}// Of for i
		System.out.println("Inserted, the list is: " + tempFirstList.toString());
		
		tempFirstList.insert(6, 9);
		
		tempFirstList.delete(4);
		
		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());
		
		tempFirstList.delete(0);
		System.out.println("Deleted, the list id: " + tempFirstList.toString());
		
		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		}// Of for i 
	}// Of main	
}// Of class LinkedList

运行结果

Initialized, the list is: empty
Inserted, the list is: 4, 3, 2, 1, 0, 
The position6 is illegal.
Deleted, the list is: 4, 3, 1, 
Deleted, the list id: 3, 1, 
Looped delete, the list is: 1, 
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty

小结

  1. java所有类的父类是Object类,toString()是Object类的方法,如果你没有进行重写操作,调用toString()返回的是你这个对象在堆内存中的物理地址信息;如果进行了重写,返回的是你重写后的信息;一般重写后用于开发时,数据的打印查看

  2. node节点

  3. 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的地址。链表可分为单向链表和双向链表。一个单向链表包含两个值: 当前节点的值和一个指向下一个节点的链接。一个双向链表有三个整数值: 数值、向后的节点链接、向前的节点链接。

  4. Java LinkedList(链表) 类似于 ArrayList,是一种常用的数据容器与 ArrayList 相比,LinkedList 的增加和删除对操作效率更高,而查找和修改的操作效率较低。

  5. 以下情况使用 ArrayList :

    频繁访问列表中的某一个元素。
    只需要在列表末尾进行添加和删除元素操作。

  6. 以下情况使用 LinkedList :

    你需要通过循环迭代来访问列表中的某些元素。
    需要频繁的在列表开头、中间、末尾等位置进行添加和删除元素操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值