-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack
More file actions
81 lines (65 loc) · 1.68 KB
/
Stack
File metadata and controls
81 lines (65 loc) · 1.68 KB
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
public class Stack {
private int[] data;
private int top = 0;
public Stack() {
this.data = new int[7];
}
public Stack(int cap) {
this.data = new int[cap];
}
public boolean isEmpty() {
return this.top == 0;
}
public void push(int item) {
if (isFull()) {
throw new IllegalStateException("Stack is full");
}
this.data[this.top] = item;
this.top++;
}
public int pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
this.top--;
return this.data[this.top];
}
public boolean isFull() {
return this.top == data.length;
}
public int peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return this.data[this.top - 1];
}
public int size() {
return this.top;
}
public void display() {
for (int i = 0; i < this.top; i++) {
System.out.print(this.data[i] + " ");
}
System.out.println();
}
}
client
------------------------------------------------------------------------------------------------9-9-9-9-9-------------------------------------------------------------------------------------
public class stack_client {
public static void main(String[] args) {
Stack s = new Stack();
//s.cap = 6;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.push(60);
s.push(70);
s.display();
System.out.println(s.pop());
s.display();
s.push(22);
s.push(222);
}
}