-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_lnklst.py
More file actions
126 lines (111 loc) · 2.77 KB
/
stack_lnklst.py
File metadata and controls
126 lines (111 loc) · 2.77 KB
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class createnode:
def __init__(self,data):
self.data=data
self.next=None
class Unordered_list:
def __init__(self):
self.top=None
def createstack(self,data):
node=createnode(data)
if self.top is None:
self.top=node
else :
node.next=self.top
self.top=node
def printstack(self) :
temp=self.top
while(temp!=None):
print(temp.data)
temp=temp.next
def popstack(self):
temp=self.top
while(temp!=None):
if(temp==self.top):
temp=temp.next
break;
self.top=temp
class stackusingarray():
def __init__(self,maxsize=5):
self.list1=[]
self.maxsize=maxsize
def isfull(self):
if len(self.list1)==self.maxsize:
print("Stack Overflow")
return True
else :
return False
def isEmpty(b):
if len(b.list)==0:
print("Stack is empty")
return True
else :
return False
def push(self,data):
if not self.isfull():
self.list1.append(data)
print("Appended data :" ,data)
########Pop using normal funcion#
def pop(self):
return self.list1.pop()
######Pop using reducing the lenght of list####
def popwithdefination(self):
for i in range(len(self.list1)-1):
print(self.list1[i])
######Reverse a string using stack#####
def reversestack(self,strng):
b=list(strng)
strlen=len(strng)
var2=""
for i in range(0,strlen,1):
var2+=b.pop()
print(var2)
########Passing an expression#######
def reverseexpr(self,A):
splt=list(A)
empty_list=[]
strlen=len(splt)
for i in range(0,strlen,1):
if (splt[i] == "(" or splt[i]== "[" or splt[i]== "{"):
empty_list.append(splt[i])
print(empty_list)
elif splt[i]==")" or splt[i]== "]" or splt[i]== "}":
scnd_len=len(empty_list)
print(scnd_len)
if scnd_len>0:
last_scand_element=empty_list.pop()
else :
break
print(last_scand_element)
if((last_scand_element=="(" and splt[i]==")") or(last_scand_element=="[" and splt[i]=="]") or(last_scand_element=="{" and splt[i]=="}")):
continue;
else:
print("List is invalid");
exit();
if not empty_list and scnd_len>0:
print("list is valid ")
else :
print("List is invalid")
a=Unordered_list()
b=stackusingarray()
b.push(30)
b.push(40)
b.push(40)
b.push(40)
b.popwithdefination()
c=b.pop()
print("the last popped out item is ")
strng=raw_input("enter the string you want to reverse:")
b.reversestack(strng)
A="[A+B-{C%D}]"
b.reverseexpr(A)
#a.createstack(20)
#a.createstack(30)
#a.createstack(40)
#a.printstack()
#print("Let's PopOut")
#a.popstack()
#a.printstack()
#print("stack using array")
#a.createarrstack()
#print("pop using array")
#A.poparrstack()