算法通关6-链表-1 实现单链表

Github:
https://github.com/345166018/Algorithm/blob/master/HxAlgo/src/linkedlist/SinglyLinkedList.java

在这里插入图片描述

1 顺序的单链表

定义结点

/**
	 * 定义结点
	 *
	 */
	public static class Node {

		int val;
		Node next;

		public Node(int val, Node next) {
			this.val = val;
			this.next = next;
		}

		public int getVal() {
			return val;
		}

	}

定义一个头结点head为null

private Node head = null;

在链表尾部插入结点

/**
	 * 顺序插入 
	 * 链表尾部插入
	 * 找到链表的末尾结点,把新添加的数据作为末尾结点的后续结点
	 */
	public void insertTail(int val) {
		Node newNode = new Node(val, null);//新结点的next指针指向null
		if (head == null) {//如果头结点为null,直接将新结点作为头结点
			head = newNode;
		} else {//否则将新结点插入到链表末尾
			Node q = head;
			while (q.next != null) {
				q = q.next;
			}
			newNode.next = q.next;//其实还是将新结点的next指针指向null(q.next为null)
			q.next = newNode;//将新结点插入到链表末尾
		}
	}

打印所有结点值

/**
	 * 打印链表的所有结点值
	 */
	public void printAll() {
		Node p = head;
		while (p != null) {
			System.out.println(p.val + "");
			p = p.next;
		}
		System.out.println();
	}

测试

public static void main(String[] args) {
		HxSinglyLinkedList link = new HxSinglyLinkedList();

		int data[] = { 1, 2, 3, 4, 5 };

		for (int i = 0; i < data.length; i++) {
			link.insertTail(data[i]);
		}

		link.printAll();

	}

打印结果:

1
2
3
4
5

2 逆序的单链表

表头部插入,这种操作将于输入的顺序相反,逆序

/**
	 * 表头部插入,这种操作将于输入的顺序相反,逆序
	 */
    public void insertToHead(int value) {
        Node newNode = new Node(value, null);
        if (head == null) {
			head = newNode;
		}else {
			newNode.next = head;//将新结点的next指针指向头结点,就是在表头部插入
			head = newNode;
		}

    }

贴上所有代码

package linkedlist;

public class SinglyLinkedList {

	public static void main(String[] args) {
		SinglyLinkedList link = new SinglyLinkedList();

		int data[] = { 1, 2, 3, 4, 5 };

		Node p = link.createNodeByTail(data);

		link.printAll(p);

		Node q = link.createNodeByHead(data);

		link.printAll(q);

	}

	public static class Node {
		private int data;
		private Node next;

		public Node(int data, Node next) {
			this.data = data;
			this.next = next;
		}

		public int getData() {
			return data;
		}
	}
	
	//链表尾部插入
	public Node createNodeByTail(int[] data) {

		Node head = null;

		// 顺序插入
		// 链表尾部插入
		for (int i = 0; i < data.length; i++) {
			Node newNode = new Node(data[i], null);
			// 空链表,可以插入新节点作为head,也可以不操作
			if (head == null) {
				head = newNode;
			} else {
				Node q = head;
				while (q.next != null) {
					q = q.next;
				}
				q.next = newNode;

			}
		}

		return head;

	}

	//链表头部插入
	public Node createNodeByHead(int[] data) {

		Node head = null;

		// 表头部插入,这种操作将于输入的顺序相反,逆序
		for (int i = 0; i < data.length; i++) {
			Node newNode = new Node(data[i], null);
			if (head == null) {
				head = newNode;
			} else {
				newNode.next = head;
				head = newNode;
			}
		}

		return head;

	}

	public void printAll(Node p) {

		while (p != null) {
			System.out.println(p.data + "");
			p = p.next;
		}
		System.out.println();
	}

	private Node head = null;

	// 顺序插入
	// 链表尾部插入
	public void insertTail(int value) {

		Node newNode = new Node(value, null);
		// 空链表,可以插入新节点作为head,也可以不操作
		if (head == null) {
			head = newNode;

		} else {
			Node q = head;
			while (q.next != null) {
				q = q.next;
			}
			newNode.next = q.next;
			System.out.println("hongx q next data = " + q.next.data);
			q.next = newNode;
		}
	}

	/**
	 * 表头部插入,这种操作将于输入的顺序相反,逆序
	 */
	public void insertToHead(int value) {
		Node newNode = new Node(value, null);
		if (head == null) {
			head = newNode;
		} else {
			newNode.next = head;
			head = newNode;
		}

	}

	public void printAll() {

		Node p = head;
		while (p != null) {
			System.out.println(p.data + "");
			p = p.next;
		}
		System.out.println();
	}

}

Github:
https://github.com/345166018/Algorithm/blob/master/HxAlgo/src/linkedlist/SinglyLinkedList.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值