数据结构<九>: 链表实现的线程表

package com.mo.object;

/**
 *  线程表-->顺序表的数据结构
 */
public interface List {
	/**
	 * 插入
	 */
	void insert(int i,Object obj)throws Exception;
	/**
	 * 删除
	 */
	Object delete(int i)throws Exception;
	/**
	 * 取数据元素
	 */
	Object getData(int i)throws Exception;
	/**
	 * 求元素个数
	 */
	int size();
	/**
	 * 是否空
	 */
	boolean isEmpty();
}



package com.mo.linList;

public class Node {
	
	Object data;
	Node next;
	
	/**
	 *	用于头节点 ,头节点是没有数据的
	 */
	public Node(Node nextval) {
		this.next = nextval; 
	}
	/**
	 * 用于其他的节点
	 */
	public Node(Object data,Node next) {
		this.data = data;
		this.next = next;
	}
	/**
	 * 获取该节点的下一个
	 */
	public Object getNext() {
		return next;
	}
	/**
	 * 设置该节点的下一个Node
	 */
	public void setNext(Node node) {
		this.next = node;
	}
	/**
	 * 取出该节点的数据
	 */
	public Object getData() {
		return data;
	}
	/**
	 *	设置该节点的数据
	 */
	public void setData(Object data) {
		this.data = data; 
	}
	public String toString() {
		return data.toString();
	}
}



package com.mo.linList;

import com.mo.object.List;

public class LinList implements List {

	private Node head;//头节点
	private Node current;//当前的节点
	private int size;//当前元素的个数
	
	public LinList() {
		head = current = new Node(null);
		size = 0;
	}
	
	//定位-->传插进去和删除都要定位到前一个节点
	public void location(int i) throws Exception {
		if(i < -1 || i > size - 1) throw new Exception();
		//这里是下标为零的插进元素,直接return就行,这是当前的节点是head,就会直接在head的后面进行插进
		if(i == -1) return;
		current = head.next;
		int j = 0;
		while((current.next != null) && j < i) {
			current = current.next;
			j++;
		}
		//到这里就已经是定位成功了,current的节点就是你输入i值的位置了(这里的i是表的下标,从零开始的)
	}
	
	public void insert(int i, Object obj) throws Exception {
		//因为这是可以插到链表的最后一个元素,所以这里是不同减一的,删除的话就不一样了,因为不存在一个下标为size的元素
		if(i < 0 || i > size) throw new Exception();
		location(i - 1);
		Node node = new Node(obj,current.next);
		current.setNext(node);
		size++;
	}

	public Object delete(int i) throws Exception {
		if(i < 0 || i > size - 1) throw new Exception();
		//删除操作是定位到current的上一位的
		location(i - 1);
		//下面的current是要删除元素的上一个节点,因为你如果定位到要删除的元素的话就要调用上一个节点,但是这个是单链表,不能取得上一个节点
		Object delObj = current.next.getData();
		current.setNext(current.next.next);
		size--;
		return delObj;
	}

	public Object getData(int i) throws Exception {
		if(i < -1 || i > size - 1) throw new Exception();
		location(i);
		return current.getData();
	}

	public int size() {
		return size;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	public static void main(String[] args) throws Exception {
		LinList linList = new LinList();
		for(int i = 0; i < 10; i++) {
			linList.insert(i, i + 1);
		}
		linList.delete(4);
		for(int i = 0; i < linList.size(); i++) {
			System.out.print(linList.getData(i));
		}
	}

}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值