-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainStack.java
More file actions
40 lines (38 loc) · 931 Bytes
/
MainStack.java
File metadata and controls
40 lines (38 loc) · 931 Bytes
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
public class MainStack{
protected int[] data;
private static final int DEFAULT_SIZE=10;
int ptr=-1;
public MainStack(){
this(DEFAULT_SIZE);
}
public MainStack(int size){
this.data = new int[size];
}
public boolean push(int element) {
if(isfull()){
System.out.println("Stack is full");
}
ptr++;
data[ptr]=element;
return true;
}
public boolean isfull(){
return ptr==data.length;
}
public int pop() throws stackexception{
if(isempty()){
throw new stackexception("Stack is empty");
}
ptr--;
return data[ptr];
}
public boolean isempty(){
return ptr==-1;
}
public void peek() throws stackexception{
if(isempty()){
throw new stackexception("Stack is empty");
}
System.out.println(data[ptr]);
}
}