-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
56 lines (47 loc) · 1.18 KB
/
Copy pathStack.java
File metadata and controls
56 lines (47 loc) · 1.18 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
public class Stack {
private int[] element;
private int top;
public Stack() {
element = new int[10];
top = -1;
}
//overriding constructor with size
public Stack(int size) {
element = new int[size];
top = -1;
}
public void push(int data) {
if (!isFull()) //insert data if stack is not full
element[++top] = data;
else
System.out.println("Can't push data, stack is full.");
}
//return the top data from stack without removing
public int top() {
return element[this.top];
}
//pop element from top of the stack
public void pop() {
if (!isEmpty())
top--;
else
System.out.println("Stack is already empty.");
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == element.length - 1);
}
public int getLength() {
return top + 1;
}
//printer method
public void print() {
if (!isEmpty()) {
for (int i=0;i<=top;i++)
System.out.print(element[i]+" ");
}
System.out.println();
}
}