堆栈是一个后进先出的数据结构,其工作方式就像一堆汽车排队进去一个死胡同里面,最先进去的一定是最后出来。
我们可以设置一个类,用列表来存放栈中元素的信息,利用列表的append()和pop()方法可以实现栈的出栈pop和入栈push的操作,list.append(obj)意思是向列表添加一个对象obj,list.pop(index=-1)意思是删除指定位置的对象,默认是最后一个对象,也就是说list.pop(),是删除列表中下标最大的元素。可先将Stack类写入文件stack.py,在其它程序文件中使用from stack import Stack,然后就可以使用堆栈了。
stack.py的程序:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Stack():
def __init__(self,size):
self.size=size
self.stack=[]
self.top=-1
def push(self,ele): #入栈之前检查栈是否已满
if self.isfull():
raise exception("out of range")
else:
self.stack.append(ele)
self.top=self.top+1
def pop(self): # 出栈之前检查栈是否为空
if self.isempty():
raise exception("stack is empty")
else:
self.top=self.top-1
return self.stack.pop()
def isfull(self):
return self.top+1==self.size
def isempty(self):
* E( F: ^9 |( U0 \1 {3 M( Zreturn self.top==-1
: H. Z- M( N. X6 D再写一个程序文件,stacktest.py 使用栈,内容如下:
代码如下:
1
2
3
4
5
6
7
#!/usr/bin/python
from stack import Stack
s=Stack(20)
for i in range(3):
s.push(i)
s.pop()
6 h$ X% h6 W" k/ @& w, tprint s.isempty()
weibo.com/ttarticle/p/show?id=2309404963800309956654
weibo.com/ttarticle/p/show?id=2309404963800343511106
weibo.com/ttarticle/p/show?id=2309404963800368676885
weibo.com/ttarticle/p/show?id=2309404963800406425690
weibo.com/ttarticle/p/show?id=2309404963800439980172
weibo.com/ttarticle/p/show?id=2309404963800465145966
weibo.com/ttarticle/p/show?id=2309404963800485855281
weibo.com/ttarticle/p/show?id=2309404963800670666827
weibo.com/ttarticle/p/show?id=2309404963800725192722
weibo.com/ttarticle/p/show?id=2309404963800762941457
weibo.com/ttarticle/p/show?id=2309404963800809078882
weibo.com/ttarticle/p/show?id=2309404963800834244724
weibo.com/ttarticle/p/show?id=2309404963800855216133
weibo.com/ttarticle/p/show?id=2309404963800871993373
weibo.com/ttarticle/p/show?id=2309404963800892702727
weibo.com/ttarticle/p/show?id=2309404963800913936450
weibo.com/ttarticle/p/show?id=2309404963800934908008
weibo.com/ttarticle/p/show?id=2309404963800955617286
weibo.com/ttarticle/p/show?id=2309404963800985239578
weibo.com/ttarticle/p/show?id=2309404963801002016902
weibo.com/ttarticle/p/show?id=2309404963801052086305
weibo.com/ttarticle/p/show?id=2309404963801115000889
weibo.com/ttarticle/p/show?id=2309404963801157206086
weibo.com/ttarticle/p/show?id=2309404963801199149090
weibo.com/ttarticle/p/show?id=2309404963801232703534
weibo.com/ttarticle/p/show?id=2309404963801270452288
weibo.com/ttarticle/p/show?id=2309404963801291423986
weibo.com/ttarticle/p/show?id=2309404963801316589612
weibo.com/ttarticle/p/show?id=2309404963801337561139
0 l# D% S% L* _. f; Z, w5 nweibo.com/ttarticle/p/show?id=2309404963801358532740
python 实现队列
队列是一种先进先出的数据类型,它的跟踪原理类似于在超市收银处排队,队列里的的第一个人首先接受服务,新的元素通过入队的方式添加到队列的末尾,而出队就是将队列的头元素删除。
我们可以设置一个类,用列表来存放栈中元素的信息,利用列表的append()和pop()方法可以实现队列的入队enqueue和出队dequeue的操作,上面栈一个元素每次出去是列表的最后一个,直接用list.pop()出栈,而出队列每次是第一个,所以要用list.pop(0)出队列
weibo.com/ttarticle/p/show?id=2309404964059492516280
weibo.com/ttarticle/p/show?id=2309404964059530264962
weibo.com/ttarticle/p/show?id=2309404964059572207761
weibo.com/ttarticle/p/show?id=2309404964059601568094
weibo.com/ttarticle/p/show?id=2309404964059920334999
weibo.com/ttarticle/p/show?id=2309404964059958083663
weibo.com/ttarticle/p/show?id=2309404964060083913002
weibo.com/ttarticle/p/show?id=2309404964060100689944
weibo.com/ttarticle/p/show?id=2309404964060113272898
weibo.com/ttarticle/p/show?id=2309404964060126118163
weibo.com/ttarticle/p/show?id=2309404964060138438902
weibo.com/ttarticle/p/show?id=2309404964060256141751
weibo.com/ttarticle/p/show?id=2309404964060268724386
weibo.com/ttarticle/p/show?id=2309404964060281307659
weibo.com/ttarticle/p/show?id=2309404964060293890641
weibo.com/ttarticle/p/show?id=2309404964060306211240
weibo.com/ttarticle/p/show?id=2309404964060449079625
weibo.com/ttarticle/p/show?id=2309404964060461400185
weibo.com/ttarticle/p/show?id=2309404964060474245719
weibo.com/ttarticle/p/show?id=2309404964060486828643
weibo.com/ttarticle/p/show?id=2309404964060709126637
weibo.com/ttarticle/p/show?id=2309404964060725903771
weibo.com/ttarticle/p/show?id=2309404964060742418546
weibo.com/ttarticle/p/show?id=2309404964060755263588
weibo.com/ttarticle/p/show?id=2309404964060767584572
weibo.com/ttarticle/p/show?id=2309404964060872442015
weibo.com/ttarticle/p/show?id=2309404964060906258715
weibo.com/ttarticle/p/show?id=2309404964060931162556
weibo.com/ttarticle/p/show?id=2309404964060973367685
weibo.com/ttarticle/p/show?id=2309404964061002465341
+ V% E2 _7 x: t, _代码如下:
1
23
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Queue():
def __init__(self,size):
self.size=size;
self.front=-1;
self.rear=-1;
self.queue=[];
def enqueue(self,ele): #入队操作
if self.isfull():
raise exception("queue is full");
else:
self.queue.append(ele)
self.rear=self.rear+1
def dequeue(self): #出队操作
if self.isempty():
raise exception("queue is empty")
else:
self.front=self.front+1
return self.queue[self.front]
def isfull(self):
return self.rear-self.front+1==self.size
def isempty(self):
return self.front==self.rear
q=Queue(10);
for i in range(3):
q.enqueue(i)
print q.dequeue()
" z& U( k) Y, W7 Qprint q.isempty()
| 欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) | Powered by Discuz! X3.2 |