Python手写模拟单向链表对象,栈对象和树
单向链表:
class error(Exception): def __init__(self,msg): super(error,self).__init__() self.msg=msg def __str__(self): return self.msg class Node: def __init__(self,ele): self.ele=ele self.next=None class mylist: def __init__(self,ele=None): self.index=0 self.pos=0 if ele: self._head=Node(ele) else: self._head = None def __iter__(self): return self def __next__(self): if self.pos < self.length(): self.pos += 1 cursor = self._head current=cursor num = 0 while cursor != None: current = cursor cursor = cursor.next num += 1 if num == self.pos: return current.ele else: raise StopIteration def empty(self): return self._head==None def add(self,item): node=Node(item) node.next=self._head self._head=node def length(self): cur=self._head num=0 if cur: num+=1 while cur.next: cur=cur.next num+=1 return num else: return num def append(self,item): node=Node(item) cursor=self._head if self.empty(): self._head = node else: while cursor.next!= None: cursor = cursor.next cursor.next=node def pop(self): if self.empty(): return None else: if self.length()==1: ele=self._head.ele self._head.ele=None return ele else: cur=self._head current=cur while cur.next!=None: current=cur cur=cur.next ele=cur.ele current.next=None return ele def insert(self,index,item): if index<0 or index>self.length(): raise error("out of range") else: if index==0: self.add(item) elif index==self.length(): self.append(item) else: node = Node(item) cur=self._head pre=0 while pre<index-1: cur=cur.next pre+=1 node.next=cur.next cur.next=node def get(self,index): if index<0 or index>self.length()-1: raise error("out of range") else: num=0 cur=self._head while num<index: cur=cur.next num+=1 return cur.ele def remove(self,item): cur=self._head pre=None while cur!=None: if cur.ele==item: if pre==None: self._head=cur.next else: pre.next=cur.next break else: pre=cur cur=cur.next def delete(self,index): if index<0 or index>self.length()-1: raise error("out of range") else: if index==0: self._head=self._head.next else: num=1 current=self._head cur=current.next while num!=index: current=cur cur=current.next num+=1 current.next=cur.next cur.ele=None cur.next=None
栈:
class stack: def __init__(self): self._box=[] def empty(self): return self._box==[] def length(self): return len(self._box) def push(self,item): self._box.append(item) def pop(self): return self._box.pop()
树:
class Node: def __init__(self,ele=None,left=None,right=None): self.ele = ele self.left = None self.right = None class Tree: def __init__(self,node=None): self.root = node def append(self,item): node = Node(item) if self.root is None: self.root = node else: queue = [self.root] while queue: current=queue.pop(0) if current.left is None: current.left = node return elif current.right is None: current.right = node return else: queue.append(current.left) queue.append(current.right) def loop(self): if self.root is None: return queue=[self.root]#广度优先 while queue: current=queue.pop(0) print(current.ele) if current.left!=None: queue.append(current.left) elif current.right!=None: queue.append(current.right) def deep_loop_top(self,node): if node==None: return print(node.ele) self.deep_loop_top(node.left) self.deep_loop_top(node.right) def deep_loop_left(self,node): if node==None: return self.deep_loop_left(node.left) print(node.ele) self.deep_loop_left(node.right) def deep_loop_last(self,node): if node==None: return self.deep_loop_last(node.left) self.deep_loop_last(node.right) print(node.ele)
学而不用则罔 用而不学则殆