数据结构Python实现之队列,双端队列以及栈

队列:

class Node(object):
    def __init__(self,value=None,next=None):
        self.value = value
        self.next = next

class Linked_List(object):
    def __init__(self,maxsize=None):
        self.maxsize=maxsize
        self.head = Node()
        self.tail = None
        self.length = 0
    def __len__(self):
        return self.length
    def append(self,value):
        if self.maxsize is not None and len(self)>self.maxsize:
            raise Exception('Linked_List is full')
        node = Node(value)
        tailnode = self.tail
        if tailnode is None:
            self.head.next = node
        else:
            tailnode.next = node
        self.tail = node
        self.length += 1
    def appendleft(self,value):
        node = Node(value)
        head_node = self.head.next
        self.head.next = node
        node.next = head_node
        self.length += 1
    def popleft(self):
        if self.length == 0:
            raise Exception('Linked_List is empty')
        head_node = self.head.next
        head_node_next = head_node.next
        self.head.next = head_node_next
        self.length -= 1
        value = head_node.value
        del head_node
        return value
    def iter_node(self):
        cur_node=self.head.next
        while cur_node.next != None:
            yield cur_node
            cur_node =cur_node.next
        yield cur_node
    def __iter__(self):
        for node in self.iter_node():
            yield node.value
class Queue_Empty_Error(Exception):
    pass
class Queue_Full_Error(Exception):
    pass

class Queue(object):
    def __init__(self,maxsize=None):
        self.maxsize = maxsize
        self._item_link_list = Linked_List(maxsize)
    def __len__(self):
        return len(self._item_link_list)
    def push(self,value):#队尾添加元素
        if len(self) >= self.maxsize:
            raise Queue_Full_Error('full queue')
        self._item_link_list.append(value)
    def pop(self):#队列头部删除元素
        if len(self) == 0:
            raise Queue_Empty_Error('empty queue')
        return self._item_link_list.popleft()
    def iter_queue(self):#遍历队列
        for q in self._item_link_list.iter_node():
            yield q.value
    def __iter__(self):#输出队列值
        for q in self._item_link_list:
            yield q

q = Queue(5)#队列的最大长度为5
q.push(1)
q.push(2)
q.push(3)
print('队列的长度:',len(q))
q.push(1)
q.push(2)
print('队列的长度:',len(q))
print('遍历队列:')
j = 1
for i in q:
    print('队列的第%d个值为:%d'%(j,i))
    j += 1

q.pop()
print('队列的长度:',len(q))
q.pop()
print('队列的长度:',len(q))
q.pop()
print('队列的长度:',len(q))
print('遍历队列:')
j = 1
for i in q.iter_queue():
    print('队列的第%d个值为:%d'%(j,i))
    j += 1
'''
输出:
队列的长度: 3
队列的长度: 5
遍历队列:
队列的第1个值为:1
队列的第2个值为:2
队列的第3个值为:3
队列的第4个值为:1
队列的第5个值为:2
队列的长度: 4
队列的长度: 3
队列的长度: 2
遍历队列:
队列的第1个值为:1
队列的第2个值为:2

'''

双端队列:

class Node(object):
    def __init__(self,value=None,prev=None,next=None):
        self.value = value
        self.prev = prev
        self.next = next
class C_Double_Linked_List(object):#循环双端列表
    def __init__(self):
        node = Node()
        node.next = node
        node.prev = node
        self.head = node
        self.length = 0
    def __len__(self):
        return self.length
    def headnode(self):
        return self.head.next
    def tailnode(self):
        return self.head.prev
    def append(self,value):#从末尾添加
        node = Node(value)
        tail_node = self.tailnode()
        tail_node.next = node
        node.prev = tail_node
        node.next = self.head
        self.head.prev = node
        self.length += 1
    def appendleft(self,value):#从开头添加
        node = Node(value)
        if self.head.next is self.head:
            node.next = self.head
            node.prev = self.head
            self.head.next = node
            self.head.prev = node
        else:
            head_node = self.headnode()
            self.head.next = node
            node.prev = self.head
            node.next = head_node
            head_node.prev = node
        self.length += 1
    def remove(self):#从末尾删除
        if self.length == 0:
            return
        else:
            tail_node = self.tailnode()
            tail_node_prev = tail_node.prev
            tail_node_prev.next = self.head
            self.head.prev = tail_node_prev
        self.length -= 1
        return True
    def removeleft(self):#从开头删除
        if self.length == 0:
            return
        else:
            head_node = self.headnode()
            head_node_next = head_node.next
            self.head.next = head_node_next
            head_node_next.prev = self.head
        self.length -= 1
        return True
    def iter_node(self):
        if self.length == 0:
            return
        cur_node = self.head.next
        while cur_node.next is not self.head:
            yield cur_node
            cur_node = cur_node.next
        yield cur_node
    def __iter__(self):
        for node in self.iter_node():
            yield node.value


class Deque(C_Double_Linked_List):#双端队列
    def push(self,value):#从双端队列的末尾添加节点
        self.append(value)
    def pushleft(self,value):#从双端队列的开头添加节点
        self.appendleft(value)
    def pop(self):#从双端队列末尾删除节点
        self.remove()
    def popleft(self):
        self.removeleft()

dq = Deque()
dq.push(1)
dq.push(3)
dq.push(4)
print('双端队列的长度为:',len(dq))
dq.pushleft(7)
dq.pushleft(8)
dq.pushleft(9)
print('双端队列的长度为:',len(dq))
i=1
print('遍历队列:')
for node in dq:
    print('第%d个节点的值:%d'%(i,node))
    i += 1
print('出对操作······')
dq.pop()
dq.pop()
print('双端队列的长度为:',len(dq))
print('遍历队列:')
i=1
for node in dq:
    print('第%d个节点的值:%d'%(i,node))
    i += 1
dq.popleft()
dq.popleft()
print('双端队列的长度为:',len(dq))
print('遍历队列:')
i=1
for node in dq:
    print('第%d个节点的值:%d'%(i,node))
    i += 1
'''
输出:
双端队列的长度为: 3
双端队列的长度为: 6
遍历队列:
第1个节点的值:9
第2个节点的值:8
第3个节点的值:7
第4个节点的值:1
第5个节点的值:3
第6个节点的值:4
出对操作······
双端队列的长度为: 4
遍历队列:
第1个节点的值:9
第2个节点的值:8
第3个节点的值:7
第4个节点的值:1
双端队列的长度为: 2
遍历队列:
第1个节点的值:7
第2个节点的值:1
'''

栈:

class Stack(object):
    def __init__(self):
        self.deque = Deque()
    def push(self,value):
        self.deque.push(value)
    def pop(self):
        self.deque.pop()
    def __len__(self):
        return len(self.deque)
    def __iter__(self):
        for node in self.deque:
            yield node
s = Stack()
s.push(10)
s.push(20)
s.push(30)
s.push(40)
s.push(50)
print('栈的长度为:',len(s))
for i in s:
    print(i)
s.pop()
s.pop()
s.push(100)
s.pop()
s.push(200)
print('栈的长度为:',len(s))
for i in s:
    print(i)

'''
输出:
栈的长度为: 5
10
20
30
40
50
栈的长度为: 4
10
20
30
200
'''


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值