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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值