【玩转数据结构 从入门到进阶】 链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014543872/article/details/84566887
public class LinkList<E> {

	private class Node {

		public E e;
		public Node next;

		public Node(E e, Node next) {
			this.e = e;
			this.next = next;
		}

		public Node(E e) {
			this(e, null);
		}

		public Node() {
		}

		@Override
		public String toString() {
			return e.toString();
		}

	}

	private Node dummyhead;
	int size;

	public LinkList() {
		dummyhead = new Node(null, null);
		size = 0;
	}

	// 获取链表中的元素个数
	public int getSize() {
		return size;
	}

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

	// 在链表中间添加元素e
	public void add(int index, E e) {
		if (index < 0 || index > size) {
			throw new IllegalArgumentException("Add failed.Illegal index");
		}

		Node prev = dummyhead;
		for (int i = 0; i < index; i++) {
			prev = prev.next;
		}
		Node node = new Node(e);
		node.next = prev.next;
		prev.next = node;
		size++;

	}

	// 在链表头添加元素e
	public void addFirst(E e) {
		add(0, e);
	}

	// 在链表末尾添加元素e
	public void addList(E e) {
		add(size, e);
	}

	// 获得链表中index位置的元素e
	public E get(int index) {
		// 判断元素的合法性
		if (index < 0 || index > size)
			throw new IllegalArgumentException("Get failed.Illegal index");

		Node cur = dummyhead.next;
		for (int i = 0; i < index; i++)
			cur = cur.next;

		return cur.e;
	}

	// 获得链表第一个元素
	public E getFirst() {
		return get(0);
	}

	// 获得链表最后一个元素
	public E getLast() {
		return get(size);
	}

	// 修改链表中index位置的元素为e
	public void set(int index, E e) {

		// 判断元素的合法性
		if (index < 0 || index > size)
			throw new IllegalArgumentException("Set failed.Illegal index");
		Node cur = dummyhead.next;
		for (int i = 0; i < index; i++)
			cur = cur.next;

		cur.e = e;
	}

	// 查看链表中是否存在元素e
	public boolean contains(E e) {
		Node cur = dummyhead.next;
		while (cur != null) {
			if (cur.e == e)
				return true;
			cur = cur.next;
		}

		return false;

	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		/*
		 * Node cur=dummyhead.next; while(cur!=null) { res.append(cur+"->");
		 * cur=cur.next; }
		 */
		for (Node cur = dummyhead.next; cur != null; cur = cur.next)
			res.append(cur + "->");
		res.append("NULL");

		return res.toString();
	}

	// 删除链表中的元素e
	public E remove(int index) {

		// 判断元素的合法性
		if (index < 0 || index > size)
			throw new IllegalArgumentException("Remove failed.Illegal index");

		Node prev=dummyhead;
		for(int i=0;i<index;i++) {
			prev=prev.next;
		}
	    Node retNode=prev.next;
	    
	    prev.next=retNode.next;
	    retNode.next=null;
	    
	    size--;
		
		return retNode.e;
	}
	
	//删除链表中的第一个元素
    public E removrFirst() {
	   return remove(0);
   }
    //删除链表中的最后一个元素
    public E removeLast() {
    	return remove(size-1);
    }
   
	
	
	
	
	
	

}
//用链表实现栈
public class LinkListStack<E> implements Stack<E> {
    
	
	private LinkList<E> list;
	
	 public LinkListStack() {
	    list=new LinkList<>();
	}
	
	//获得栈中元素个数
	@Override
	public int getSize() {
		return list.getSize();
	}
    //判断栈中是否为空
	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}
    //进栈操作
	@Override
	public void push(E e) {
		list.addFirst(e);
		
	}
    
	//出栈操作
	@Override
	public E pop() {    
		return list.removrFirst();
	}
    //获得栈顶元素操作
	@Override
	public E peek() {
		// TODO Auto-generated method stub
		return list.getFirst();
	}
    
	
   @Override
   public String toString() {
	   
	   StringBuilder res=new StringBuilder();
	   
	   res.append("Stack top:");
	   res.append(list);
	   return res.toString();
	    
   }
	
	
	
}
import Arr.LinkList.Node;

//用链表实现队列
public class LinkListQueue<E> implements Queue<E> {
       
	public class Node {

		public E e;
		public Node next;

		public Node(E e, Node next) {
			this.e = e;
			this.next = next;
		}

		public Node(E e) {
			this(e, null);
		}

		public Node() {
		}

		@Override
		public String toString() {
			return e.toString();
		}

	}
	
	private Node head,tail;
	private int size;
	
	public LinkListQueue() {
		head=null;
		tail=null;
		size=0;	
	}
	
	//获得队列中的元素个数
	public int getSize() {
		return size;
	}
	
	//判断队列中是否为空
	public boolean isEmpty() {
		return size==0;
	}
    //入队操作
	@Override
	public void enqueue(E e) {
		if(tail==null) {
			tail=new Node(e);
			head=tail;
		}else {
			tail.next=new Node(e);
			tail=tail.next;
			
		}
		size++;	
	}

	@Override
	public E dequeue() {
		//判断队列中元素是否为空
		if(isEmpty()) 
			throw new IllegalArgumentException("Dequeue failed.Illegal index");
		
		Node retNode=head;
		head=head.next;
		retNode=null;
		if(head==null) {
			tail=null;
		}
		
		size--;
		return retNode.e;
	}

	@Override
	public E getFront() {
		if(isEmpty()) 
			throw new IllegalArgumentException("Queue is empty");
		return head.e;
	}
	
	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		/*
		 * Node cur=dummyhead.next; while(cur!=null) { res.append(cur+"->");
		 * cur=cur.next; }
		 */
		res.append("Queue front");
		for (Node cur = head.next; cur != null; cur = cur.next)
		res.append(cur + "->");
		res.append("NULL tail");

		return res.toString();
		
		
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/u014543872/article/details/84566887