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
/**
* @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)