-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackPractise.java
More file actions
74 lines (65 loc) · 1.97 KB
/
StackPractise.java
File metadata and controls
74 lines (65 loc) · 1.97 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
// Easy version of above solution
public class StackPractise {
private int[] arr;
private int top;
private int capacity;
// creating stack
public StackPractise(int size) {
arr = new int[size];
top = -1;
capacity = size;
}
// checking the stack is full
public boolean isFull() {
return (top == capacity - 1);
}
// checking the stack is empty
public boolean isEmpty() {
return (top == -1);
}
// adding elements into the stack
public void pushElement(int i) {
if (isFull()) {
System.out.println("Stack is full");
return;
}
top++;
arr[top] = i;
}
// deleting elements into the stack
public int popElement() {
if (isEmpty()) {
System.out.println("Stack is Empty");
return -1;
}
return arr[top--];
}
// calculating the size of the stack
public int sizeOfStack() {
return top + 1;
}
// printing the stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
StackPractise stackPractise = new StackPractise(5);
System.out.println("Size of the Stack: " + stackPractise.sizeOfStack());
stackPractise.pushElement(12);
stackPractise.pushElement(24);
stackPractise.pushElement(36);
System.out.print("Print the Stack: ");
stackPractise.printStack();
stackPractise.popElement();
System.out.print("Print the Stack: ");
stackPractise.printStack();
System.out.println("Size of the Stack: " + stackPractise.sizeOfStack());
stackPractise.popElement();
stackPractise.popElement();
System.out.println("Is Stack Empty: " + stackPractise.isEmpty());
System.out.println("Is Stack Full: " + stackPractise.isFull());
}
}