栈数据结构java实现
import java.util.*;

class MyStack ...{
int keyWord;
MyStack next;

public MyStack(int keyWord,MyStack next)...{
this.keyWord = keyWord;
this.next = next;
}

public MyStack(int keyWord)...{
this.keyWord = keyWord;
this.next = null;
}
}


public class StackTest ...{
MyStack sk = null;

public void push(int count)...{
MyStack q = sk;
if(sk == null)
sk = q = new MyStack(count);
else...{
//MyStack p = new MyStack(count,q);
//sk = p;
MyStack p = new MyStack(count);
p.next = q;
sk = p;
}
}

public int pop()...{
int count = 0;
if(sk != null)...{
//System.out.println(sk.keyWord);
count = sk.keyWord;
//return sk.keyWord;
MyStack q = sk;
sk = q.next;
q.next = null;
q = null;
}//end if
else...{
System.out.println("the stack is empty");
return -1;
}
//return -1;
return count;
}

public void listElements()...{
MyStack q = sk;

while(q != null)...{
System.out.println("list : "+q.keyWord);
q = q.next;
}
}

public static void main(String[] args)...{
StackTest test = new StackTest();
/**//*test.push(1);
System.out.println("list now ");
test.listElements();
System.out.println(test.pop());
System.out.println(test.pop());
test.push(2);
System.out.println("list now ");
test.listElements();
System.out.println(test.pop());
test.push(3);
test.push(4);
//System.out.println(test.pop());
//System.out.println(test.pop());
System.out.println("list now ");
test.listElements();
*/
test.push(1);
test.push(2);
test.push(3);
test.push(5);
System.out.println(test.pop());
System.out.println(test.pop());
//System.out.println(test.pop());
test.listElements();
test.push(6);
test.push(7);
test.listElements();
System.out.println(test.pop());
test.listElements();
}
}

504

被折叠的 条评论
为什么被折叠?



