Java实现数据结构与算法-顺序栈与链表栈

顺序栈结构:序号为零的元素就是栈底
链式栈结构:链表首部为栈顶,链表尾部为栈底
入栈,出栈(push,pop)先进先出(First In First Out)

1.顺序栈

/**
 * @author Hash.Zhang
 *
 */
public class SequentialStack {
	public int MAXLEN=50;
	public Data data[]=new Data[MAXLEN];
	private int top;
	/**
	 * constructor for create a new empty stack
	 */
	public SequentialStack()
	{
		this.top=-1;
	}
	/**
	 * constructor for create a new empty stack with size
	 */
	public SequentialStack(int size)
	{
		this.top=-1;
		MAXLEN=size;
		data=new Data[MAXLEN];
	}
	/**
	 * SequentialStackIsFull
	 * @return true for a full stack, false for others
	 */
	public boolean SequentialStackIsFull()
	{
		return (this.top>=MAXLEN);
	}
	/**
	 * SequentialStackIsEmpty
	 * @return true for an empty stack, false for others
	 */
	public boolean SequentialStackIsEmpty()
	{
		return (this.top<0);
	}
	/**
	 * SequentialStackClear
	 * Clear the whole stack
	 */
	public void SequentialStackClear()
	{
		this.top=-1;
	}
	/**
	 * PushSequentialStack
	 * Push a new data to the stack
	 */
	public boolean PushSequentialStack(Data data)
	{
		if((this.top+1)>=MAXLEN)
		{
			System.out.println("The stack is overflow!");
			return false;
		}
		this.data[++this.top]=data;
		return true;
	}
	/**
	 * PopSequentialStack
	 * Pop an element out of the stack
	 */
	public Data PopSequentialStack()
	{
		if(SequentialStackIsEmpty())
		{
			System.out.println("The stack is empty!");
			return null;
		}
		return this.data[this.top--];
	}
	/**
	 * PeekSequentialStack
	 * return the top element of the stack
	 */
	public Data PeekSequentialStack()
	{
		if(SequentialStackIsEmpty())
		{
			System.out.println("The stack is empty!");
			return null;
		}
		return this.data[this.top];
	}
	/**
	 * Main method to test
	 */
	public static void main(String[] args) {
		SequentialStack SeStack=new SequentialStack(10);
		for(int i=0;i<11;i++)
			SeStack.PushSequentialStack(new Data(i,"Hash"+i));
		System.out.println(SeStack.PopSequentialStack().toString()+SeStack.PeekSequentialStack().toString());
		for(int i=0;i<11;i++)
			System.out.println(SeStack.PopSequentialStack());
	}
}
public class Data {
<span style="white-space:pre">	</span>int SequenceNo;
<span style="white-space:pre">	</span>String ID;
<span style="white-space:pre">	</span>public Data()
<span style="white-space:pre">	</span>{}
<span style="white-space:pre">	</span>public Data(int a)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(int a,String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public String toString()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>return SequenceNo+ID;
<span style="white-space:pre">	</span>}
}

输出结果:

The stack is overflow!
9Hash98Hash8
8Hash8
7Hash7
6Hash6
5Hash5
4Hash4
3Hash3
2Hash2
1Hash1
0Hash0
The stack is empty!
null
The stack is empty!
null

2. 链式栈(用到了之前文章“Java实现数据结构与算法-链表”的主类LinkedList )

/**
 * @author Hash.Zhang
 *
 */
public class LinkedListStack {
	public int count=0;
	public LinkedList top=new LinkedList();
	/**
	 * LinkedListStackIsEmpty
	 * @return true for an empty stack, false for others
	 */
	public boolean LinkedListStackIsEmpty()
	{
		return (this.count<=0);
	}
	/**
	 * LinkedListStackClear
	 * Clear the whole stack
	 */
	public void LinkedListStackClear()
	{
		this.count=0;
		this.top=new LinkedList();
		System.out.println("Stack Cleared!");
	}
	/**
	 * PushLinkedListStack
	 * Push a new data to the stack
	 */
	public boolean PushLinkedListStack(Data data)
	{
		top.LinkedListAddFirst(top, data);
		this.count++;
		return true;
	}
	/**
	 * PopLinkedListStack
	 * Pop an element out of the stack
	 */
	public Data PopLinkedListStack()
	{
		LinkedList p=top.NextNode;
		top.LinkedListDeleteNode(top,p.NodeData.ID);
		this.count--;
		return p.NodeData;
	}
	/**
	 * PeekLinkedListStack
	 * return the top element of the stack
	 */
	public Data PeekLinkedListStack()
	{
		LinkedList p=top.NextNode;
		return p.NodeData;
	}
	/**
	 * main method to test Linked List Stack
	 */
	public static void main(String[] args) {
		LinkedListStack LLStack=new LinkedListStack();
		for(int i=0;i<11;i++)
			LLStack.PushLinkedListStack(new Data(i,"Hash"+i));
		for(int i=0;i<11;i++)
			System.out.println(LLStack.PopLinkedListStack().toString()+" "+LLStack.count+" left! The next is"+LLStack.PeekLinkedListStack().toString());
	}

}
public class Data {
<span style="white-space:pre">	</span>int SequenceNo;
<span style="white-space:pre">	</span>String ID;
<span style="white-space:pre">	</span>public Data()
<span style="white-space:pre">	</span>{}
<span style="white-space:pre">	</span>public Data(int a)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(int a,String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public String toString()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>return SequenceNo+ID;
<span style="white-space:pre">	</span>}
}

输出结果:

10Hash10 10 left! The next is9Hash9
9Hash9 9 left! The next is8Hash8
8Hash8 8 left! The next is7Hash7
7Hash7 7 left! The next is6Hash6
6Hash6 6 left! The next is5Hash5
5Hash5 5 left! The next is4Hash4
4Hash4 4 left! The next is3Hash3
3Hash3 3 left! The next is2Hash2
2Hash2 2 left! The next is1Hash1
1Hash1 1 left! The next is0Hash0
Exception in thread "main" java.lang.NullPointerException
at LinkedListStack.PeekLinkedListStack(LinkedListStack.java:54)
at LinkedListStack.main(LinkedListStack.java:64)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值