一、笔记
class Stack:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
class Queue:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
class Deque:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def addFront(self,item):
self.items.append(item)
def addRear(self,item):
self.items.insert(0,item)
def removeFront(self):
return self.items.pop()
def removeRear(self):
return self.items.pop(0)
def size(self):
return len(self.items)
class Node:
def __init__(self,initdata):
self.data=initdata
self.next=None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data=newdata
def setNext(self,newnext):
self.next=newnext
class UnorderedList:
def __init__(self):
self.head=None
def add(self,item):
temp=Node(item)
temp.setNext(self.head)
self.head=temp
def size(self):
current=self.head
count=0
while current!=None:
count+=1
current=current.getNext()
return count
def search(self,item):
current=self.head
found=False
while current!=None and not found:
if current.getData()==item:
found=True
else:
current.getNext()
return found
def remove(self,item):
current=self.head
previous=None
found=False
while not found:
if current.getData()==item:
found=True
else:
previous=current
current=current.getNext()
if previous==None:
self.head=current.getNext()
else:
previous.setNext(current.getNext())
class OrderedList:
def __init__(self):
self.head=None
def add(self,item):
current=self.head
previous=None
stop=False
while current!=None and not stop:
if current.getData()>item:
stop=True
else:
previous=current()
current=current.getNext()
temp=Node(item)
if previous==None:
temp.setNext(self.head)
self.head=temp
else:
temp.setNext(current)
previous.setNext(temp)
def size(self):
current=self.head
count=0
while current!=None:
count+=1
current=current.getNext()
return count
def search(self,item):
current=self.head
found=False
stop=False
while current!=None and not found and not stop:
if current.getData()==item:
found=True
else:
if current.getData()>item:
stop=True
else:
current=current.getNext()
current.getNext()
return found
def remove(self,item):
current=self.head
previous=None
found=False
while not found:
if current.getData()==item:
found=True
else:
previous=current
current=current.getNext()
if previous==None:
self.head=current.getNext()
else:
previous.setNext(current.getNext())
import random
from func import Stack
from func import Queue
from func import Deque
from func import Node
#Stark匹配括号
def cheekpar(str):
s=Stack()
flag=True
index=0
while index<len(str):
a=str[index]
if a =="(":
s.push(a)
elif a ==")" and s.isEmpty() == False:
s.pop()
index+=1
if not s.isEmpty():
return False
else:
return True
print(cheekpar('((()'))
#Stark匹配各种符号
def match(top,end):
tops="{(["
ends="})]"
print(f'aa{tops.index(top) == ends.index(end)}')
return tops.index(top) == ends.index(end)
def cheekpar(str):
s=Stack()
index=0
while index<len(str):
a=str[index]
if a in "{([":
s.push(a)
else:
top=s.peek()
if match(top,a) and s.isEmpty() == False:
s.pop()
index+=1
if not s.isEmpty():
return False
else:
return True
print(cheekpar('{{([][])}()}'))
#约瑟夫问题
def hotpotato(str1,num):
q=Queue()
for name in str1:
q.enqueue(name)
while q.size()!=1:
for i in range(num):
q.enqueue(q.dequeue())
q.dequeue()
return q.dequeue()
print(hotpotato(["a","b","c","d","e","f","g"],7))
#回文词
def huiwen(str1):
d=Deque()
for letter in str1:
d.addRear(letter)
flag=True
while d.size()>1 and flag:
top=d.removeFront()
end=d.removeRear()
if top !=end:
flag=False
return flag
print(huiwen("radar"))