数据结构和算法(Java版)学习笔记之单链表(八)

单链表基本概念

单链表是一种链式存取的数据结构,用一组地址任意的存储单元(可以是连续或者不连续的存储单元)存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的数据) + 指针(指示后继元素存储位置)。

  1. 结点结构

    ┌───┬───┐
    │data │next │
    └───┴───┘
    data域–存放结点值的数据域
    next域–存放结点的直接后继结点的地址(位置)
    链表通过每个结点的链域将线性表的n个结点按其逻辑顺序链接在一起的,每个结点只有一个链域的链表称为单链表(Single Linked List)。

  2. 单链表存储示例图
    单链表

单链表Java代码实现,超级基础写法

  1. 创建一个Node类(结点)
public class Node {
	public int iData;
	public double dData;
	public Node next;
	/**
	 * 结点id和节点数据dd
	 * @param id
	 * @param dd
	 */
	public Node(int id, double dd) {
		iData = id;
		dData = dd;
	}
	//显示当前结点信息
	public void displayNode() {
            System.out.print("{"+iData+" ,"+dData+"} ");
	}
}
  1. LinkList类,单链表方法实现
public class LinkList {
	private Node head;
	public LinkList() {
		head = null;
	}
	//添加结点
	public void insertFirst(int id, double dd) {
		//创建一个新的链接点
		Node newNode = new Node(id, dd);
		newNode.next = head;
		head = newNode;
		
	}
	//是否为空
	public boolean isEmpty() {
		return head==null;
	}
	//删除第一个结点
	public Node deleteHead() {
		Node temp = head;
		head = head.next;
		return temp;
	}
	//通过id查找结点
	public Node find(int key) {
		Node current =  head;
		while(current.iData != key) {
			if(current.next == null) {
				return null;
			}else {
				current = current.next;
			}
		}
		return current;
	}
	//通过id删除结点
	public Node delete(int key) {
		Node current = head;
		Node previous = head;//前一个
		while(current.iData != key) {
			if(current.next == null) {
				return null;
			}else {
				previous = current;
				current = current.next;
			}
		}
		
		if(current == head) {
			head = head.next;
		}else {
			previous.next = current.next;
		}
		return current;
	}
	//显示所有结点
	public void displayNode() {
		System.out.print("List (first-->last):");
		Node current = head;
		while(current != null) {
			current.displayNode();
			current = current.next;
		}
		System.out.println();
	}
}
  1. LinkListApp类,对单链表功能进行测试
public class LinkListApp {
	public static void main(String[] args) {
		LinkList theList = new LinkList();
		// 添加结点
		theList.insertFirst(1, 2.99);
		theList.insertFirst(2, 4.99);
		theList.insertFirst(3, 6.99);
		theList.insertFirst(4, 8.99);
		// 显示所有结点
		theList.displayNode();

		// 通过id查找结点
		Node f = theList.find(2);
		if (f != null) {
			System.out.println("Found node with key: " + f.dData);
		} else {
			System.out.println("Not Found");
		}
		// 通过id删除结点
		Node d = theList.delete(3);
		if (d != null) {
			System.out.println("Delete node with key: " + d.dData);
		} else {
			System.out.println("Can't delete link");
		}
		// 显示所有结点
		theList.displayNode();

		// 删除全部
		while (!theList.isEmpty()) {
			Node deleteFirst = theList.deleteHead();
			System.out.print("Deleted");
			deleteFirst.displayNode();
			System.out.println();
		}
		// 显示所有结点
		theList.displayNode();
	}
}
  1. 结果显示
List (first-->last):{4 ,8.99} {3 ,6.99} {2 ,4.99} {1 ,2.99} 
Found node with key: 4.99
Delete node with key: 6.99
List (first-->last):{4 ,8.99} {2 ,4.99} {1 ,2.99} 
Deleted{4 ,8.99} 
Deleted{2 ,4.99} 
Deleted{1 ,2.99} 
List (first-->last):

以上就是单链表的实现,本人认为难点在于LinkList类的添加结点方法:

	//添加结点
	public void insertFirst(int id, double dd) {
		//创建一个新的链接点
		Node newNode = new Node(id, dd);
		newNode.next = head;
		head = newNode;
	}

这里需要重点理解一下这个方法,将这个方法掌握,单链表也就基本掌握了。

发布了46 篇原创文章 · 获赞 11 · 访问量 3922

猜你喜欢

转载自blog.csdn.net/qq_42197800/article/details/89577890