java实现单链表

前言

有时java api提供的链表并不能满足我们所有的需要,所以我们可以尝试自己实现一个单链表作为练习

一、单链表是什么?

单链表是一种链式存取的数据结构,因为它只有指向下一个地址的引用,所以叫单向链表。

二、单链表图解

三、单链表案例

请使用单链表模拟对水浒传中英雄人物的尾添加,有序添加,修改,删除,顺序查找,逆序查找,链表逆转,逆序打印等操作。

四、代码实现

1.测试代码

代码如下:

package com.yc.linkedlist;

import java.util.Stack;

public class SingleLinkedListDemo {

	public static void main(String[] args) {
		SingleLinkedListDemo slld = new SingleLinkedListDemo();
		HeroNode hero1 = slld.new HeroNode(1,"宋江","及时雨");
		HeroNode hero2 = slld.new HeroNode(2,"卢俊义","玉麒麟");
		HeroNode hero3 = slld.new HeroNode(3,"吴用","智多星");
		SingleLinkedList singleLinkedList = slld.new SingleLinkedList();
//		singleLinkedList.add(hero1);
//		singleLinkedList.add(hero3);
//		singleLinkedList.add(hero2);
		singleLinkedList.addByOrder(hero1);
		singleLinkedList.addByOrder(hero3);
		singleLinkedList.addByOrder(hero2);
		System.out.println("修改前");
		singleLinkedList.list();
		HeroNode hero4 = slld.new HeroNode(2,"卢俊义--","玉麒麟--");
		singleLinkedList.update(hero4);
		System.out.println("修改后");
		singleLinkedList.list();
		System.out.println("删除后");
		singleLinkedList.delete(1);
		singleLinkedList.delete(3);
		singleLinkedList.list();
		System.out.println("查找");
		System.out.println(singleLinkedList.find(2));
		System.out.println("链表长度");
		System.out.println(singleLinkedList.size());
		System.out.println("逆序查找");
		System.out.println(singleLinkedList.findByLast(1));
		System.out.println("链表逆转前");
		singleLinkedList.addByOrder(hero3);
		singleLinkedList.list();
		System.out.println("链表逆转后");
		singleLinkedList.reverse();
		singleLinkedList.list();
		System.out.println("逆序打印");
		singleLinkedList.reversePrint();
	}

	//单链表
	class SingleLinkedList{
		private HeroNode head = new HeroNode();
		
		/**
		 * 逆序打印
		 */
		public void reversePrint(){
			if(head.next == null){
				return;
			}
			if(head.next.next == null){
				System.out.println(head.next);
				return;
			}
			Stack<HeroNode> stack = new Stack<HeroNode>();
			HeroNode temp = head.next;
			while(temp!=null){
				stack.push(temp);
				temp = temp.next;
			}
			while(!stack.isEmpty()){
				System.out.println(stack.pop());
			}
			return;
		}
		
		/**
		 * 链表逆转
		 */
		public void reverse(){
			if(head.next == null || head.next.next == null){
				return;
			}
			HeroNode newHead = new HeroNode();
			HeroNode temp = head.next;
			HeroNode next = null;
			while(temp!=null){
				next = temp.next;
				temp.next = newHead.next;
				newHead.next = temp;
				temp = next;
			}
			head.next = newHead.next;
			return;
		}
		
		/**
		 * 逆序查找
		 * @param index 索引
		 * @return
		 */
		public HeroNode findByLast(int index){
			if(head.next == null){
				return null;
			}
			int len = size();
			HeroNode temp = head.next;
			if(index<1||index>len){
				return null;
			}
			for(int i=0;i<len-index;i++){
				temp = temp.next;
			}
			return temp;
		}
		
		/**
		 * 链表大小
		 * @return
		 */
		public int size(){
			if(head.next == null){
				return 0;
			}
			int count = 0;
			HeroNode temp = head.next;
			while(temp!=null){
				count++;
				temp = temp.next;
			}
			return count;
		}
		
		/**
		 * 顺序查找
		 * @param no
		 * @return
		 */
		public HeroNode find(int no){
			if(head.next == null){
				return null;
			}
			boolean flag = false;
			HeroNode temp = head.next;
			while(temp != null){
				if(temp.no == no){
					flag = true;
					break;
				}
				temp = temp.next;
			}
			if(flag){
				return temp;
			}
			return null;
		}
		
		/**
		 * 删除节点
		 * @param no
		 */
		public void delete(int no){
			if(head.next == null){
				return;
			}
			boolean flag = false;
			HeroNode temp = head;
			while(temp.next != null){
				if(temp.next.no == no){
					flag = true;
					break;
				}
				temp = temp.next;
			}
			if(flag){
				temp.next = temp.next.next;
			}
			return;
		}
		
		/**
		 * 更新节点
		 * @param hero
		 */
		public void update(HeroNode hero){
			if(head.next == null){
				return;
			}
			boolean flag = false;
			HeroNode temp = head.next;
			while(temp != null){
				if(temp.no == hero.no){
					flag = true;
					break;
				}
				temp = temp.next;
			}
			if(flag){
				temp.name = hero.name;
				temp.nickname = hero.nickname;
			}
			return;
		}
		
		/**
		 * 有序添加
		 * @param hero
		 */
		public void addByOrder(HeroNode hero){
			HeroNode temp = head;
			while(true){
				if(temp.next == null){
					break;
				}
				if(temp.next.no>hero.no){
					break;
				}
				temp = temp.next;
			}
			hero.next = temp.next;
			temp.next = hero;
			return;
		}
		
		/**
		 * 尾添加
		 * @param hero
		 */
		public void add(HeroNode hero){
			HeroNode temp = head;
			while(temp.next!=null){
				temp = temp.next;
			}
			temp.next = hero;
			return;
		}
		
		/**
		 * 打印链表
		 */
		public void list(){
			HeroNode temp = head.next;
			while(temp!=null){
				System.out.println(temp);
				temp = temp.next;
			}
			return;
		}
	}
	
	//英雄节点
	class HeroNode{
		private int no;
		private String name;
		private String nickname;
		private HeroNode next;
		
		public HeroNode(int no, String name, String nickname) {
			super();
			this.no = no;
			this.name = name;
			this.nickname = nickname;
		}

		public HeroNode() {
			super();
		}

		public int getNo() {
			return no;
		}

		public void setNo(int no) {
			this.no = no;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getNickname() {
			return nickname;
		}

		public void setNickname(String nickname) {
			this.nickname = nickname;
		}

		public HeroNode getNext() {
			return next;
		}

		public void setNext(HeroNode next) {
			this.next = next;
		}

		@Override
		public String toString() {
			return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
		}
	}
}

2.代码分析

我认为单链表中的有序添加是一个比较有意义的方法,因为java的LinkedList中没有类似的方法,因此这个方法可以分享给大家。

总结

这就是我所了解的单链表的全部内容,希望能与更多的朋友一起交流讨论单链表的知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值