Skip to content

Commit 1b1afaf

Browse files
authored
Create MyStack.java
1 parent 4457b84 commit 1b1afaf

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

MyStack.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* @Name: My Stack Implementation In Java
3+
* @Author: Max Base
4+
* @Date: 2022-10-31
5+
* @Class: Data Structure, Dr. Mahsa Soheil Shamaee
6+
*/
7+
8+
// Stack
9+
class MyStack {
10+
private int size;
11+
private int top;
12+
private int[] stack;
13+
14+
// Constructor
15+
public MyStack(int size) {
16+
this.size = size;
17+
this.top = -1;
18+
this.stack = new int[size];
19+
}
20+
21+
// O(1)
22+
// Push an element to the top of the stack and return true
23+
// If the stack is full, throw an error and return false
24+
public boolean push(int value) {
25+
if (top == size - 1) {
26+
System.out.println("Stack is full");
27+
return false;
28+
} else {
29+
top++;
30+
stack[top] = value;
31+
return true;
32+
}
33+
}
34+
35+
// O(1)
36+
// return -1 if stack is empty
37+
// return top value if stack is not empty
38+
// remove top value
39+
public int pop() {
40+
if (top == -1) {
41+
System.out.println("Stack is empty");
42+
return -1;
43+
} else {
44+
int value = stack[top];
45+
top--;
46+
return value;
47+
}
48+
}
49+
50+
// O(1)
51+
// return -1 if stack is empty
52+
// return top value if stack is not empty
53+
// return top value without removing it
54+
public int peek() {
55+
if (top == -1) {
56+
System.out.println("Stack is empty");
57+
return -1;
58+
} else {
59+
return stack[top];
60+
}
61+
}
62+
63+
// O(1)
64+
public boolean isEmpty() {
65+
return top == -1;
66+
}
67+
68+
// O(1)
69+
public boolean isFull() {
70+
return top == size - 1;
71+
}
72+
73+
// O(1)
74+
public int size() {
75+
return top + 1;
76+
}
77+
78+
public boolean resize(int newSize) {
79+
if (newSize < size) {
80+
System.out.println("New size is smaller than current size");
81+
return false;
82+
} else {
83+
int[] newStack = new int[newSize];
84+
for (int i = 0; i < size; i++) {
85+
newStack[i] = stack[i];
86+
}
87+
stack = newStack;
88+
size = newSize;
89+
return true;
90+
}
91+
}
92+
93+
// O(n)
94+
public void printStack() {
95+
if (top == -1) {
96+
System.out.println("Stack is empty");
97+
} else {
98+
System.out.println("Stack items:");
99+
for (int i = top; i >= 0; i--) {
100+
System.out.println(stack[i]);
101+
}
102+
}
103+
}
104+
}
105+
106+
// Example
107+
public class HelloWorld {
108+
public static void main(String []args){
109+
System.out.println("Hello, World!");
110+
111+
MyStack stack = new MyStack(5);
112+
stack.push(1);
113+
stack.push(2);
114+
stack.push(3);
115+
stack.push(4);
116+
stack.push(5);
117+
118+
stack.printStack();
119+
120+
System.out.println("Peek: " + stack.peek());
121+
System.out.println("Pop: " + stack.pop());
122+
System.out.println("Peek: " + stack.peek());
123+
124+
stack.printStack();
125+
}
126+
}

0 commit comments

Comments
 (0)