package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Queue;
import java.util.Scanner;
public class LinkedStack<Item> {
private Node first,last;
private int N;
public LinkedStack(int i) {
}
private class Node{
Item item;
Node next;
}
public boolean isEmpty(){
return first == null;
}
public int size(){
return N;
}
//在表头插入结点
public void push(Item item){
Node oldfirst = first;
first = new Node();
first.item = (Item) item;
first.next = oldfirst;
N++;
}
//从表头删除节点
public Item pop(){
Item item = first.item;
first = first.next;
N--;
return item;
}
//在表尾插入结点
public void push2(Item item){
Node oldlast = last;
last = new Node();
last.item = (Item) "not";
oldlast.next = last;
N++;
}
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>{
private Node current = first;
public boolean hasNext(){
return current != null;
}
public void remove(){
}
public Item next(){
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) throws IOException {
LinkedStack<String> stack = new LinkedStack<>(100);
System.out.println("Enter the statement:");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
String[] item = input.split(" ");
for(String s:item){
if (!s.equals("-")){
stack.push(s);
}else if (!stack.isEmpty()){
System.out.println(stack.first.toString()+"left on stack");
stack.pop();
}
}
System.out.println("The Stack:");
int number = stack.size();
while (number != 0){
System.out.print(stack.first.toString()+" ");
stack.first = stack.first.next;
number--;
}
}
}
