Skip to content

Commit cea79a2

Browse files
authored
Merge branch 'master' into median_new_algo
2 parents c807b8e + 9dbdaf6 commit cea79a2

File tree

7 files changed

+321
-174
lines changed

7 files changed

+321
-174
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@
868868
* [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java)
869869
* [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java)
870870
* stacks
871+
* [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
871872
* [StackArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java)
872873
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
873874
* [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)

src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
package com.thealgorithms.datastructures.lists;
22

33
/**
4-
* Rotate a list
5-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4+
* The RotateSinglyLinkedLists class provides a method to rotate a singly linked list
5+
* to the right by a specified number of positions.
6+
* <p>
7+
* In a right rotation by `k` steps, each node in the list moves `k` positions to the right.
8+
* Nodes that are rotated off the end of the list are placed back at the beginning.
9+
* </p>
10+
* <p>
11+
* Example:
12+
* Given linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 2, the output will be:
13+
* 4 -> 5 -> 1 -> 2 -> 3.
14+
* </p>
15+
* <p>
16+
* Edge Cases:
17+
* <ul>
18+
* <li>If the list is empty, returns null.</li>
19+
* <li>If `k` is 0 or a multiple of the list length, the list remains unchanged.</li>
20+
* </ul>
21+
* </p>
22+
* <p>
23+
* Complexity:
24+
* <ul>
25+
* <li>Time Complexity: O(n), where n is the number of nodes in the linked list.</li>
26+
* <li>Space Complexity: O(1), as we only use a constant amount of additional space.</li>
27+
* </ul>
28+
* </p>
29+
*
30+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
631
*/
7-
832
public class RotateSinglyLinkedLists {
33+
34+
/**
35+
* Rotates a singly linked list to the right by `k` positions.
36+
*
37+
* @param head The head node of the singly linked list.
38+
* @param k The number of positions to rotate the list to the right.
39+
* @return The head of the rotated linked list.
40+
*/
941
public Node rotateRight(Node head, int k) {
1042
if (head == null || head.next == null || k == 0) {
1143
return head;

src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
import java.util.List;
55

66
/**
7-
* This class implements a GenericArrayListQueue.
7+
* This class implements a GenericArrayListQueue, a queue data structure that
8+
* holds elements of any type specified at runtime, allowing flexibility in the type
9+
* of elements it stores.
810
*
9-
* A GenericArrayListQueue data structure functions the same as any
10-
* specific-typed queue. The GenericArrayListQueue holds elements of types
11-
* to-be-specified at runtime. The elements that are added first are the first
12-
* to be removed (FIFO). New elements are added to the back/rear of the queue.
11+
* <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
12+
* elements added first are the first to be removed. New elements are added to the back
13+
* (or rear) of the queue, while removal of elements occurs from the front.
14+
*
15+
* @param <T> The type of elements held in this queue.
1316
*/
1417
public class GenericArrayListQueue<T> {
1518

1619
/**
17-
* The generic List for the queue. T is the generic element type.
20+
* A list that stores the queue's elements in insertion order.
1821
*/
1922
private final List<T> elementList = new ArrayList<>();
2023

2124
/**
2225
* Checks if the queue is empty.
2326
*
24-
* @return True if the queue is empty, false otherwise.
27+
* @return {@code true} if the queue has no elements; {@code false} otherwise.
2528
*/
26-
private boolean isEmpty() {
29+
public boolean isEmpty() {
2730
return elementList.isEmpty();
2831
}
2932

3033
/**
31-
* Returns the element at the front of the queue without removing it.
34+
* Retrieves, but does not remove, the element at the front of the queue.
3235
*
33-
* @return The element at the front of the queue, or null if the queue is empty.
36+
* @return The element at the front of the queue, or {@code null} if the queue is empty.
3437
*/
3538
public T peek() {
3639
return isEmpty() ? null : elementList.getFirst();
3740
}
3841

3942
/**
40-
* Inserts an element of type T to the back of the queue.
43+
* Inserts an element at the back of the queue.
4144
*
42-
* @param element the element to be added to the queue.
43-
* @return True if the element was added successfully.
45+
* @param element The element to be added to the queue.
46+
* @return {@code true} if the element was successfully added.
4447
*/
4548
public boolean add(T element) {
4649
return elementList.add(element);
@@ -49,7 +52,7 @@ public boolean add(T element) {
4952
/**
5053
* Retrieves and removes the element at the front of the queue.
5154
*
52-
* @return The element removed from the front of the queue, or null if the queue is empty.
55+
* @return The element removed from the front of the queue, or {@code null} if the queue is empty.
5356
*/
5457
public T poll() {
5558
return isEmpty() ? null : elementList.removeFirst();
Lines changed: 55 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,109 @@
11
package com.thealgorithms.datastructures.stacks;
22

33
/**
4-
* Implementation of a stack using nodes. Unlimited size, no arraylist.
4+
* A stack implementation using linked nodes, supporting unlimited size without an ArrayList.
55
*
6-
* @author Kyler Smith, 2017
6+
* <p>Each node in the stack contains data of generic type {@code Item}, along with references
7+
* to the next and previous nodes, supporting typical stack operations.
8+
*
9+
* <p>The stack follows a Last-In-First-Out (LIFO) order where elements added last are
10+
* removed first. Supported operations include push, pop, and peek.
11+
*
12+
* @param <Item> the type of elements held in this stack
713
*/
814
public class NodeStack<Item> {
915

1016
/**
11-
* Entry point for the program.
17+
* Node class representing each element in the stack.
1218
*/
13-
public static void main(String[] args) {
14-
NodeStack<Integer> stack = new NodeStack<Integer>();
15-
16-
stack.push(3);
17-
stack.push(4);
18-
stack.push(5);
19-
System.out.println("Testing :");
20-
stack.print(); // prints : 5 4 3
19+
private class Node {
20+
Item data;
21+
Node previous;
2122

22-
Integer x = stack.pop(); // x = 5
23-
stack.push(1);
24-
stack.push(8);
25-
Integer y = stack.peek(); // y = 8
26-
System.out.println("Testing :");
27-
stack.print(); // prints : 8 1 4 3
28-
29-
System.out.println("Testing :");
30-
System.out.println("x : " + x);
31-
System.out.println("y : " + y);
23+
Node(Item data) {
24+
this.data = data;
25+
this.previous = null;
26+
}
3227
}
3328

34-
/**
35-
* Information each node should contain.
36-
*
37-
* @value data : information of the value in the node
38-
* @value head : the head of the stack
39-
* @value next : the next value from this node
40-
* @value previous : the last value from this node
41-
* @value size : size of the stack
42-
*/
43-
private Item data;
44-
45-
private static NodeStack<?> head;
46-
private NodeStack<?> previous;
47-
private static int size = 0;
29+
private Node head; // Top node in the stack
30+
private int size; // Number of elements in the stack
4831

4932
/**
50-
* Constructors for the NodeStack.
33+
* Constructs an empty NodeStack.
5134
*/
5235
public NodeStack() {
53-
}
54-
55-
private NodeStack(Item item) {
56-
this.data = item;
36+
head = null;
37+
size = 0;
5738
}
5839

5940
/**
60-
* Put a value onto the stack.
41+
* Pushes an item onto the stack.
6142
*
62-
* @param item : value to be put on the stack.
43+
* @param item the item to be pushed onto the stack
6344
*/
6445
public void push(Item item) {
65-
NodeStack<Item> newNs = new NodeStack<Item>(item);
66-
67-
if (this.isEmpty()) {
68-
NodeStack.setHead(new NodeStack<>(item));
69-
newNs.setNext(null);
70-
newNs.setPrevious(null);
71-
} else {
72-
newNs.setPrevious(NodeStack.head);
73-
NodeStack.head.setNext(newNs);
74-
NodeStack.setHead(newNs);
75-
}
76-
77-
NodeStack.setSize(NodeStack.getSize() + 1);
46+
Node newNode = new Node(item);
47+
newNode.previous = head;
48+
head = newNode;
49+
size++;
7850
}
7951

8052
/**
81-
* Value to be taken off the stack.
53+
* Removes and returns the item at the top of the stack.
8254
*
83-
* @return item : value that is returned.
55+
* @return the item at the top of the stack, or {@code null} if the stack is empty
56+
* @throws IllegalStateException if the stack is empty
8457
*/
8558
public Item pop() {
86-
Item item = (Item) NodeStack.head.getData();
87-
88-
NodeStack.setHead(NodeStack.head.getPrevious());
89-
NodeStack.head.setNext(null);
90-
91-
NodeStack.setSize(NodeStack.getSize() - 1);
92-
93-
return item;
59+
if (isEmpty()) {
60+
throw new IllegalStateException("Cannot pop from an empty stack.");
61+
}
62+
Item data = head.data;
63+
head = head.previous;
64+
size--;
65+
return data;
9466
}
9567

9668
/**
97-
* Value that is next to be taken off the stack.
69+
* Returns the item at the top of the stack without removing it.
9870
*
99-
* @return item : the next value that would be popped off the stack.
71+
* @return the item at the top of the stack, or {@code null} if the stack is empty
72+
* @throws IllegalStateException if the stack is empty
10073
*/
10174
public Item peek() {
102-
return (Item) NodeStack.head.getData();
75+
if (isEmpty()) {
76+
throw new IllegalStateException("Cannot peek from an empty stack.");
77+
}
78+
return head.data;
10379
}
10480

10581
/**
106-
* If the stack is empty or there is a value in.
82+
* Checks whether the stack is empty.
10783
*
108-
* @return boolean : whether or not the stack has anything in it.
84+
* @return {@code true} if the stack has no elements, {@code false} otherwise
10985
*/
11086
public boolean isEmpty() {
111-
return NodeStack.getSize() == 0;
87+
return head == null;
11288
}
11389

11490
/**
115-
* Returns the size of the stack.
91+
* Returns the number of elements currently in the stack.
11692
*
117-
* @return int : number of values in the stack.
93+
* @return the size of the stack
11894
*/
11995
public int size() {
120-
return NodeStack.getSize();
96+
return size;
12197
}
12298

12399
/**
124-
* Print the contents of the stack in the following format.
125-
*
126-
* <p>
127-
* x <- head (next out) y z <- tail (first in) . . .
100+
* Prints the contents of the stack from top to bottom.
128101
*/
129102
public void print() {
130-
for (NodeStack<?> n = NodeStack.head; n != null; n = n.previous) {
131-
System.out.println(n.getData().toString());
103+
Node current = head;
104+
while (current != null) {
105+
System.out.println(current.data);
106+
current = current.previous;
132107
}
133108
}
134-
135-
private static void setHead(NodeStack<?> ns) {
136-
NodeStack.head = ns;
137-
}
138-
139-
private void setNext(NodeStack<?> next) {
140-
}
141-
142-
private NodeStack<?> getPrevious() {
143-
return previous;
144-
}
145-
146-
private void setPrevious(NodeStack<?> previous) {
147-
this.previous = previous;
148-
}
149-
150-
private static int getSize() {
151-
return size;
152-
}
153-
154-
private static void setSize(int size) {
155-
NodeStack.size = size;
156-
}
157-
158-
private Item getData() {
159-
return this.data;
160-
}
161109
}

0 commit comments

Comments
 (0)