python链栈的实现
时间: 2024-04-29 13:24:58 浏览: 10
好的,以下是Python链栈的实现:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.top = None
def push(self, data):
new_node = Node(data)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.top is None:
return None
popped_node = self.top
self.top = self.top.next
popped_node.next = None
return popped_node.data
def peek(self):
return self.top.data if self.top is not None else None
def is_empty(self):
return self.top is None
```
希望对你有所帮助!现在有什么其他问题吗?
相关问题
链栈实现数制转换算法
链栈可以用来实现数制转换算法。下面是一个使用链栈实现通用数制转换程序的示例代码:
```python
class StackNode:
def __init__(self, data):
self.data = data
self.next = None
class LinkedStack:
def __init__(self):
self.top = None
def is_empty(self):
return self.top is None
def push(self, data):
new_node = StackNode(data)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.is_empty():
return None
data = self.top.data
self.top = self.top.next
return data
def convert_decimal_to_base(decimal, base):
stack = LinkedStack()
digits = "0123456789ABCDEF"
is_negative = False
if decimal < 0:
is_negative = True
decimal = abs(decimal)
while decimal > 0:
remainder = decimal % base
stack.push(remainder)
decimal = decimal // base
result = ""
if is_negative:
result += "-"
while not stack.is_empty():
digit = stack.pop()
result += digits[digit]
return result
```
使用上述代码,你可以调用`convert_decimal_to_base`函数将任意一个十进制数转换成p进制的数。其中,`decimal`是待转化的十进制数,`base`是要转化的进制数。
例如,将十进制数15转换为二进制数和十六进制数,可以使用以下代码:
```python
decimal = 15
binary = convert_decimal_to_base(decimal, 2)
hexadecimal = convert_decimal_to_base(decimal, 16)
print("Binary: ", binary) # 输出:1111
print("Hexadecimal: ", hexadecimal) # 输出:F
```
链栈实现数制转换算法介绍
链栈实现数制转换算法的思想是利用栈的先进后出的特性,将待转化的数从低位到高位依次入栈,然后再依次出栈,得到转化后的结果。具体步骤如下:
1. 创建一个空栈。
2. 将待转化的数从低位到高位依次取余,将余数入栈,直到待转化的数为0。
3. 依次出栈,得到转化后的数。
下面是一个使用链栈实现十进制转二进制的示例代码:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.top = None
def is_empty(self):
return self.top is None
def push(self, data):
new_node = Node(data)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.is_empty():
return None
data = self.top.data
self.top = self.top.next
return data
def decimal_to_binary(decimal):
stack = Stack()
while decimal > 0:
remainder = decimal % 2
stack.push(remainder)
decimal = decimal // 2
binary = ""
while not stack.is_empty():
binary += str(stack.pop())
return binary
decimal = 10
binary = decimal_to_binary(decimal)
print(f"The binary representation of {decimal} is {binary}") # 输出:The binary representation of 10 is 1010
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)