栈结构
2026-01-25 10:38:11
发布于:浙江
C++
#include<iostream>
#include<stack>
using namespace std;
int main(){
stack<int> stackint;
stackint.push(1)
stackint.push(2)
cout<<stackint.empty()<<endl;//0
cout<<stackint.size()<<endl;
cout<<stackint.top()<<endl;
stackint.top()
}
Python
class Stack:
def __init__(self,typef):
self.typef=typef
self.lists=[]
def push(self,data):
if type(data)!=self.typef:
raise ValueError
else:
self.lists.append(data)
def pop(self):
a=self.lists[-1]
lists=lists[:-2]
return a
def size(self):
return len(self.lists)
def empty(self):
return len(self.lists)==0
stack=Stack(int)
stack.push(2)
print(stack.pop())
print(stack.size(),stack.empty())
C++用着#include<stack> ,Python需要自己写代码
这里空空如也
















有帮助,赞一个