Requirements 1. You must implement and use a Stack<E> class to solve this problem. Your class needs to implement the user-defined operations: push, pop, and peek. 2. Your Stack<E> class must be based on dynamic-linked lists as discussed in class (see Chapter 26). 3. Do not use fixed-length arrays, JDK Stack to implement your Stack class. 它的要求是上面这些。可是我不知道peek到底怎么用,还有stack<E>为什么显示的我是错误啊?
Stack<E>中的E是类型参数,你这里需要的是Character ,这样Stack<Character> stack = new Stack<Character>(); main函数中 if (isBalanced(s)) ,s都没定义啊
可是他的要求是要用stack<E>啊。 s不都定义完了吗?int s = reader.nextInt();
1. You must implement and use a Stack<E> class to solve this problem. Your class needs
to implement the user-defined operations: push, pop, and peek.
2. Your Stack<E> class must be based on dynamic-linked lists as discussed in class
(see Chapter 26).
3. Do not use fixed-length arrays, JDK Stack to implement your Stack class.
它的要求是上面这些。可是我不知道peek到底怎么用,还有stack<E>为什么显示的我是错误啊?
main函数中 if (isBalanced(s)) ,s都没定义啊
楼主 我修改的代码如下
s 你定义在while循环里面了 应该拿到while循环外面定义还有s是int 类型的 应该用其包装类 转换为String类型 才能传参 给方法 isBalanced(String s)关于泛型的问题 由于你声明的静态常量为Char类型 而后又将char类型的添加到到stack中
其泛型很明确是 Character 类型
public class test
{
private static int i;
private static char LEFTPARENT = '(';
private static char RIGHTPARENT = ')';
private static char LEFTBRACE = '{';
private static char RIGHTBRACE = '}';
private static char LEFTBRACKET = '[';
private static char RIGHTBRACKET = ']';
public static boolean isBalanced(String s)
{
Stack<Character> stack = new Stack<Character>();
for (i = 0; i < s.length(); i++)
{
if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT);
else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE);
else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET);
else if (s.charAt(i) == RIGHTPARENT)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTPARENT)
return false;
}
else if (s.charAt(i) == RIGHTBRACE)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACE)
return false;
}
else if (s.charAt(i) == RIGHTBRACKET)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACKET)
return false;
}
}
return stack.isEmpty();
}
public static void main(String args[])
{
Scanner reader = null;
try {
reader = new Scanner(new File("data.txt"));
} catch (Exception e) {
e.printStackTrace();
}
Integer s = 0;
while (reader.hasNext()){
s = reader.nextInt();
System.out.println(i);
}
;
{
if (isBalanced(s.toString()))
System.out.println("The grouping symbols in"+" \""+ s +"\" "+"match");
else
System.out.println("The grouping symbols in"+" \""+ s +"\" "+ "do not match");
System.out.println();
}
}
}
private Object[] es = null;
private final int len = 2; public MyStack() {
es = new Object[len];
} public boolean isEmpty() {
for (Object e : es) {
if (e != null) {
return false;
}
}
return true;
} private void fill(Object[] os, Object o) {
for (Object obj : os) {
obj = o;
}
} private void beforePop() {
for (Object e : es) {
if (e == null) {
return;
}
}
Object[] back = es;
es = new Object[es.length + len];
fill(es, null);
for (int i = 0; i < back.length; i++) {
es[i] = back[i];
}
} @SuppressWarnings("unchecked")
public synchronized E peek() {
if (isEmpty()) {
throw new RuntimeException("stack is emptyp");
}
return (E) es[0];
} public synchronized void push(E e) {
beforePop();
for (int i = es.length - 1; i >= 0; i--) {
if (es[i] != null) {
es[i + 1] = es[i];
}
}
es[0] = e;
} @SuppressWarnings("unchecked")
public synchronized E pop() {
if (isEmpty()) {
throw new RuntimeException("stack is emptyp");
}
E ret = (E) es[0];
remove();
return ret;
} private void remove() {
for (int i = 0; i < es.length - 1; i++) {
es[i] = es[i + 1];
}
es[es.length - 1] = null;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyStack<String> t = new MyStack<String>();
// t.push("abc");
t.push("def");
t.push("xyz");
t.push("uvw");
// t.push("hij");
System.out.println(t.pop());
System.out.println(t.peek());
}}