Advertisement
ada1711

Lekcja 15

Apr 16th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 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 self.is_empty():
  48.             return None
  49.         else:
  50.             return self.queue.pop(0)
  51.  
  52. queue = Queue()
  53.  
  54. queue.enqueue(1)
  55. queue.enqueue(2)
  56. queue.enqueue(3)
  57.  
  58. print(queue.dequeue())  # 1
  59. print(queue.dequeue())  # 2
  60. print(queue.is_empty())  # False
  61. print(queue.dequeue())  # 3
  62. print(queue.is_empty())  # True
  63.  
  64.  
  65.  
  66.  
  67.  
  68. class Node:
  69.     def __init__(self, data):
  70.         self.data = data
  71.         self.next = None
  72.  
  73. class LinkedList:
  74.     def __init__(self):
  75.         self.head = None
  76.  
  77.     def append(self, data):
  78.         new_node = Node(data)
  79.         if self.head is None:
  80.             self.head = new_node
  81.             return
  82.         last_node = self.head
  83.         while last_node.next is not None:
  84.             last_node = last_node.next
  85.         last_node.next = new_node
  86.  
  87.     def display(self):
  88.         if self.head is None:
  89.             print("Lista jest pusta")
  90.             return
  91.         current_node = self.head
  92.         while current_node is not None:
  93.             print(current_node.data, end=" -> ")
  94.             current_node = current_node.next
  95.         print("None")
  96.  
  97. my_list = LinkedList()
  98. my_list.append(1)
  99. my_list.append(2)
  100. my_list.append(3)
  101. my_list.display()
  102.  
  103.  
  104.  
  105. def is_palindrome(string):
  106.     stack = Stack()
  107.     for char in string:
  108.         stack.push(char)
  109.     reversed_string = ""
  110.     while not stack.is_empty():
  111.         reversed_string += stack.pop()
  112.     return string == reversed_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement