import java.util.*;
public class Stack {
public Node top;
public int size;
public Stack()
{
top = null;
size = 0;
}
public class Node
{
public int data;
public Node next;
public Node(int value)
{data= value;}
}
public void Push(int value)
{
Node tmp = new Node(value);
tmp.next = top;
top = tmp;
size++;
}
public void Pop()
{
if(IsEmpty())
{
System.out.println("The Stack is Empty!");
}
else
{
top = top.next;
size--;
}
}
public boolean IsEmpty()
{
return size==0;
}
public void MyShow()
{
Node tmp = top;
if (IsEmpty())
{
System.out.println("The Stack is Empty!");
return;
}
System.out.println("The Stack Data is : ");
for (int i = 0;i < size; i++)
{
System.out.print(tmp.data+"\t");
tmp = tmp.next;
}
System.out.println();
}
public static void main(String args[])
{
Stack mystack = new Stack();
int inputnum;
Scanner in = new Scanner(System.in);
System.out.println("Please input the number of the stack : ");
int maxsize = in.nextInt();
for (int i=0;i<maxsize;i++)
{
System.out.println("Please input the value to be pushed : ");
inputnum = in.nextInt();
mystack.Push(inputnum);
mystack.MyShow();
}
in.close();
for (int i=0;i<maxsize;i++)
{
mystack.Pop();
mystack.MyShow();
}
}
}
栈 Java
最新推荐文章于 2024-01-21 19:33:37 发布