queue hàng đợi và ngăn xếp stack, Exercises of Mathematics

Ngân hàng cũng sở hữu một bộ máy tổ chức vô cùng chặt chẽ và linh hoạt. Các chính sách đãi ngộ đã ngộ nhân viên vô cùng tốt nhằm mục đích chiêu mộ người tài vào làm việc cho ngân hàng.

Typology: Exercises

2023/2024

Uploaded on 03/23/2024

tram-pham-10
tram-pham-10 🇻🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bài tập:
1. Định nghĩa các phép toán cho stack và sử dụng nó để chuyển cơ
số từ cơ số 10, sang cơ số x
2. Định nghĩa kiểu dữ liệu và phép toán cho queue, sau đó sử dụng
để nhập vào 1 dãy số, in ra bình phương của từng số theo thứ
tự nhập vào
Ví dụ: nhập vào 2, 4, 5. Xuất ra 4, 16, 25. Lưu ý nhập xong
rồi xuất ra 1 lần.
class Stack:
def __init__(self):
self.stack = []
self.size =0
self.top =-1
def pushS(self, x):
self.stack.append(x)
self.size +=1
self.top +=1
# Use list pop method to remove element
def popS(self):
if self.size <= 0:
return ("No element in the Stack")
else:
self.top-=1
self.size-=1
return self.stack.pop()
def isEmpty(self):
def getTop(self):
AStack = Stack()
AStack.pushS("Mon")
AStack.pushS("Tue")
AStack.pushS("Wed")
AStack.pushS("Thu")
print(AStack.popS())
print(AStack.popS())
Output
When the above code is executed, it produces the following result −
Thu
Wed
pf2

Partial preview of the text

Download queue hàng đợi và ngăn xếp stack and more Exercises Mathematics in PDF only on Docsity!

Bài tập:

  1. Định nghĩa các phép toán cho stack và sử dụng nó để chuyển cơ số từ cơ số 10, sang cơ số x
  2. Định nghĩa kiểu dữ liệu và phép toán cho queue, sau đó sử dụng để nhập vào 1 dãy số, in ra bình phương của từng số theo thứ tự nhập vào Ví dụ: nhập vào 2, 4, 5. Xuất ra 4, 16, 25. Lưu ý nhập xong rồi xuất ra 1 lần. class Stack: def init(self): self.stack = [] self.size = self.top =- def pushS(self, x): self.stack.append(x) self.size += self.top +=

Use list pop method to remove element

def popS(self): if self.size <= 0 : return ("No element in the Stack") else: self.top-= self.size-= return self.stack.pop() def isEmpty(self): def getTop(self): AStack = Stack() AStack.pushS("Mon") AStack.pushS("Tue") AStack.pushS("Wed") AStack.pushS("Thu") print(AStack.popS()) print(AStack.popS())

Output

When the above code is executed, it produces the following result − Thu Wed

class Queue: def init(self): self.queue = list() def addtoQ(self,dataval):

Insert method to add element

if dataval not in self.queue: self.queue.insert( 0 ,dataval) return True return False

Pop method to remove element

def removefromQ(self): if len(self.queue)> 0 : return self.queue.pop() return ("No elements in Queue!") TheQueue = Queue() TheQueue.addtoq("Mon") TheQueue.addtoq("Tue") TheQueue.addtoq("Wed") print(TheQueue.removefromq()) print(TheQueue.removefromq())

Output

When the above code is executed, it produces the following result − Mon Tue