#模擬棧結構
stack = []
#壓棧(想棧里存數據)
stack.append("A")
print(stack)
stack.append("B")
print(stack)
stack.append("C")
print(stack)#出棧(在棧里取數據)
res = stack.pop()
print("res= ",res)
print(stack)
res = stack.pop()
print("res2= ",res)
print(stack)
res = stack.pop()
print("res3= ",res)
print(stack)import collections#創建一個隊列
queue = collections.deque()
print(queue)#進隊(存數據)
queue.append("A")
print(queue)
queue.append("B")
print(queue)
queue.append("C")
print(queue)#出隊(取數據)
res1 = queue.popleft()
print("res1 = ",res1)
print(queue)import osdef getAllDir(path,sp=""):sp +=" "# 得到當前目錄下所有文件fileList = os.listdir(path)#處理每一個文件for fileName in fileList:#path\fileName#判斷是否是路徑(用絕對路徑)fileAbsPath = os.path.join(path,fileName)if os.path.isdir(fileAbsPath):print(sp+"目錄:",fileName)#遞歸調用getAllDir(fileAbsPath,sp)else:print(sp+"普通文件:",fileName)getAllDir(r"D:\xiazaipan\第1章 Python語言基礎\第1章 Python語言基礎")import os
def getAllDirDE(path):stack =[]stack.append(path)#處理棧,當棧為空的時候結束循環while len(stack)!=0:#從棧里取出數據dirPath = stack.pop()fileList = os.listdir(dirPath)#處理每一個文件,如果是普通文件則打印出來,如果是目錄則將該目錄地址壓棧for fileName in fileList:fileAbspath = os.path.join(dirPath,fileName)if os.path.isdir(fileAbspath):#如果是目錄就壓棧stack.append(fileAbspath)print("目錄:",fileName)else:#打印普通文件print("普通文件:"+fileName)getAllDirDE(r"D:\xiazaipan\第1章 Python語言基礎\第1章 Python語言基礎")import os
import collections
def getAllDirQU(path):queue = collections.deque()#進隊queue.append(path)while len(queue)!=0:#出隊數據dirPath = queue.popleft()#找出所有文件fileList = os.listdir(dirPath)for fileName in fileList:#絕對路徑fileAbsPath = os.path.join(dirPath,fileName)#判斷是否是目錄,是目錄進隊,不是打印if os.path.isdir(fileAbsPath):print("目錄"+fileName)queue.append(fileAbsPath)else:print("普通文件"+fileName)getAllDirQU(r"D:\xiazaipan\第1章 Python語言基礎\第1章 Python語言基礎")
?