本文已参与「新人创作礼」活动,一起开启掘金创作之路。
题目:使用链表建立一个栈
代码
public class MyStack<Item> {
/**
* Pointer to top of the stack
*/
private StackNode<Item> top;
/**
* Keeps track of the size of the stack
*/
private int size;
/**
* Default constructor of the class initializes all
* parameters to default values
*/
public MyStack() {
//TODO implement MyStack
this.top=new StackNode();
this.size=0;
}
/**
* @return if the stack is empty or not
*/
public boolean isEmpty() {
//TODO implement isEmpty
if(this.size==0){
return true;
}
return false;
}
/**
* Pushes a new a new value into the stack
* Remember to update size
*
* @param value the value to be pushed
*/
public void push(Item value) {
//TODO implement push
StackNode node=new StackNode(value);
node.next=top;
top=node;
size=size+1;
}
/**
* Peeks the top element of the stack
*
* @return the value at the top of the stack
* @throws EmptyStackException if the stack is empty.
* You can throw an exception by “throw new EmptyStackException();”
*/
public Item peek() throws EmptyStackException {
//TODO implement peek
if(size==0)
{
EmptyStackException e = new EmptyStackException();
throw e;
//return null;
}
else{
return top.value;
}
}
/**
* Pops the top element of the stack
* Remember to update size
*
* @return the popped element
* @throws EmptyStackException if the stack is empty
* You can throw an exception by “throw new EmptyStackException();”
*/
public Item pop() throws EmptyStackException {
//TODO implement pop
if(size==0)
{
EmptyStackException e = new EmptyStackException();
throw e;
}
else{
size=size-1;
Item temp=top.value;
top=top.next;
return temp;
}
}
/**
* @return the size of the stack
*/
public int getSize() {
//TODO implement getSize
return size;
//return Integer.MIN_VALUE;
}
}
题目:使用栈验证一个表达式是否有效
代码
/**
* Checks whether the expression is valid or not
*
* @return true if the expression is valid
*/
public boolean isValid() {
//TODO: complete isValid
MyStack operand=new MyStack(); //操作数
MyStack operator=new MyStack(); //运算符
int len=this.expression.length();
for(int i=0;i<len;i++){
char c=this.expression.charAt(i);
int ascii=Integer.valueOf(c);
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='('){
operator.push(c);
}
else if(c==')'){
while(true){
try {
char c2= (char) operator.peek();
if(c2=='(')
{operator.pop();break;}
operator.pop();
}catch(EmptyStackException e)
{
return false;
}
try {
operand.pop();
operand.pop();
}catch(EmptyStackException e)
{
return false;
}
operand.push('c');
}
}
else if(c==' '){
continue;
}
else if(ascii>96&&ascii<123){
operand.push(c);
}
else{return false;}
}
while(operator.getSize()!=0){
try {
operator.pop();
}catch(EmptyStackException e)
{
return false;
}
try {
operand.pop();
operand.pop();
}catch(EmptyStackException e)
{
return false;
}
operand.push('c');
}
if(operator.getSize()==0&&operand.getSize()==1){
return true;
}
return false;
}
题目:建立表达式树
代码
/**
* Makes an expression tree of the expression
*
* @return the root of the expression tree
*/
public TreeNode makeTree() {
//TODO complete makeTree
MyStack operand=new MyStack(); //操作数
MyStack operator=new MyStack(); //运算符
int len=this.expression.length();
for(int i=0;i<len;i++){
char c=this.expression.charAt(i);
int ascii=Integer.valueOf(c);
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='('){
TreeNode node1=new TreeNode(c);
operator.push(node1);
}
else if(ascii>96&&ascii<123){
TreeNode node1=new TreeNode(c);
operand.push(node1);
}
else if(c==')') {
try {
char c2= ((TreeNode)operator.pop()).value;
while(c2!='(')
{
TreeNode node2=new TreeNode(c2);
node2.right=(TreeNode) operand.pop();
node2.left=(TreeNode) operand.pop();
operand.push(node2);
c2=((TreeNode)operator.pop()).value;
}
}catch(EmptyStackException e)
{
return null;
}
}
}
while(operator.getSize()!=0){
try {
char c2= ((TreeNode) operator.pop()).value;
TreeNode node2=new TreeNode(c2);
node2.right=(TreeNode) operand.pop();
node2.left=(TreeNode) operand.pop();
operand.push(node2);
} catch(EmptyStackException e)
{
return null;
}
}
try {
return (TreeNode) operand.pop();
} catch (EmptyStackException e) {
e.printStackTrace();
}
return null;
}