链表
#链节点 class Node: def __init__(self, item = 0, next = None): self.item = item self.next = next #链表 class LinkedList: def __init__(self): self.head = None def create(self, data): self.head = Node(data[0]) cur = self.head for i in range(len(data)): if i == 0: continue nd = Node(data[i]) cur.next = nd cur = cur.next #删除链节点 def remove(self, x): cur = self.head pre = None while cur: if cur.item == x: #删cur if cur == self.head: self.head = cur.next else: pre.next = cur.next cur = cur.next ##移动 else: pre = cur cur = cur.next #遍历输出 def Print(self): cur = self.head while cur: print(cur.item, end = " ") cur = cur.next print('\n')
栈
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()
队列
class Node: def __init__(self, data = None): self.data = data self.next = None class LinkQueue: def __init__(self): self.front = None self.rear = None def empty(self): return self.front == None def push(self, e): new = Node(e) if self.empty(): self.front = self.rear = new else: self.rear.next = new self.rear = new def pop(self): if self.empty(): return if self.front == self.rear: e = self.front.data self.front = self.rear = None else: e = self.front self.front = self.front.next return e def gethead(self): if self.empty(): return e = self.front.data return e
标签:__,cur,self,基础,next,front,数据结构,def From: https://www.cnblogs.com/k43-ween/p/17752025.html