Adding new method Items() to the StringStack + docummentation
This commit is contained in:
parent
093b853785
commit
9ce435af85
1 changed files with 12 additions and 0 deletions
|
@ -23,6 +23,7 @@ type StringStack struct {
|
||||||
items []string
|
items []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MakeStringStack initializes a new StringStack pointer.
|
||||||
func MakeStringStack(capacity int) *StringStack {
|
func MakeStringStack(capacity int) *StringStack {
|
||||||
st := StringStack{}
|
st := StringStack{}
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ func MakeStringStack(capacity int) *StringStack {
|
||||||
return &st
|
return &st
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push pushes an item of type string to the stack.
|
||||||
func (st *StringStack) Push(item interface{}) {
|
func (st *StringStack) Push(item interface{}) {
|
||||||
if st.index == st.Capacity() {
|
if st.index == st.Capacity() {
|
||||||
panic(StackOverflowError)
|
panic(StackOverflowError)
|
||||||
|
@ -47,6 +49,7 @@ func (st *StringStack) Push(item interface{}) {
|
||||||
st.index++
|
st.index++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pop returns the last pushed item from the stack and removes it
|
||||||
func (st *StringStack) Pop() interface{} {
|
func (st *StringStack) Pop() interface{} {
|
||||||
if st.Size() == 0 {
|
if st.Size() == 0 {
|
||||||
panic(StackUnderflowError)
|
panic(StackUnderflowError)
|
||||||
|
@ -55,6 +58,7 @@ func (st *StringStack) Pop() interface{} {
|
||||||
return st.items[st.index]
|
return st.items[st.index]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Top return the last pushed item from the stack without removing it.
|
||||||
func (st *StringStack) Top() interface{} {
|
func (st *StringStack) Top() interface{} {
|
||||||
if st.Size() == 0 {
|
if st.Size() == 0 {
|
||||||
panic(StackUnderflowError)
|
panic(StackUnderflowError)
|
||||||
|
@ -62,14 +66,22 @@ func (st *StringStack) Top() interface{} {
|
||||||
return st.items[st.index-1]
|
return st.items[st.index-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEmpty returns true if the stack contains no items.
|
||||||
func (st *StringStack) IsEmpty() bool {
|
func (st *StringStack) IsEmpty() bool {
|
||||||
return st.Size() == 0
|
return st.Size() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capacity returns the maximum items the stack can hold.
|
||||||
func (st *StringStack) Capacity() int {
|
func (st *StringStack) Capacity() int {
|
||||||
return st.capacity
|
return st.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size returns number of items the stack currently holds.
|
||||||
func (st *StringStack) Size() int {
|
func (st *StringStack) Size() int {
|
||||||
return st.index
|
return st.index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Items returns an array of the stack items as a copy.
|
||||||
|
func (st StringStack) Items() []string {
|
||||||
|
return st.items
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue