【數據結構】棧的鏈表實現

import java.util.EmptyStackException;

public class LinkedStack<E> implements Cloneable
{
	private Node<E> top;
	
	public LinkedStack()
	{
		top = null;
	}
	
	public LinkedStack<E> clone()
	{
		LinkedStack<E> answer;
		try
		{
			answer = (LinkedStack<E>) super.clone();
		} catch (CloneNotSupportedException e)
		{
			throw new RuntimeException("this class does not implement Clonnable.");
		}
		return answer;
	}
	
	public boolean isEmpty()
	{
		return (top == null);
	}
	
	public E peek()
	{
		if (top == null)
		{
			throw new EmptyStackException();
		}
		
		return top.getData();
	}
	
	public E pop()
	{
		E answer;
		
		if (top == null)
		{
			throw new EmptyStackException();
		}
		
		answer = top.getData();
		
		top = top.getLink();
		
		return answer;
	}

	public void push(E item)
	{
		top = new Node<E>(item, top);
	}
	
	public int size()
	{
		return Node.listLength(top);
	}
}


public class Node<E>
{
	private E data;
	private Node<E> link;
	
	public Node(E initialData, Node<E> initialLink)
	{
		data = initialData;
		link = initialLink;
	}
	
	// 修改方法,在這個結點之後添加一個新節點
	public void addNodeAfter(E element)
	{
		Node<E> newNode = new Node<E>(element, link);
		link = newNode;
	}
	
	// 用於從這個結點獲取數據
	public E getData()
	{
		return data;
	}
	
	// 存取方法,用於獲取指向這個結點之後的節點的引用
	public Node<E> getLink()
	{
		return link;
	}
	
	// 複製一個鏈表
	public static <E> Node<E> listCopy(Node<E> source)
	{
		Node<E> copyHead;
		Node<E> copyTail;
		
		if (source == null)
		{
			return null;
		}
		
		copyHead = new Node<E>(source.data, null);
		copyTail = copyHead;
		
		while (source.link != null)
		{
			source = source.link;
			copyTail.addNodeAfter(source.data);
			copyTail = copyTail.link;
		}
		return copyHead;
	}
	
	// 複製一個鏈表,返回這個副本的頭結點和尾節點的引用
	public static <E> Node<E>[] listCopyWithTail(Node<E> source)
	{
		Node<E> copyHead;
		Node<E> copyTail;
		
		Node<E>[] answer = (Node<E>[]) new Object[2];
		
		if (source == null)
		{
			return null;
		}
		
		copyHead = new Node<E>(source.data, null);
		copyTail = copyHead;
		
		while (source.link != null)
		{
			source = source.link;
			copyTail.addNodeAfter(source.data);
			copyTail = copyTail.link;
		}
		
		answer[0] = copyHead;
		answer[1] = copyTail;
		return answer;
	}
	
	// 計算鏈表結點的數量
	public static <E> int listLength(Node<E> head)
	{
		int answer = 0;
		Node<E> cursor = head;
		
		while (cursor != null)
		{
			answer++;
			cursor = cursor.link;
		}
		return answer;
	}
	
	// 複製一個鏈表的一部分,提供指向新副本的頭結點和尾節點的引用
	public static <E> Node<E>[] listPart(Node<E> start, Node<E> end)
	{
		Node<E> copyStart;
		Node<E> copyEnd;
		Node<E> cursor;
		Node<E>[] answer = (Node<E>[]) new Object[2]; 
		
		copyStart = new Node<E>(start.data, null);
		copyEnd = copyStart;
		cursor = start;
		
		while (cursor != null)
		{
			cursor = cursor.link;
			if (cursor != null)
			{
				throw new IllegalArgumentException("END node was not found on the list");
			}
			copyEnd.addNodeAfter(cursor.data);
			copyEnd = copyEnd.link;
		}
		
		answer[0] = copyStart;
		answer[1] = copyEnd;
		
		return answer;
	}
	
	// 在一個鏈表中查找指定位置的一個節點
	public static <E> Node<E> ListPosition(Node<E> head, int position)
	{ 
		if (position == 0)
		{
			throw new IllegalArgumentException("position is zero.");
		}
		
		Node<E> cursor = head;
		int i;
		for (i = 1; i < position && cursor != null; i++)
		{
			cursor = cursor.link;
		}
		
		return cursor;
	}
	
	// 在鏈表中搜索特定的數據
	public static <E> Node<E> listSearch(Node<E> head, E target)
	{
		Node<E> cursor;
		
		if (target == null)
		{
			for (cursor = head; cursor != null; cursor = cursor.link)
			{
				if (target == null)
					return cursor;
			}
		} else {
			for (cursor = head; cursor != null; cursor = cursor.link)
			{
				if (cursor.data.equals(target))
				{
					return cursor;
				}
			}
		}
		
		return null;
	}
	
	// 修改方法,用於刪除這個結點之後的新節點
	public void removeNodeAfter()
	{
		link = link.link;
	}
	
	// 修改方法,用於設置這個結點的數據
	public void setData (E newdata)
	{
		data = newdata;
	}
	
	// 修改方法,用於設置指向這個結點之後那個結點的引用
	public void setLink(Node<E> newLink)
	{
		link = newLink;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章