From d60f836861fb1d1237935a9c342ef492d0df8cc4 Mon Sep 17 00:00:00 2001 From: Ojas Saxena <43749506+ojasiiitd@users.noreply.github.com> Date: Mon, 4 Mar 2019 15:05:15 +0530 Subject: [PATCH 1/2] Updated StackOfLinkedList.java I made the code shorter and less prone to mistakes by removing the "size" variable altogether from the LinkedList class. I found that after each push/pop operation, changing the value of size did make the code more efficient, but made it more prone to mistakes. So, for empty stack, a simple "head == null" was enough, which has been incorporated. --- DataStructures/Stacks/StackOfLinkedList.java | 37 +++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/DataStructures/Stacks/StackOfLinkedList.java b/DataStructures/Stacks/StackOfLinkedList.java index 35052457fe1c..7b744bcb85af 100644 --- a/DataStructures/Stacks/StackOfLinkedList.java +++ b/DataStructures/Stacks/StackOfLinkedList.java @@ -25,9 +25,7 @@ public static void main(String[] args) { stack.pop(); System.out.println("Top element of stack currently is: " + stack.peek()); - } - } // A node class @@ -51,11 +49,10 @@ public Node(int data) { class LinkedListStack { Node head = null; - int size = 0; public void push(int x) { Node n = new Node(x); - if (getSize() == 0) { + if (head == null) { head = n; } else { @@ -63,47 +60,55 @@ public void push(int x) { n.next = temp; head = n; } - size++; } public void pop() { - if (getSize() == 0) { + if (head == null) { System.out.println("Empty stack. Nothing to pop"); } Node temp = head; head = head.next; - size--; - System.out.println("Popped element is: " + temp.data); } public int peek() { - if (getSize() == 0) { + if (head == null) { return -1; } - return head.data; } public void printStack() { - Node temp = head; System.out.println("Stack is printed as below: "); while (temp != null) { - System.out.println(temp.data + " "); + if(temp.next == null) { + System.out.print(temp.data); + } + else { + System.out.print(temp.data + " -> "); + } temp = temp.next; } System.out.println(); - } public boolean isEmpty() { - return getSize() == 0; + return head == null; } public int getSize() { - return size; + if(head == null) + return 0; + else { + int size = 1; + Node temp = head; + while(temp.next != null) { + temp = temp.next; + size++; + } + return size; + } } - } From 2ea534000bafe95a5c03dd6248aeda1d1ff151a7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 5 Mar 2019 10:18:20 +0800 Subject: [PATCH 2/2] Update StackOfLinkedList.java --- DataStructures/Stacks/StackOfLinkedList.java | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/DataStructures/Stacks/StackOfLinkedList.java b/DataStructures/Stacks/StackOfLinkedList.java index 7b744bcb85af..d9f737040271 100644 --- a/DataStructures/Stacks/StackOfLinkedList.java +++ b/DataStructures/Stacks/StackOfLinkedList.java @@ -1,7 +1,5 @@ /** - * * @author Varun Upadhyay (https://github.com/varunu28) - * */ // An implementation of a Stack using a Linked List @@ -42,7 +40,7 @@ public Node(int data) { /** * A class which implements a stack using a linked list - * + *

* Contains all the stack methods : push, pop, printStack, isEmpty **/ @@ -54,8 +52,7 @@ public void push(int x) { Node n = new Node(x); if (head == null) { head = n; - } - else { + } else { Node temp = head; n.next = temp; head = n; @@ -73,20 +70,19 @@ public void pop() { } public int peek() { - if (head == null) { - return -1; - } - return head.data; + if (head == null) { + return -1; + } + return head.data; } public void printStack() { Node temp = head; System.out.println("Stack is printed as below: "); while (temp != null) { - if(temp.next == null) { + if (temp.next == null) { System.out.print(temp.data); - } - else { + } else { System.out.print(temp.data + " -> "); } temp = temp.next; @@ -99,12 +95,12 @@ public boolean isEmpty() { } public int getSize() { - if(head == null) + if (head == null) return 0; else { int size = 1; Node temp = head; - while(temp.next != null) { + while (temp.next != null) { temp = temp.next; size++; }