-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStack.java
76 lines (64 loc) · 1.49 KB
/
MyStack.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Authors: Diane Zhang, Kayla Dixon
* Project: CS321 Project 2 Assembler
* Instructor: Prof. Al Madi
*/
public class MyStack<T>{
private Node firstNode;
public MyStack(){
this.firstNode = null;
}
public void push(T d){
Node newNode = new Node(d);
if(isEmpty()){
this.firstNode = newNode;
}
else{
newNode.setNext(this.firstNode);
this.firstNode = newNode;
}
}
public T pop(){
Node toPop = this.firstNode;
this.firstNode = toPop.next;
T data = (T) toPop.data;
return data;
}
public T peek(){
Node first = this.firstNode;
return (T) first.data;
}
public boolean isEmpty(){
if (this.firstNode == null) {
return true;
}
return false;
}
public String toString(){
String str = "";
Node curNode = this.firstNode;
while (curNode != null){
str += curNode.toString();
curNode = curNode.next;
}
return str;
}
private class Node<T>{
T data;
Node prev;
Node next;
public Node(T d){
this.data = d;
this.next = null;
this.prev = null;
}
public void setNext(Node n){
this.next = n;
}
public String toString(){
String str = "";
str += data.toString() + ", ";
return str;
}
}
}