手写链表(二)-使用内部类实现添加、查询、定位、插入等功能

要求是这样的:


我们在示例(示例即本人博客手写链表(一))中给出的链表类是递归定义的。这样做的好处是链表的某个局部也是一个链表,这与链表在逻辑上的概念具有一致性。


但出于效率的考虑,经常需要引入一些辅助变量来加快操作的速度,这时,如果能给链表类型增加一个外壳就很方便后续的处理。


基本思路:


class MyList {
private Node head;


class Node {
int data;
Node next;
}
....
}


这种方案中,MyList不是递归定义的,而内部包含的Node类代表了链表的真实结构。MyList类是一个外壳类。它通过持有head 指针来记录一个链表的头在哪里。


请试着完善这种方案。


使用内部类实现添加、查询、定位、插入等功能

public class MyList<T> {
	class Node{
		public Node next;
		public T data;
		public Node(T data, Node next){
			this.data=data;
			this.next=next;
		}
	}
	
	private Node head;
	private Node tail;
	private int size=0;
	
	public MyList(){
		
	}

	public MyList(T headValue){
		this.head=new Node(headValue, null);
		this.tail=this.head;
		size++;
	}
	public int length(){
		return size;
	}
	public boolean empty(){
		return size==0;
	}

	public void add(T element){
		if(size==0){
			this.head=new Node(element, null);
			this.tail=this.head;
		}
		else{
			Node next=new Node(element, null);
			tail.next=next;
			tail=next;
		}
		size++;
	}
	//头增加方式
	public void addToHead(T element){
		if(size==0){
			this.head.data=element;
			this.head.next=null;
			this.tail=this.head;
		}
		else{
			Node header=new Node(element, head);
			head=header;
		}
		size++;
	}
	//插入
	public void insert(T element , int index){
		if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
		else if(index==0) addToHead(element);
		else if(size==0) add(element);
		else{
			Node previous=head;
			for(int i=0;i<=index & previous!=null;i++,previous=previous.next){
				if(i==index-1){
					previous.next=new Node(element,previous.next);
					size++;
					break;
				}
			}
		}
	}
	//搜索
	public T search(int index){
		if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
		else{
			Node current=head;
			for(int i=0;i<=index & current!=null;i++,current=current.next){
				if(i==index) return (T)current.data;
			}
			return null;
		}
	}
	//元素定位
	public int locate(T element){
		Node current=head;
		for(int i=0;i<size & current!=null;i++,current=current.next){
			if(current.data.equals(element)) return i;
		}
		return -1;
	}
	//删除
	public T delete(int index){
		if(index<0 | index>size-1) throw new IndexOutOfBoundsException("越界");
		else if(index==0) {
			T value=head.data;
			head=head.next;
			size--;
			return value;
		}
		else{
			Node current=head;
			for(int i=0;i<size & current!=null;i++,current=current.next){
				if(i==index-1){
					T value=current.next.data;
					Node delNode=current.next;
					current.next=delNode.next;
					delNode.next=null;
					size--;
					return value;
				}
			}
			return null;
		}
	}
	public void remove(){
		delete(size-1);
	}
	public void clear(){
		head=null;
		tail=null;
		size=0;
	}
	
	public String toString(){
		StringBuilder strB=new StringBuilder("[");
		if(size==0) return "[]";
		else{
			for(Node current=head; current!=null;current=current.next){
				strB.append(current.data.toString()+", ");
			}
			return strB.toString().substring(0,strB.length()-2)+"]";
		}
	}
	//展示
	public void show(){
		System.out.println(this.toString());
	}
}

测试主函数:

public class Test {
	public static void main(String[] args) {
		MyList<Object> a = new MyList<Object>();
		a.add(10);
		a.add(20);
		a.add(30);
		a.insert(15, 1);
		a.addToHead(5);
		a.show();
		a.delete(1);//删除第二个元素
		a.show();
		System.out.println(a.search(2));//查询第三个元素
		System.out.println(a.locate(20));//元素定位
	}
}

结果:

[5, 10, 15, 20, 30]
[5, 15, 20, 30]
20
2


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值