This is a creation in Article, where the information may have evolved or changed.
Stack is like a water cup in-first out, the chain of the stack in the back element of the next always point to the advanced element, the top element of the stack is always the same as the last element to go in
Type Elem int
node element structure
Type Node struct {
Data Elem
Next *node
}
Stacklink Stack
Type Stacklink struct {
Top *node//Stack tops elements
length int
}
Initstack initialization of a stack, stack advanced after the
Func (S *stacklink) Initstack () {
S.top = Nil
}
Push adds a STACK element
Func (S *stacklink) Push (data Elem) {
Create a Node
Node: = new (node)
Node.data = Data
Node.next = S.top
S.top = node
s.length++
}
Pop pops up an element
Func (S *stacklink) Pop (e *elem) error {
If S.empty () {
return errors. New ("Empty Stack")
}
*e = S.top.data
Node: = S.top
S.top = Node.next
s.length--
return Nil
}
Empty stack
Func (S *stacklink) Empty () bool {
return s.top = = Nil
}
The number of elements in the Length stack
Func (S *stacklink) Length () int {
Return s.length
}