Skip to content

Commit 91229b7

Browse files
authored
Merge pull request TheAlgorithms#1081 from shellhub/dev
update StackOfLinkedList
2 parents 23d9eae + bb9e082 commit 91229b7

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

DataStructures/Stacks/StackOfLinkedList.java

+74-44
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public static void main(String[] args) {
1717
stack.push(4);
1818
stack.push(5);
1919

20-
stack.printStack();
20+
System.out.println(stack);
2121

2222
System.out.println("Size of stack currently is: " + stack.getSize());
2323

24-
stack.pop();
25-
stack.pop();
24+
assert stack.pop() == 5;
25+
assert stack.pop() == 4;
2626

2727
System.out.println("Top element of stack currently is: " + stack.peek());
2828
}
@@ -48,65 +48,95 @@ public Node(int data) {
4848

4949
class LinkedListStack {
5050

51-
Node head = null;
51+
/**
52+
* Top of stack
53+
*/
54+
Node head;
55+
56+
/**
57+
* Size of stack
58+
*/
59+
private int size;
60+
61+
/**
62+
* Init properties
63+
*/
64+
public LinkedListStack() {
65+
head = null;
66+
size = 0;
67+
}
5268

53-
public void push(int x) {
54-
Node n = new Node(x);
55-
if (head == null) {
56-
head = n;
57-
} else {
58-
Node temp = head;
59-
n.next = temp;
60-
head = n;
61-
}
69+
/**
70+
* Add element at top
71+
*
72+
* @param x to be added
73+
* @return <tt>true</tt> if add successfully
74+
*/
75+
public boolean push(int x) {
76+
Node newNode = new Node(x);
77+
newNode.next = head;
78+
head = newNode;
79+
size++;
80+
return true;
6281
}
6382

64-
public void pop() {
65-
if (head == null) {
66-
System.out.println("Empty stack. Nothing to pop");
83+
/**
84+
* Pop element at top of stack
85+
*
86+
* @return element at top of stack
87+
* @throws NoSuchElementException if stack is empty
88+
*/
89+
public int pop() {
90+
if (size == 0) {
91+
throw new NoSuchElementException("Empty stack. Nothing to pop");
6792
}
68-
69-
Node temp = head;
93+
Node destroy = head;
7094
head = head.next;
71-
System.out.println("Popped element is: " + temp.data);
95+
int retValue = destroy.data;
96+
destroy = null; // clear to let GC do it's work
97+
size--;
98+
return retValue;
7299
}
73100

101+
/**
102+
* Peek element at top of stack
103+
*
104+
* @return element at top of stack
105+
* @throws NoSuchElementException if stack is empty
106+
*/
74107
public int peek() {
75-
if (head == null) {
76-
return -1;
108+
if (size == 0) {
109+
throw new NoSuchElementException("Empty stack. Nothing to pop");
77110
}
78111
return head.data;
79112
}
80113

81-
public void printStack() {
82-
Node temp = head;
83-
System.out.println("Stack is printed as below: ");
84-
while (temp != null) {
85-
if (temp.next == null) {
86-
System.out.print(temp.data);
87-
} else {
88-
System.out.print(temp.data + " -> ");
89-
}
90-
temp = temp.next;
114+
@Override
115+
public String toString() {
116+
Node cur = head;
117+
StringBuilder builder = new StringBuilder();
118+
while (cur != null) {
119+
builder.append(cur.data).append("->");
120+
cur = cur.next;
91121
}
92-
System.out.println();
122+
return builder.replace(builder.length() - 2, builder.length(), "").toString();
93123
}
94124

125+
/**
126+
* Check if stack is empty
127+
*
128+
* @return <tt>true</tt> if stack is empty, otherwise <tt>false</tt>
129+
*/
95130
public boolean isEmpty() {
96-
return head == null;
131+
return size == 0;
97132
}
98133

134+
/**
135+
* Return size of stack
136+
*
137+
* @return size of stack
138+
*/
99139
public int getSize() {
100-
if (head == null)
101-
return 0;
102-
else {
103-
int size = 1;
104-
Node temp = head;
105-
while (temp.next != null) {
106-
temp = temp.next;
107-
size++;
108-
}
109-
return size;
110-
}
140+
return size;
111141
}
112142
}

0 commit comments

Comments
 (0)