Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack:
- def __init__(self):
- self.stack = []
- def push(self, item):
- self.stack.append(item)
- def pop(self):
- if not self.is_empty():
- return self.stack.pop()
- def peek(self):
- if not self.is_empty():
- return self.stack[-1]
- def is_empty(self):
- return len(self.stack) == 0
- def size(self):
- return len(self.stack)
- stack = Stack()
- stack.push(1)
- stack.push(2)
- stack.push(3)
- print(stack.pop()) # 3
- print(stack.peek()) # 2
- print(stack.is_empty()) # False
- print(stack.size()) # 2
- class Queue:
- def __init__(self):
- self.queue = []
- def is_empty(self):
- return len(self.queue) == 0
- def enqueue(self, item):
- self.queue.append(item)
- def dequeue(self):
- if not self.is_empty():
- return self.queue.pop(0)
- queue = Queue()
- queue.enqueue(1)
- queue.enqueue(2)
- queue.enqueue(3)
- print(queue.dequeue()) # 1
- print(queue.dequeue()) # 2
- print(queue.is_empty()) # False
- print(queue.dequeue()) # 3
- print(queue.is_empty()) # True
- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class LinkedList:
- def __init__(self):
- self.head = None
- def append(self, data):
- new_node = Node(data)
- if self.head is None:
- self.head = new_node
- return
- last_node = self.head
- while last_node.next is not None:
- last_node = last_node.next
- last_node.next = new_node
- def display(self):
- if self.head is None:
- print("Lista jest pusta")
- return
- current_node = self.head
- while current_node is not None:
- print(current_node.data, end=" -> ")
- current_node = current_node.next
- print("None")
- my_list = LinkedList()
- my_list.append(1)
- my_list.append(2)
- my_list.append(3)
- my_list.display()
- def is_palindrome(word):
- stack = Stack()
- for character in word:
- stack.push(character)
- reversed_word = ""
- while not stack.is_empty():
- reversed_word += stack.pop()
- return word == reversed_word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement