-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStack.java
executable file
·77 lines (66 loc) · 1.35 KB
/
MyStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
//data structure :stack using link list
public class MyStack
{
private Node first;
private int N;//whether N is initial 0 ??
//inner class Node
private class Node
{
int item;
Node next;
}//end class Node
//check whether stack is empty
public boolean isEmpty()
{
return first==null;
}//end func isEmpty
//return the size of stack
public int size()
{
return N;
}//end func size
//pop item from stack
public int pop()
{
//pop preconditon
assert isEmpty(): "The stack is empty";
int item =first.item;
first =first.next;
N--;
//pop post condition
assert inv(): "the size of stack is disqualified";
return item;
}//end func pop
//push item into stack
public void push(int item)
{
assert inv(): "the size of stack is disqualified";
Node oldfirst =first;
first =new Node();
first.item= item;
first.next=oldfirst;
assert inv(): "the size of stack is disqualified";
N++;
}//end func push
//test the size of stack is greater than zero
private boolean inv()
{
return (this.size() >=0);
}//end func inv
//test main function
public static void main(String[] args)
{
MyStack s =new MyStack();
//insert 5 number test push
for(int i=0;i<5;i++)
{
s.push(i);
}
//test pop()
for(int i=0;i<5;i++)
{
System.out.println(s.pop());
}
}//end func main
}//end class MyStack