Advertisement
ada1711

Python - Lekcja 15

Apr 19th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. class Stack:
  2.     def __init__(self):
  3.         self.stack = []
  4.  
  5.     def push(self, item):
  6.         self.stack.append(item)
  7.  
  8.     def pop(self):
  9.         if not self.is_empty():
  10.             return self.stack.pop()
  11.  
  12.     def peek(self):
  13.         if not self.is_empty():
  14.             return self.stack[-1]
  15.  
  16.     def is_empty(self):
  17.         return len(self.stack) == 0
  18.  
  19.     def size(self):
  20.         return len(self.stack)
  21.  
  22. stack = Stack()
  23.  
  24. stack.push(1)
  25. stack.push(2)
  26. stack.push(3)
  27.  
  28. print(stack.pop())  # 3
  29. print(stack.peek())  # 2
  30. print(stack.is_empty())  # False
  31. print(stack.size())  # 2
  32.  
  33.  
  34.  
  35.  
  36. class Queue:
  37.     def __init__(self):
  38.         self.queue = []
  39.  
  40.     def is_empty(self):
  41.         return len(self.queue) == 0
  42.  
  43.     def enqueue(self, item):
  44.         self.queue.append(item)
  45.  
  46.     def dequeue(self):
  47.         if not self.is_empty():
  48.             return self.queue.pop(0)
  49.            
  50.  
  51. queue = Queue()
  52.  
  53. queue.enqueue(1)
  54. queue.enqueue(2)
  55. queue.enqueue(3)
  56.  
  57. print(queue.dequeue())  # 1
  58. print(queue.dequeue())  # 2
  59. print(queue.is_empty())  # False
  60. print(queue.dequeue())  # 3
  61. print(queue.is_empty())  # True
  62.  
  63.  
  64.  
  65.  
  66.  
  67. class Node:
  68.     def __init__(self, data):
  69.         self.data = data
  70.         self.next = None
  71.  
  72. class LinkedList:
  73.     def __init__(self):
  74.         self.head = None
  75.  
  76.     def append(self, data):
  77.         new_node = Node(data)
  78.         if self.head is None:
  79.             self.head = new_node
  80.             return
  81.         last_node = self.head
  82.         while last_node.next is not None:
  83.             last_node = last_node.next
  84.         last_node.next = new_node
  85.  
  86.     def display(self):
  87.         if self.head is None:
  88.             print("Lista jest pusta")
  89.             return
  90.         current_node = self.head
  91.         while current_node is not None:
  92.             print(current_node.data, end=" -> ")
  93.             current_node = current_node.next
  94.         print("None")
  95.  
  96. my_list = LinkedList()
  97. my_list.append(1)
  98. my_list.append(2)
  99. my_list.append(3)
  100. my_list.display()
  101.  
  102.  
  103.  
  104. def is_palindrome(word):
  105.     stack = Stack()
  106.     for character in word:
  107.         stack.push(character)
  108.     reversed_word = ""
  109.     while not stack.is_empty():
  110.         reversed_word += stack.pop()
  111.     return word == reversed_word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement