最后更新时间:2007-10-12 关键字: 堆栈/线性表
java 代码
-
-
-
- package link;
-
-
-
-
-
- public class Node {
-
-
-
-
-
-
-
- public int data;
- public Node next;
-
- public Node(int data) {
- this.data=data;
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java 代码
-
-
-
-
-
-
- package link;
-
-
-
-
- public class Stack {
-
- private Node head;
-
-
-
-
-
-
- public boolean push(int data) {
- Node node = new Node(data);
- if(head==null) {
- head = node;
- return true;
- }
-
- node.next = head;
-
- head = node;
- return true;
- }
-
-
-
-
-
-
- public int pop() {
- int data = head.data;
- head = head.next;
- return data;
- }
-
- public static void main(String[] args) {
- Stack stack=new Stack();
- stack.push(1);
- stack.push(2);
- stack.push(3);
- stack.push(4);
- stack.push(5);
- stack.push(6);
- while(stack.head!=null)
- {
- System.out.println(stack.pop());
- }
- }
- }