mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
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.
This commit is contained in:
@ -25,9 +25,7 @@ class StackOfLinkedList {
|
|||||||
stack.pop();
|
stack.pop();
|
||||||
|
|
||||||
System.out.println("Top element of stack currently is: " + stack.peek());
|
System.out.println("Top element of stack currently is: " + stack.peek());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A node class
|
// A node class
|
||||||
@ -51,11 +49,10 @@ class Node {
|
|||||||
class LinkedListStack {
|
class LinkedListStack {
|
||||||
|
|
||||||
Node head = null;
|
Node head = null;
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
public void push(int x) {
|
public void push(int x) {
|
||||||
Node n = new Node(x);
|
Node n = new Node(x);
|
||||||
if (getSize() == 0) {
|
if (head == null) {
|
||||||
head = n;
|
head = n;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -63,47 +60,55 @@ class LinkedListStack {
|
|||||||
n.next = temp;
|
n.next = temp;
|
||||||
head = n;
|
head = n;
|
||||||
}
|
}
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pop() {
|
public void pop() {
|
||||||
if (getSize() == 0) {
|
if (head == null) {
|
||||||
System.out.println("Empty stack. Nothing to pop");
|
System.out.println("Empty stack. Nothing to pop");
|
||||||
}
|
}
|
||||||
|
|
||||||
Node temp = head;
|
Node temp = head;
|
||||||
head = head.next;
|
head = head.next;
|
||||||
size--;
|
|
||||||
|
|
||||||
System.out.println("Popped element is: " + temp.data);
|
System.out.println("Popped element is: " + temp.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (getSize() == 0) {
|
if (head == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return head.data;
|
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.println(temp.data + " ");
|
if(temp.next == null) {
|
||||||
|
System.out.print(temp.data);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.print(temp.data + " -> ");
|
||||||
|
}
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return getSize() == 0;
|
return head == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
|
if(head == null)
|
||||||
|
return 0;
|
||||||
|
else {
|
||||||
|
int size = 1;
|
||||||
|
Node temp = head;
|
||||||
|
while(temp.next != null) {
|
||||||
|
temp = temp.next;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user