Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18657de

Browse files
refactored Stack in the package structures
1 parent 5677141 commit 18657de

File tree

7 files changed

+163
-149
lines changed

7 files changed

+163
-149
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ Content:
4545

4646
2. package <code>structures</code> - data structure
4747
* [Binary tree](/src/main/kotlin/structures/BinaryTree.kt)
48-
* [Stack](/src/main/kotlin/structures/Stack.kt)
48+
* [Stack 1st implementation](/src/main/kotlin/structures/ArrayListStack.kt)
49+
* [Stack 2nd implementation](/src/main/kotlin/structures/LinkedListStack.kt)
4950
* [Queue](/src/main/kotlin/structures/Queue.kt)
5051
* [Undirected Graph](/src/main/kotlin/structures/Graph.kt)
5152
* [Directed Graph with weights](/src/main/kotlin/structures/GraphWithWeights.kt)

‎README_ru.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545

4646
2. пакет <code>ru.structures</code> - структуры данных
4747
* [Бинарное дерево](/src/main/kotlin/structures/BinaryTree.kt)
48-
* [Стэк](/src/main/kotlin/structures/Stack.kt)
48+
* [Стэк 1-ая реализация](/src/main/kotlin/structures/ArrayListStack.kt)
49+
* [Стэк 2-ая реализация](/src/main/kotlin/structures/LinkedListStack.kt)
4950
* [Очередь](/src/main/kotlin/structures/Queue.kt)
5051
* [Неориентированный граф](/src/main/kotlin/structures/Graph.kt)
5152
* [Ориентированный граф с весами](/src/main/kotlin/structures/GraphWithWeights.kt)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package structures
2+
3+
import java.lang.IllegalArgumentException
4+
import java.util.ArrayList
5+
6+
/**
7+
*
8+
* Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle
9+
*
10+
* LIFO implies that the element that is inserted last, comes out first.
11+
*
12+
* The main operations:
13+
*
14+
* push() - when we insert an element in a stack then the operation is known as a push.
15+
* pop() - when we delete an element from the stack, the operation is known as a pop.
16+
* If the stack is empty means that no element exists in the stack.
17+
*
18+
* All these operations performed in O(1) time.
19+
*
20+
*/
21+
22+
class ArrayListStack<T> {
23+
// this implementation uses ArrayList
24+
private val data = ArrayList<T>()
25+
26+
val isEmpty: Boolean
27+
get() = data.size == 0
28+
29+
val size: Int
30+
get() = data.size
31+
32+
fun push(item: T) {
33+
data.add(item)
34+
}
35+
36+
fun pop() : T {
37+
if (isEmpty) {
38+
throw IllegalArgumentException("Stack is empty!")
39+
}
40+
return data.removeLast()
41+
}
42+
43+
fun peek() : T {
44+
if (isEmpty) {
45+
throw IllegalArgumentException("Stack is empty!")
46+
}
47+
return data.last()
48+
}
49+
50+
fun clear() {
51+
data.clear()
52+
}
53+
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package structures
2+
3+
import java.util.LinkedList
4+
5+
/**
6+
*
7+
* Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle
8+
*
9+
* LIFO implies that the element that is inserted last, comes out first.
10+
*
11+
* The main operations:
12+
*
13+
* push() - when we insert an element in a stack then the operation is known as a push.
14+
* pop() - when we delete an element from the stack, the operation is known as a pop.
15+
* If the stack is empty means that no element exists in the stack.
16+
*
17+
* All these operations performed in O(1) time.
18+
*
19+
*/
20+
21+
class LinkedListStack<T> {
22+
// this implementation uses LinkedList
23+
private val data = LinkedList<T>()
24+
25+
val isEmpty: Boolean
26+
get() = data.size == 0
27+
28+
val size: Int
29+
get() = data.size
30+
31+
fun push(item: T) {
32+
data.add(item)
33+
}
34+
35+
fun pop(): T {
36+
if (isEmpty) {
37+
throw IllegalArgumentException("Stack is empty!")
38+
}
39+
return data.removeLast()
40+
}
41+
42+
fun peek(): T {
43+
if (isEmpty) {
44+
throw IllegalArgumentException("Stack is empty!")
45+
}
46+
return data.peekLast()
47+
}
48+
49+
fun clear() {
50+
data.clear()
51+
}
52+
53+
}

‎src/main/kotlin/structures/Stack.kt

Lines changed: 0 additions & 107 deletions
This file was deleted.

‎src/test/kotlin/structures/ArrayListStackTest.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ import org.junit.Assert.assertEquals
55

66
class ArrayListStackTest {
77

8-
private val arrayListStack = Stack.ArrayListStack<Int>()
9-
108
@Test
11-
fun test_push() {
12-
arrayListStack.push(10)
13-
assertEquals(10, arrayListStack.peek())
14-
}
9+
fun test() {
10+
val stack = ArrayListStack<Int>()
1511

16-
@Test
17-
fun test_pop() {
18-
arrayListStack.push(20)
19-
assertEquals(20, arrayListStack.pop())
20-
}
12+
stack.push(1)
13+
stack.push(2)
14+
stack.push(3)
2115

22-
@Test
23-
fun test_is_empty() {
24-
arrayListStack.clear()
25-
assertEquals(true, arrayListStack.isEmpty())
26-
}
16+
assertEquals(false, stack.isEmpty)
17+
assertEquals(3, stack.size)
2718

28-
@Test
29-
fun test_is_not_empty() {
30-
arrayListStack.push(20)
31-
assertEquals(false, arrayListStack.isEmpty())
19+
assertEquals(3, stack.pop())
20+
assertEquals(2, stack.pop())
21+
assertEquals(1, stack.pop())
22+
23+
assertEquals(true, stack.isEmpty)
24+
assertEquals(0, stack.size)
25+
26+
stack.push(10)
27+
stack.push(20)
28+
stack.push(30)
29+
30+
assertEquals(3, stack.size)
31+
assertEquals(30, stack.peek())
32+
assertEquals(3, stack.size)
33+
34+
stack.clear()
35+
36+
assertEquals(true, stack.isEmpty)
37+
assertEquals(0, stack.size)
3238
}
3339

3440
}

‎src/test/kotlin/structures/LinkedListStackTest.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ import org.junit.Assert.assertEquals
55

66
class LinkedListStackTest {
77

8-
private val arrayListStack = Stack.LinkedListStack<Int>()
9-
108
@Test
11-
fun test_push() {
12-
arrayListStack.push(10)
13-
assertEquals(10, arrayListStack.peek())
14-
}
9+
fun test() {
10+
val stack = LinkedListStack<Int>()
1511

16-
@Test
17-
fun test_pop() {
18-
arrayListStack.push(20)
19-
assertEquals(20, arrayListStack.pop())
20-
}
12+
stack.push(1)
13+
stack.push(2)
14+
stack.push(3)
2115

22-
@Test
23-
fun test_is_empty() {
24-
arrayListStack.clear()
25-
assertEquals(true, arrayListStack.isEmpty())
26-
}
16+
assertEquals(false, stack.isEmpty)
17+
assertEquals(3, stack.size)
2718

28-
@Test
29-
fun test_is_not_empty() {
30-
arrayListStack.push(20)
31-
assertEquals(false, arrayListStack.isEmpty())
19+
assertEquals(3, stack.pop())
20+
assertEquals(2, stack.pop())
21+
assertEquals(1, stack.pop())
22+
23+
assertEquals(true, stack.isEmpty)
24+
assertEquals(0, stack.size)
25+
26+
stack.push(10)
27+
stack.push(20)
28+
stack.push(30)
29+
30+
assertEquals(3, stack.size)
31+
assertEquals(30, stack.peek())
32+
assertEquals(3, stack.size)
33+
34+
stack.clear()
35+
36+
assertEquals(true, stack.isEmpty)
37+
assertEquals(0, stack.size)
3238
}
3339

3440
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /