[Data Structure] Stack

The stack meets the first-in-last-out principle

1, python description

Share a picture

# Stack first in last out principle

MAXSTACK = 10
global stack
stack
= [None] * MAXSTACK
top
= -1


def is_empty():
if top == -1:
return True
else:
return False


def push(data):
global top
global MAXSTACK
global stack
if top >= MAXSTACK-1:
print("The stack is full and cannot be added")
else:
top
+= 1
stack[top]
= data


def pop():
global top
global stack
if is_empty():
print("The stack is empty")
else:
print("The pop-up element is: %d" % stack[top])
top
= top-1


if __name__ == "__main__":
i
= 0
while i <10:
i
+= 1
push(i)

pop()

View Code

2, go description

share picture

package test


import (
"fmt"
"testing "
)

const MAX_CAPACITY int = 10 // Define stack capacity
var stack [MAX_CAPACITY]interface{}
var top = -1 //the top element index of the stack


func isEmpty()
bool{
if top == -1 {
return true
}
return false
}

func push(data
interface{}){
if top> MAX_CAPACITY-1 {
fmt.Println(
"The stack capacity is full and cannot be pushed span>")
}
else {
top
++
stack[top]
= data
}
}


func pop(){
if isEmpty() {
fmt.Println(
"The stack is empty< span style="color: #800000;">")
}
else {
fmt.Println(
"The pop-up element is: < span style="color: #800000;">",stack[top])
top
--
}
}

func Test_Stack(t
*testing.T) {
for i:=0;i<5;i++{
push(i)
}
fmt.Println(stack)
pop()
}

View Code

share picture

# Stack first in last out principle

MAXSTACK = 10
global stack
stack
= [None] * MAXSTACK
top
= -1


def is_empty():
if top == -1:
return True
else:
return False


def push(data):
global top
global MAXSTACK
global stack
if top >= MAXSTACK-1:
print("The stack is full and cannot be added")
else:
top
+= 1
stack[top]
= data


def pop():
global top
global stack
if is_empty():
print("The stack is empty")
else:
print("The pop-up element is: %d" % stack[top])
top
= top-1


if __name__ == "__main__":
i
= 0
while i <10:
i
+= 1
push(i)

pop()

View Code

# Stack first in last out principle

MAXSTACK = 10
global stack
stack
= [None] * MAXSTACK
top
= -1


def is_empty():
if top == -1:
return True
else:
return False


def push(data):
global top
global MAXSTACK
global stack
if top >= MAXSTACK-1:
print("The stack is full and cannot be added")
else:
top
+= 1
stack[top]
= data


def pop():
global top
global stack
if is_empty():
print("The stack is empty")
else:
print("The pop-up element is: %d" % stack[top])
top
= top-1


if __name__ == "__main__":
i
= 0
while i <10:
i
+= 1
push(i)

pop()

share picture share picture

package test


import (
"fmt"
"testing "
)

const MAX_CAPACITY int = 10 // Define stack capacity
var stack [MAX_CAPACITY]interface{}
var top = -1 //the top element index of the stack


func isEmpty()
bool{
if top == -1 {
return true
}
return false
}

func push(data
interface{}){
if top> MAX_CAPACITY-1 {
fmt.Println(
"The stack capacity is full and cannot be pushed span>")
}
else {
top
++
stack[top]
= data
}
}


func pop(){
if isEmpty() {
fmt.Println(
"The stack is empty< span style="color: #800000;">")
}
else {
fmt.Println(
"The pop-up element is: < span style="color: #800000;">",stack[top])
top
--
}
}

func Test_Stack(t
*testing.T) {
for i:=0;i<5;i++{
push(i)
}
fmt.Println(stack)
pop()
}

View Code

package test


import (
"fmt"
"testing "
)

const MAX_CAPACITY int = 10 // Define stack capacity
var stack [MAX_CAPACITY]interface{}
var top = -1 //the top element index of the stack


func isEmpty()
bool{
if top == -1 {
return true
}
return false
}

func push(data
interface{}){
if top> MAX_CAPACITY-1 {
fmt.Println(
"The stack capacity is full and cannot be pushed span>")
}
else {
top
++
stack[top]
= data
}
}


func pop(){
if isEmpty() {
fmt.Println(
"The stack is empty< span style="color: #800000;">")
}
else {
fmt.Println(
"The pop-up element is: < span style="color: #800000;">",stack[top])
top
--
}
}

func Test_Stack(t
*testing.T) {
for i:=0;i<5;i++{
push(i)
}
fmt.Println(stack)
pop()
}

Leave a Comment

Your email address will not be published.