stack的refference的java实现
1package stack;
2
3/**
4 * This is stack interface
5 *
6 * @author
7 * @version 1.0
8 */
9public interface IStack
10{
11 /**
12 * Method that will return the last element in the stack, if there is no
13 * element left, it will return null
14 *
15 * @return object, the last element in the stack
16 */
17 public Object peek();
18
19 /**
20 * Method that will return the last element in the stack and also pull the
21 * element out of the stack, if there is no element left, it will return
22 * null
23 *
24 * @return object, the last element in the stack
25 */
26 public Object pop();
27
28 /**
29 * Method that will push the element specified by the parameter into the
30 * stack
31 *
32 * @param object, the element that will be push into the stack
33 */
34 public void push(Object object);
35
36 /**
37 * Method that will check if the stack is empty, if it is empty,it will
38 * return true,else it will return false
39 *
40 * @return boolean that indicates if the stack is empty or not
41 */
42 public boolean isEmpty();
43}
44
2
3/**
4 * This is stack interface
5 *
6 * @author
7 * @version 1.0
8 */
9public interface IStack
10{
11 /**
12 * Method that will return the last element in the stack, if there is no
13 * element left, it will return null
14 *
15 * @return object, the last element in the stack
16 */
17 public Object peek();
18
19 /**
20 * Method that will return the last element in the stack and also pull the
21 * element out of the stack, if there is no element left, it will return
22 * null
23 *
24 * @return object, the last element in the stack
25 */
26 public Object pop();
27
28 /**
29 * Method that will push the element specified by the parameter into the
30 * stack
31 *
32 * @param object, the element that will be push into the stack
33 */
34 public void push(Object object);
35
36 /**
37 * Method that will check if the stack is empty, if it is empty,it will
38 * return true,else it will return false
39 *
40 * @return boolean that indicates if the stack is empty or not
41 */
42 public boolean isEmpty();
43}
44
1package stack;
2
3import list.*;
4
5/**
6 * This class is the implementation of IStack interface
7 *
8 * @author
9 * @version 1.0
10 */
11public class Stack implements IStack
12{
13 /**
14 * Attributes
15 */
16 private DoublyLinkedList dll;
17
18 /**
19 * Constructor
20 */
21 public Stack()
22 {
23 this.dll = new DoublyLinkedList();
24 }
25
26 /* (non-Javadoc)
27 * @see stack.IStack#peek()
28 */
29 public Object peek()
30 {
31 if(this.dll.isEmpty())
32 {
33 return null;
34 }
35 else
36 {
37 return this.dll.get(this.dll.size() - 1);
38 }
39 }
40
41 /* (non-Javadoc)
42 * @see stack.IStack#pop()
43 */
44 public Object pop()
45 {
46 if(this.dll.isEmpty())
47 {
48 return null;
49 }
50 else
51 {
52 Object object = this.dll.get(this.dll.size() - 1);
53 this.dll.remove(this.dll.size() - 1);
54 return object;
55 }
56 }
57
58 /* (non-Javadoc)
59 * @see stack.IStack#push(java.lang.Object)
60 */
61 public void push(Object object)
62 {
63 this.dll.add(object);
64 }
65
66 /* (non-Javadoc)
67 * @see stack.IStack#isEmpty()
68 */
69 public boolean isEmpty()
70 {
71 return dll.isEmpty();
72 }
73}
74
2
3import list.*;
4
5/**
6 * This class is the implementation of IStack interface
7 *
8 * @author
9 * @version 1.0
10 */
11public class Stack implements IStack
12{
13 /**
14 * Attributes
15 */
16 private DoublyLinkedList dll;
17
18 /**
19 * Constructor
20 */
21 public Stack()
22 {
23 this.dll = new DoublyLinkedList();
24 }
25
26 /* (non-Javadoc)
27 * @see stack.IStack#peek()
28 */
29 public Object peek()
30 {
31 if(this.dll.isEmpty())
32 {
33 return null;
34 }
35 else
36 {
37 return this.dll.get(this.dll.size() - 1);
38 }
39 }
40
41 /* (non-Javadoc)
42 * @see stack.IStack#pop()
43 */
44 public Object pop()
45 {
46 if(this.dll.isEmpty())
47 {
48 return null;
49 }
50 else
51 {
52 Object object = this.dll.get(this.dll.size() - 1);
53 this.dll.remove(this.dll.size() - 1);
54 return object;
55 }
56 }
57
58 /* (non-Javadoc)
59 * @see stack.IStack#push(java.lang.Object)
60 */
61 public void push(Object object)
62 {
63 this.dll.add(object);
64 }
65
66 /* (non-Javadoc)
67 * @see stack.IStack#isEmpty()
68 */
69 public boolean isEmpty()
70 {
71 return dll.isEmpty();
72 }
73}
74