2017-10-28 16:02:26 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
// Stack interface
|
|
|
|
type Stack interface {
|
|
|
|
// Push pushes an item to the stack. Returns an error if it fails.
|
|
|
|
Push(item interface{})
|
|
|
|
// Pop retrieves an item from the stack and removes it.
|
|
|
|
Pop() interface{}
|
|
|
|
// Top peeks at an item from the stack.
|
|
|
|
Top() interface{}
|
|
|
|
// IsEmpty returns bool indicating if the stack is empty.
|
|
|
|
IsEmpty() bool
|
|
|
|
// Capacity returns the capacity of the stack.
|
|
|
|
Capacity() int
|
|
|
|
// Size returns the size of the stack
|
|
|
|
Size() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringStack is a stack that holds string objects
|
|
|
|
type StringStack struct {
|
|
|
|
index int
|
|
|
|
capacity int
|
|
|
|
items []string
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// MakeStringStack initializes a new StringStack pointer.
|
2017-10-28 16:02:26 +00:00
|
|
|
func MakeStringStack(capacity int) *StringStack {
|
|
|
|
st := StringStack{}
|
|
|
|
|
|
|
|
st.capacity = capacity
|
|
|
|
st.index = 0
|
|
|
|
st.items = make([]string, capacity, capacity)
|
|
|
|
|
|
|
|
return &st
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// Push pushes an item of type string to the stack.
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) Push(item interface{}) {
|
|
|
|
if st.index == st.Capacity() {
|
|
|
|
panic(StackOverflowError)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := item.(string)
|
|
|
|
if ok == false {
|
|
|
|
panic(StackInvalidTypeError)
|
|
|
|
}
|
|
|
|
|
|
|
|
st.items[st.index] = value
|
|
|
|
st.index++
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// Pop returns the last pushed item from the stack and removes it
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) Pop() interface{} {
|
|
|
|
if st.Size() == 0 {
|
|
|
|
panic(StackUnderflowError)
|
|
|
|
}
|
|
|
|
st.index--
|
|
|
|
return st.items[st.index]
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// Top return the last pushed item from the stack without removing it.
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) Top() interface{} {
|
|
|
|
if st.Size() == 0 {
|
|
|
|
panic(StackUnderflowError)
|
|
|
|
}
|
|
|
|
return st.items[st.index-1]
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// IsEmpty returns true if the stack contains no items.
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) IsEmpty() bool {
|
|
|
|
return st.Size() == 0
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// Capacity returns the maximum items the stack can hold.
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) Capacity() int {
|
|
|
|
return st.capacity
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:45:38 +00:00
|
|
|
// Size returns number of items the stack currently holds.
|
2017-10-28 16:02:26 +00:00
|
|
|
func (st *StringStack) Size() int {
|
|
|
|
return st.index
|
|
|
|
}
|
2017-10-29 15:45:38 +00:00
|
|
|
|
|
|
|
// Items returns an array of the stack items as a copy.
|
|
|
|
func (st StringStack) Items() []string {
|
|
|
|
return st.items
|
|
|
|
}
|