patternjavaModerate
Implementing a Stack in Java for a technical interview
Viewed 0 times
stackjavatechnicalinterviewforimplementing
Problem
This code is based off the Stack implementation in Chapter 3 of Cracking The Coding Interview. I modified the code to make it compile and give me the correct output. I'd appreciate any feedback on code style and correctness, assuming that I write this code in a technical interview.
The pseudocode from Cracking the Coding Interview, which is implemented using a linked list:
My code:
```
public class Stack {
Node first;
Node last;
public Stack(Node f, Node l) {
first = f;
last = l;
first.next = last;
}
public Stack() {
first.next = last;
}
public void push(Object data) {
if(first == null) {
first = new Node(data, null);
}
else {
last.next = new Node(data, null);
last = last.next;
}
}
public Object pop() {
if(first == null) {
return -1;
}
else {
Object item = last.data;
Node cur = first;
while (cur.next.next != null) {
cur = cur.next;
}
last = cur;
return item;
}
}
public Object peek() {
if(first == null) {
return -1;
}
Object item = last.data;
return item;
}
public static void main(String[] args) {
Stack stack = new Stack(new Node(1, null), new Node(2, null));
stack.push(3);
System.out.println(stack.peek() == 3);
stack.pop();
System.out.println(stack.peek() == 2);
}
private static class Node {
Object data;
N
The pseudocode from Cracking the Coding Interview, which is implemented using a linked list:
class Stack {
Node top;
Object pop() {
if (top != null) {
Node item = top.data;
top = top.next;
return item;
}
return null;
}
void push(Object item) {
Node t = new Node(item);
t.next = top;
top = t;
}
Object peek() {
return top.data;
}
}My code:
```
public class Stack {
Node first;
Node last;
public Stack(Node f, Node l) {
first = f;
last = l;
first.next = last;
}
public Stack() {
first.next = last;
}
public void push(Object data) {
if(first == null) {
first = new Node(data, null);
}
else {
last.next = new Node(data, null);
last = last.next;
}
}
public Object pop() {
if(first == null) {
return -1;
}
else {
Object item = last.data;
Node cur = first;
while (cur.next.next != null) {
cur = cur.next;
}
last = cur;
return item;
}
}
public Object peek() {
if(first == null) {
return -1;
}
Object item = last.data;
return item;
}
public static void main(String[] args) {
Stack stack = new Stack(new Node(1, null), new Node(2, null));
stack.push(3);
System.out.println(stack.peek() == 3);
stack.pop();
System.out.println(stack.peek() == 2);
}
private static class Node {
Object data;
N
Solution
A stack should not know about the first element. A stack only know about the last element that was pushed, so the
Edit: As @vnp says,
This constructor doesn't do anything and will throw a
In
In
In
One final point: you should not give your variables one character names like
Stack object itself should only have one Node called last. Because of this, the constructor should be changed as well to only take one Node ex: public Stack(Node node).Edit: As @vnp says,
Node is private, so it cannot be created outside this class. Either create a constructor which takes an Object, or don't create any constructors and always create an empty Stack.This constructor doesn't do anything and will throw a
NullPointerException:public Stack() {
first.next = last;
}In
pop(), you shouldn't need to do any fancy while loops. Just check if last is null and if not, get last's data and set last to last.next: public Object pop() {
if(last == null) {
return -1;
}
else {
Object item = last.data;
last = last.next;
return item;
}
}In
push(), you should simply set last to be the new Node, and have it point to the old last as it's next:public void push(Object data) {
last = new Node(data, last);
}In
peek(), you can change first to last and simply return last.data:public Object peek() {
if(last == null) {
return -1;
}
return last.data;
}One final point: you should not give your variables one character names like
d, n, l, and f. If the variables in your constructor should have the same name as the private member variables, then give them the same name and prefix the members with this to differentiate them. For example, you can change the Node constructor to:private Node(Object data, Node node) {
this.data = data;
this.next = node;
}Code Snippets
public Stack() {
first.next = last;
}public Object pop() {
if(last == null) {
return -1;
}
else {
Object item = last.data;
last = last.next;
return item;
}
}public void push(Object data) {
last = new Node(data, last);
}public Object peek() {
if(last == null) {
return -1;
}
return last.data;
}private Node(Object data, Node node) {
this.data = data;
this.next = node;
}Context
StackExchange Code Review Q#52558, answer score: 18
Revisions (0)
No revisions yet.