java单链表的实现自己动手写一个单链表

java单链表的实现自己动手写一个单链表

单链表:单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
自己手动写一个单链表:
首先,定义一个节点类:

package com.wei;

public class Link {
	public int data;// 存放数据
	public Link next;// 存放下一个节点

	public Link(int data) {
		this.data = data;
	}

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

	public Link() {
	}

	public void display() {
		System.out.println(data + " ");
	}
}

第二部分是定义一个链表类:

package com.wei;

public class LinkList {

	public Link frist;// 定义一个头节点
	public Link last;//尾指针永远指向头节点
	public int size = 0;// 节点的位置

	public LinkList() {
		this.frist = null;//
	}

	/**
	 * 判断链表是否为空
	 * 
	 * @return
	 */
	public boolean isis() {
		return size == 0;
	}

	/**
	 * 头插法
	 * 
	 * @param data
	 */
	public void addfrist(int data) {
		Link L = new Link(data);
		L.next = frist;
		frist = L;
		size++;
	}

	/**
	 * 尾插法
	 * 
	 * @param data
	 */
	public void addlast(int data) {

		if (frist == null) {
			frist = new Link(data);
			last = frist;
		} else {
			Link newL = new Link(data);
			last.next = newL;
			last = newL;
		}
		size++;
	}

	/**
	 * 从头删除
	 * 
	 * @return
	 */
	public Link removefrist() {
		Link d = frist;
		frist = d.next;
		size--;
		return d;
	}

	/**
	 * 删除最后一个
	 */
	public void dellast() {
		Dell(size - 1);
	}

	/**
	 * 获取链表长度
	 */
	public void displayAllLink() {
		Link cure = frist;
		while (cure != null) {
			cure.display();
			cure = cure.next;
		}
		System.out.println("长度" + size);
	}

	/**
	 * 获取指定位置的节点元素
	 * 
	 * @param index
	 * @return
	 */
	public Link getData(int index) {
		if (index < 0 && index > size - 1) {
			throw new IndexOutOfBoundsException("越界");
		}
		Link count = frist;
		for (int i = 0; i < size && count != null; i++, count = count.next) {
			if (i == index) {
				return count;
			}
		}
		return null;
	}

	/**
	 * 按值查找指定位置
	 * 
	 * @param element
	 * @return
	 */
	public int selectIndex(int element) {
		Link current = frist;
		for (int i = 0; i < size && current != null; i++, current = current.next) {
			if (current.data == element) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 删除链表中数值最大的元素
	 */
	public void delMax() {
		// 要遍历的链表
		Link cu = frist;
		// 初始化一个节点,当中间变量
		Link cc = new Link(0);
		// 遍历
		for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
			if (cu.data > cc.data) {
				cc.data = cu.data;
			}
		}
		int data = cc.data;
		int number = selectIndex(data);
		Dell(number);
	}

	/**
	 * 删除链表中数值最小的元素
	 */
	public void delMin() {
		// 要遍历的链表
		Link cu = frist;
		// 初始化一个节点,当中间变量
		Link cc = new Link(0);
		// 遍历
		for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
			if (cu.data < cc.data) {
				cc.data = cu.data;
			}
		}
		int data = cc.data;
		int number = selectIndex(data);
		Dell(number);
	}

	/**
	 * 从指定位置处插入数据
	 * 
	 * @param t
	 * @param index
	 */
	public void insert(int t, int index) {
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException("索引超出线性表范围");
		}
		if (frist == null) {
			addlast(t);
		} else {
			if (index == 0) {
				addfrist(t);
			} else {
				Link k = getData(index - 1);
				k.next = new Link(t, k.next);
				size++;
			}

		}
	}

	/**
	 * 从指定位置处删除
	 * 
	 * @param index
	 */
	public void Dell(int index) {
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException("索引超出线性表范围");
		}
		Link del = null;
		if (index == 0) {
			del = frist.next;
			frist = frist.next;
		} else {
			Link neL = getData(index - 1);
			del = neL.next;
			neL.next = del.next;
			del.next = null;
		}
		size--;
	}

	/**
	 * 清空链表
	 */
	public void clear() {
		frist = null;
		last = null;
		size = 0;
	}

	/**
	 * 按从小到大排序
	 */
	public void Min_to_Max() {
		// 要遍历的链表
		Link cu = frist;
		// 记录最小值
		int min;
		while (cu != null) {
			// 内重循环从当前节点的下一个节点循环到尾节点,找到和外重循环的值比较最小的那个,然后与外重循环进行交换
			Link nextLink = cu.next;
			while (nextLink != null) {
				// 比外循环小的值放在前面
				if (nextLink.data < cu.data) {
					min = nextLink.data;
					nextLink.data = cu.data;
					cu.data = min;
				}
				nextLink = nextLink.next;
			}
			cu = cu.next;
		}

	}

	/**
	 * 按从大到大排序
	 */
	public void Max_to_Min() {
		// 要遍历的链表
		Link cu = frist;
		// 记录最小值
		int min;
		while (cu != null) {
			// 内重循环从当前节点的下一个节点循环到尾节点
			//找到和外重循环的值比较最小的那个,然后与外重循环进行交换
			Link nextLink = cu.next;
			while (nextLink != null) {
				// 比外循环小的值放在前面
				if (nextLink.data > cu.data) {
					min = nextLink.data;
					nextLink.data = cu.data;
					cu.data = min;
				}
				nextLink = nextLink.next;
			}
			cu = cu.next;
		}

	}
	
}

最后是测试类:

package com.wei;

public class Test {

	public static void main(String [] arr) {
		LinkList g = new LinkList();
		g.addlast(13);
		g.addlast(16);
		g.addlast(-3);
		g.addlast(8);
		g.addlast(5);
		g.addlast(22);
	    g.Min_to_Max();
	    g.displayAllLink();
	    g.Max_to_Min();
	    g.displayAllLink();
	}
}

一条链表就这样写完了,需要什么功能可以自己扩展。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

想落天外X

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值