今天用python写了个c++
import time
"""数组初始化模块"""
x = int(input("x:"))
n = int(input("n:"))
a = []
def init(x,n):
return [n]*(x+1)
a = init(x,n)
"""栈模块"""
class stack:
def __init__(self):
self.items = [] # 初始化一个空列表来存储栈中的元素
self.size = 0 # 初始化栈的大小为 0
def push(self, item):
"""
将元素 item 添加到列表末尾
更新栈的大小
"""
self.items.append(item)
self.size += 1
def pop(self):
"""
如果栈不为空,则减少栈的大小并移除并返回列表最后一个元素
否则,抛出异常
"""
if not self.is_empty():
self.size -= 1
return self.items.pop()
else:
raise Exception("stack is empty. Cannot perform pop operation.")
def top(self):
"""
如果栈不为空,则返回列表最后一个元素
否则,抛出异常
"""
if not self.is_empty():
return self.items[-1]
else:
raise Exception("stack is empty. No top element available.")
def is_empty(self):
"""
判断栈是否为空
"""
return self.size == 0
def get_size(self):
"""
返回栈的大小
"""
return self.size
# 创建栈对象并演示使用
# stk = stack()
"""队列模块"""
# 1
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
raise Exception("Queue is empty")
def size(self):
return len(self.items)
# 使用示例
# q = Queue()
# 2
from collections import deque
class Queue:
def __init__(self):
self.items = deque()
def is_empty(self):
return len(self.items) == 0
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.popleft()
else:
raise Exception("Queue is empty")
def size(self):
return len(self.items)
# 使用示例
# q = Queue()
# q.enqueue(1)
# q.enqueue(2)
# q.enqueue(3)
# print(q.dequeue())
# print(q.size())
"""vector模块"""
class Vector:
def __init__(self, initial_capacity=0, value=None):
"""
构造函数
:param initial_capacity: 初始容量,默认为0
:param value: 如果指定了初始容量,则可以用此值填充向量
"""
self._data = []
if initial_capacity > 0 and value is not None:
self._data = [value] * initial_capacity
def __len__(self):
"""
返回向量的长度
"""
return len(self._data)
def __getitem__(self, index):
"""
支持索引访问
"""
return self._data[index]
def __setitem__(self, index, value):
"""
支持索引赋值
"""
self._data[index] = value
def append(self, value):
"""
在向量末尾添加一个元素
"""
self._data.append(value)
def extend(self, iterable):
"""
将可迭代对象的所有元素添加到向量末尾
"""
self._data.extend(iterable)
def insert(self, index, value):
"""
在指定位置插入一个元素
"""
self._data.insert(index, value)
def pop(self, index=-1):
"""
移除并返回指定位置的元素,默认移除最后一个元素
"""
return self._data.pop(index)
def remove(self, value):
"""
移除第一个匹配的元素
"""
self._data.remove(value)
def clear(self):
"""
清空向量
"""
self._data.clear()
def __iter__(self):
"""
支持迭代器协议
"""
return iter(self._data)
def __repr__(self):
"""
返回向量的字符串表示形式
"""
return f"Vector({self._data})"
# 示例
# v = Vector(3, 0)
# v.append(1)
# v.extend([2, 3])
# print(v)
# print(len(v))
# print(v[2])
# v[2] = 4
# print(v)
# print(list(v))
#呵呵呵我的python太强了
"""链表模块"""
class LinkedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head is None
def add(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def append(self, data):
new_node = Node(data)
if self.is_empty():
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
def delete(self, key):
current = self.head
previous = None
while current:
if current.data == key:
if previous:
previous.next = current.next
else:
self.head = current.next
return
previous = current
current = current.next
def find(self, key):
current = self.head
while current:
if current.data == key:
return current
current = current.next
return None
def print_list(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
"""pair模块"""
class Pair:
def __init__(self, first, second):
self.first = first
self.second = second
def get_first(self):
return self.first
def get_second(self):
return self.second
def __repr__(self):
return f"Pair({self.first}, {self.second})"
# 示例
# pair = Pair(1, "hello")
# print(pair) # 输出: Pair(1, hello)
# print(pair.get_first()) # 输出: 1
# print(pair.get_second()) # 输出: hello
"""map模块"""
class Map:
def __init__(self):
self._data = {}
def __contains__(self, key):
return key in self._data
def __getitem__(self, key):
return self._data[key]
def __setitem__(self, key, value):
self._data[key] = value
def __delitem__(self, key):
del self._data[key]
def __len__(self):
return len(self._data)
def keys(self):
return self._data.keys()
def values(self):
return self._data.values()
def items(self):
return self._data.items()
def clear(self):
self._data.clear()
def get(self, key, default=None):
return self._data.get(key, default)
def pop(self, key, default=None):
return self._data.pop(key, default)
def update(self, other):
self._data.update(other)
def __repr__(self):
return repr(self._data)
"""什么你问我array在哪里"""
# 在这里->
import array
# 示例
# 创建一个整数类型的数组
int_array = array.array('i', [1, 2, 3, 4, 5])
# # 添加元素到数组末尾
# int_array.append(6)
# # 打印数组
# print(int_array) # 输出: array('i', [1, 2, 3, 4, 5, 6])
# # 获取数组中的元素
# print(int_array[0]) # 输出: 1
# # 修改数组中的元素
# int_array[0] = 10
# print(int_array) # 输出: array('i', [10, 2, 3, 4, 5, 6])
# # 删除数组中的元素
# del int_array[0]
# print(int_array) # 输出: array('i', [2, 3, 4, 5, 6])
# # 遍历数组
# for i in int_array:
# print(i, end=' ')
# # 输出: 2 3 4 5 6
"""终于用上C++了"""
"""
妈妈再也不怕我的VSCode的C++爆炸了用PYTHON一样写哈哈哈哈哈哈哈
"""