Skip to content

Commit

Permalink
added peek() method and stack is now printed vertically
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamtewari committed Jul 9, 2018
1 parent 595cc8f commit b748b42
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Data Structures/Stacks/StackOfLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void main(String[] args) {
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);

stack.printStack();

Expand All @@ -23,6 +24,8 @@ public static void main(String[] args) {
stack.pop();
stack.pop();

System.out.println("Top element of stack currently is: " + stack.peek());

}

}
Expand Down Expand Up @@ -75,12 +78,20 @@ public void pop() {
System.out.println("Popped element is: " + temp.data);
}

public int peek() {
if (getSize() == 0) {
return -1;
}

return head.data;
}

public void printStack() {

Node temp = head;
System.out.println("Stack is printed as below: ");
while (temp != null) {
System.out.print(temp.data + " ");
System.out.println(temp.data + " ");
temp = temp.next;
}
System.out.println();
Expand All @@ -94,5 +105,5 @@ public boolean isEmpty() {
public int getSize() {
return size;
}

}

0 comments on commit b748b42

Please sign in to comment.