线性链表 java实现

public class LinkList<T> {
	class Node{//定义Node节点
		private T data;
		private Node next;
		public Node(){}
		public Node(T data,Node next){
			this.data=data;
			this.next=next;
		}	
	}
	private Node header;//头指针
	private Node tail;//尾指针
	private int size;//线性表的长度
	public LinkList(){//构造空的线性链表
		this.header=null;
		this.tail=null;
		this.size=0;
	}
	public LinkList(T element){//构造一个节点的线性链表
		this.header=new Node(element,null);
		this.tail=this.header;
		this.size++;
	}
	public int length(){//返回线性链表的长度
		return size;
	}
	public void add(T element){//尾插法添加节点
		if(header==null){
			header=new Node(element,null);
			tail=header;
		}else{
			Node newNode=new Node(element,null);
			tail.next=newNode;
			tail=newNode;
		}
		size++;
	}
	public void addAtHeader(T element){//头插法尾链表添加新节点
		header=new Node(element,header);
		if(tail==null)
			tail=header;
		
		size++;
	}
	public Node getNodeByIndex(int index){//得到节点
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}
		Node current=header;
		for(int i=0;i<size && current!=null;current=current.next,i++){
			if(i==index)
				return current;
		}
		return null;
	}
	public int locate(T element){
		Node current=header;
		for(int i=0;i<size && current!=null;i++,current=current.next){
			if(current.data==element)
				return i;
		}		
		return -1;
	}
	public void insert(T element,int index){
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}else{
			if(index==0){
				addAtHeader(element);
			}else{
				Node prev=getNodeByIndex(index-1);
				prev.next=new Node(element,prev.next);
				size++;
			}
		}
	}
	public T delete(int index){//删除节点
		if(index<0||index>size-1){
			throw new IndexOutOfBoundsException("线性表索引越界");
		}
		Node del=null;
		if(index==0){
			del=header;
			header=header.next;
		}else{
			Node prev=getNodeByIndex(index-1);
			del=prev.next;
			prev.next=del.next;
			del.next=null;
		}
		size--;
		return del.data;	
	}
	public boolean empty(){//判断链表是否为空
		return size==0;
	}
	public void clear(){//清空链表
		header=null;
		tail=null;
		size=0;
	}
	public String toString(){
		if(empty()){
			return "[]";
		}else{
			StringBuilder sb=new StringBuilder("[");
			for(Node current=header;current!=null;current=current.next){
				sb.append(current.data.toString()+",");
			}
			int len=sb.length();
			return sb.delete(len-1,len).append("]").toString();
		}
	}
	public static void main(String []args){
		LinkList<String> list=new LinkList<String>();
		list.add("zhang san");
		list.add("li si");
		list.add("wang wu");
		list.add("zhang long");
		list.add("zhao hu");
		System.out.println("打印单链表"+list);
		System.out.println("li si"+"["+list.locate("li si")+"]");
		list.delete(4);
		System.out.println("删除元素后的单链表"+list);
	}
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值