import java.util.Iterator;
public class Stack<T> implements Iterable<T> {
private int n;
private Node head;
private class Node {
private T item;
private Node next;
public Node(T item, Node next) {
this.item = item;
Node nextNode = next;
}
}
public Stack() {
this.n = 0;
this.head = new Node(null, null);
}
public boolean isEmpty() {
return this.n == 0;
}
public int length() {
return this.n;
}
public void push(T t) {
Node firstNode = head.next;
Node newNode = new Node(t, null);
head.next = newNode;
newNode.next=firstNode;
n++;
}
public T pop() {
Node firstNode = head.next;
if (firstNode == null) {
return null;
}
Node secondNode = firstNode.next;
head.next = secondNode;
n--;
return firstNode.item;
}
public Iterator<T> iterator() {
return new SIterator();
}
private class SIterator implements Iterator {
private Node n;
public SIterator() {
this.n = head;
}
public boolean hasNext() {
return n.next != null;
}
public Object next() {
n = n.next;
return n.item;
}
}
}
import java.util.Iterator;
public class StackDemo1 {
public static void main(String[] args) {
String example="(((张强加油)))()(";
System.out.println(ismatching(example));
}
public static boolean ismatching(String example)
{
Stack<String> stack=new Stack<String>();
for(int i=0;i<example.length();i++)
{
String temp = example.charAt(i)+"";
if(temp.equals("("))
{
stack.push(temp);
}
else if(temp.equals(")"))
{
String pop = stack.pop();
if(pop.equals(null))
return false;
}
}
if(stack.length()!=0)
{
return false;
}
else {
return true;
}
}
}