关闭

【数据结构】栈的链表实现

标签: 数据结构
194人阅读 评论(0) 收藏 举报
分类:
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;
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15042次
    • 积分:390
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:12篇
    • 译文:0篇
    • 评论:1条