Skip to content

Commit d0d60f1

Browse files
committed
stack
1 parent d987518 commit d0d60f1

File tree

7 files changed

+712
-332
lines changed

7 files changed

+712
-332
lines changed

examples/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ examples/
3232
- Real-world use cases
3333

3434
#### Stack Examples (`stack/stack_examples.go`)
35-
- Array-based stack implementation
36-
- Linked List-based stack implementation
35+
- Array-based stack implementation with generic type support
36+
- Linked List-based stack implementation with generic type support
3737
- LIFO (Last In First Out) operations
38+
- Support for any comparable type
39+
- Examples with different data types (int, string, custom structs)
40+
- Thread-safe operations
3841
- Expression evaluation examples
3942
- Practical stack applications
4043

examples/stack/stack_examples.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func RunExamples() {
1111
// Example 1: Array-based Stack
1212
fmt.Println("Array Stack Example:")
13-
arrayStack := stack.NewArrayStack()
13+
arrayStack := stack.NewArrayStack[int]()
1414

1515
fmt.Println("Pushing elements:")
1616
arrayStack.Push(1)

stack/ArrayStack.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ import (
55
"sync"
66
)
77

8-
type ArrayStack struct {
9-
Arr []int
8+
// ArrayStack represents a generic array-based stack
9+
type ArrayStack[T any] struct {
10+
Arr []T
1011
ArrSize int
1112
Index int
1213
mutex sync.RWMutex
1314
}
1415

15-
func NewArrayStack() *ArrayStack {
16-
return &ArrayStack{
17-
Arr: []int{0, 0},
16+
// NewArrayStack creates a new generic array-based stack
17+
func NewArrayStack[T any]() *ArrayStack[T] {
18+
return &ArrayStack[T]{
19+
Arr: make([]T, 2),
1820
ArrSize: 2,
1921
Index: 0,
2022
mutex: sync.RWMutex{},
2123
}
2224
}
2325

2426
// Push adds data to the stack
25-
func (arr *ArrayStack) Push(data int) {
27+
func (arr *ArrayStack[T]) Push(data T) {
2628
arr.mutex.Lock()
2729
defer arr.mutex.Unlock()
2830

2931
if arr.Index >= arr.ArrSize {
30-
newArr := make([]int, arr.ArrSize*2)
31-
for i := 0; i < arr.ArrSize; i++ {
32-
newArr[i] = arr.Arr[i]
33-
}
32+
newArr := make([]T, arr.ArrSize*2)
33+
copy(newArr, arr.Arr)
3434
arr.Arr = newArr
3535
arr.ArrSize *= 2
3636
}
@@ -39,45 +39,44 @@ func (arr *ArrayStack) Push(data int) {
3939
}
4040

4141
// Pop removes data from the stack
42-
func (arr *ArrayStack) Pop() {
42+
func (arr *ArrayStack[T]) Pop() {
4343
arr.mutex.Lock()
4444
defer arr.mutex.Unlock()
4545

4646
if arr.Index == 0 {
4747
return
4848
}
4949
arr.Index--
50-
arr.Arr[arr.Index] = 0
50+
var zero T
51+
arr.Arr[arr.Index] = zero
5152
if arr.Index <= arr.ArrSize/4 && arr.ArrSize > 2 {
52-
newArr := make([]int, arr.ArrSize/2)
53-
for i := 0; i < arr.Index; i++ {
54-
newArr[i] = arr.Arr[i]
55-
}
53+
newArr := make([]T, arr.ArrSize/2)
54+
copy(newArr, arr.Arr[:arr.Index])
5655
arr.Arr = newArr
5756
arr.ArrSize /= 2
5857
}
5958
}
6059

6160
// IsEmpty returns true if stack is empty
62-
func (arr *ArrayStack) IsEmpty() bool {
61+
func (arr *ArrayStack[T]) IsEmpty() bool {
6362
arr.mutex.RLock()
6463
defer arr.mutex.RUnlock()
6564
return arr.Index == 0
6665
}
6766

6867
// List returns a slice of stack data
69-
func (arr *ArrayStack) List() []int {
68+
func (arr *ArrayStack[T]) List() []T {
7069
arr.mutex.RLock()
7170
defer arr.mutex.RUnlock()
72-
var list []int
71+
var list []T
7372
for i := 0; i < arr.Index; i++ {
7473
list = append(list, arr.Arr[i])
7574
}
7675
return list
7776
}
7877

7978
// Print displays stack data
80-
func (arr *ArrayStack) Print() {
79+
func (arr *ArrayStack[T]) Print() {
8180
arr.mutex.RLock()
8281
defer arr.mutex.RUnlock()
8382
fmt.Print("print : ")

0 commit comments

Comments
 (0)