-
Notifications
You must be signed in to change notification settings - Fork 1
/
StackT.java
54 lines (43 loc) · 954 Bytes
/
StackT.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
package com.stack;
public class StackT implements StackI{
int[] arr = new int[5];
int top = 0;
@Override
public void push(int data) {
// TODO Auto-generated method stub
arr[top] = data;
top++;
}
@Override
public int pop() throws Exception {
// TODO Auto-generated method stub
System.out.println("Stack Size : "+arr.length);
if (empty() == true) {
throw new Exception("Stack is Empty You can't pop the elements");
}
top--;
return arr[top];
}
@Override
public int peek() {
System.out.println(arr.length);
return arr[--top];
}
@Override
public boolean empty() {
if(-1 < arr.length)
return false;
System.out.println("Stack is Empty");
return true;
}
@Override
public int search(int value) {
// TODO Auto-generated method stub
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
}