Merge pull request #457 from shubhamtewari/master

Good Work!
This commit is contained in:
Christian Bender 2018-07-21 20:41:07 +02:00 committed by GitHub
commit 058b99048f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@ class StackOfLinkedList {
stack.push(2); stack.push(2);
stack.push(3); stack.push(3);
stack.push(4); stack.push(4);
stack.push(5);
stack.printStack(); stack.printStack();
@ -23,6 +24,8 @@ class StackOfLinkedList {
stack.pop(); stack.pop();
stack.pop(); stack.pop();
System.out.println("Top element of stack currently is: " + stack.peek());
} }
} }
@ -75,12 +78,20 @@ class LinkedListStack {
System.out.println("Popped element is: " + temp.data); System.out.println("Popped element is: " + temp.data);
} }
public int peek() {
if (getSize() == 0) {
return -1;
}
return head.data;
}
public void printStack() { public void printStack() {
Node temp = head; Node temp = head;
System.out.println("Stack is printed as below: "); System.out.println("Stack is printed as below: ");
while (temp != null) { while (temp != null) {
System.out.print(temp.data + " "); System.out.println(temp.data + " ");
temp = temp.next; temp = temp.next;
} }
System.out.println(); System.out.println();