Skip to content

Commit 7f6d961

Browse files
committed
LinkedStack
1 parent cac947e commit 7f6d961

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Stacks.LinkedStack;
2+
3+
public class Employee {
4+
5+
private String firstName;
6+
private String lastName;
7+
private int id;
8+
9+
public Employee(String firstName, String lastName, int id) {
10+
this.firstName = firstName;
11+
this.lastName = lastName;
12+
this.id = id;
13+
}
14+
15+
@Override
16+
public boolean equals(Object o) {
17+
if (this == o) return true;
18+
if (o == null || getClass() != o.getClass()) return false;
19+
20+
Employee employee = (Employee) o;
21+
22+
if (id != employee.id) return false;
23+
if (!firstName.equals(employee.firstName)) return false;
24+
return lastName.equals(employee.lastName);
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
int result = firstName.hashCode();
30+
result = 31 * result + lastName.hashCode();
31+
result = 31 * result + id;
32+
return result;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Employee{" +
38+
"firstName='" + firstName + '\'' +
39+
", lastName='" + lastName + '\'' +
40+
", id=" + id +
41+
'}';
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Stacks.LinkedStack;
2+
3+
import java.util.LinkedList;
4+
import java.util.ListIterator;
5+
6+
class LinkedStack {
7+
8+
private LinkedList<Employee> stack;
9+
10+
LinkedStack() {
11+
stack = new LinkedList<>();
12+
}
13+
14+
void push(Employee employee) {
15+
stack.push(employee);
16+
}
17+
18+
Employee pop() {
19+
return stack.pop();
20+
}
21+
22+
Employee peek() {
23+
return stack.peek();
24+
}
25+
26+
void printStack() {
27+
ListIterator<Employee> iterator = stack.listIterator();
28+
while (iterator.hasNext()) {
29+
System.out.println(iterator.next());
30+
}
31+
}
32+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Stacks.LinkedStack;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
6+
LinkedStack stack = new LinkedStack();
7+
8+
Employee adam = new Employee("Adam", "X", 1);
9+
Employee maja = new Employee("Maja", "Y", 2);
10+
Employee kuba = new Employee("Kuba", "Z", 3);
11+
Employee olga = new Employee("Olga", "O", 4);
12+
13+
stack.push(adam);
14+
stack.push(maja);
15+
stack.push(kuba);
16+
stack.push(olga);
17+
18+
stack.printStack();
19+
System.out.println("-----------------------------");
20+
21+
System.out.println(stack.peek());
22+
System.out.println("-----------------------------");
23+
24+
System.out.println("Popped: " + stack.pop());
25+
System.out.println(stack.peek());
26+
27+
System.out.println("-----------------------------");
28+
stack.printStack();
29+
}
30+
}

0 commit comments

Comments
 (0)