mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Update StackOfLinkedList.java
This commit is contained in:
@ -1,7 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// An implementation of a Stack using a Linked List
|
// An implementation of a Stack using a Linked List
|
||||||
@ -42,7 +40,7 @@ class Node {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A class which implements a stack using a linked list
|
* A class which implements a stack using a linked list
|
||||||
*
|
* <p>
|
||||||
* Contains all the stack methods : push, pop, printStack, isEmpty
|
* Contains all the stack methods : push, pop, printStack, isEmpty
|
||||||
**/
|
**/
|
||||||
|
|
||||||
@ -54,8 +52,7 @@ class LinkedListStack {
|
|||||||
Node n = new Node(x);
|
Node n = new Node(x);
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
head = n;
|
head = n;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Node temp = head;
|
Node temp = head;
|
||||||
n.next = temp;
|
n.next = temp;
|
||||||
head = n;
|
head = n;
|
||||||
@ -73,20 +70,19 @@ class LinkedListStack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (head == null) {
|
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) {
|
||||||
if(temp.next == null) {
|
if (temp.next == null) {
|
||||||
System.out.print(temp.data);
|
System.out.print(temp.data);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
System.out.print(temp.data + " -> ");
|
System.out.print(temp.data + " -> ");
|
||||||
}
|
}
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
@ -99,12 +95,12 @@ class LinkedListStack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
if(head == null)
|
if (head == null)
|
||||||
return 0;
|
return 0;
|
||||||
else {
|
else {
|
||||||
int size = 1;
|
int size = 1;
|
||||||
Node temp = head;
|
Node temp = head;
|
||||||
while(temp.next != null) {
|
while (temp.next != null) {
|
||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user