About
class List(): class Node(): def _init_(self,value): self.value = value self.next = None
def _init_(self):
self.head = self.Node(None)
self.s = 0
def listSize(self):
return self.s
def isEmpty(self):
return (self.s==0)
def printList(self):
if self.s!=0:
i = self.head
while (i.next):
print(i.value)
i = i.next
print(i.value)
else:
print("Empty")
def insertFirst(self, value):
newnode = self.Node(value)
if self.s != 0:
newnode.next = self.head
self.head = newnode
self.s+=1
def insertLast(self, value):
newnode = self.Node(value)
i = self.head
while (i.next):
i = i.next
i.next = newnode
self.s += 1
def deleteFirst(self):
if self.s!=0:
self.head.value = None
self.head = self.head.next
else:
print("Empty")
self.s -= 1
def deleteLast(self):
if self.s>1:
i = self.head
while (i.next.next):
i = i.next
i.next.value = None
i.next = None
self.s -= 1
elif self.s == 1:
self.deleteFirst()
else:
print("Empty")
def testSLL(): sll = List() print(sll.isEmpty()) sll.insertFirst(10) sll.insertFirst(20) sll.insertFirst(30) sll.insertLast(40) sll.insertLast(50) sll.insertLast(60) sll.printList() print(sll.listSize()) sll.deleteFirst() sll.deleteLast() sll.printList() sll.deleteLast() sll.deleteLast() sll.deleteLast() sll.printList() sll.deleteLast() sll.deleteLast() sll.printList() print(sll.listSize()) print(sll.isEmpty())
def main(): testSLL()
if _name_ == '_main_': main()
class MyQueue():
def _init_(self,size):
# this is the queue container called 'queue'
self.queue = []
# front and back indexes
self.f = 0
self.r = -1
# define the queue size 'max_queue_size' and initialize it
self.max_queue_size = size
for i in range(0,self.max_queue_size):
self.queue.append(None)
self.sz=0
# define the enqueue operation which inserts the value into the queue, must throw a queue full exception("Queue Full Exception")
def enqueue(self, value):
#@start-editable@
if self.sz != self.max_queue_size:
if self.r+1 == self.max_queue_size:
self.r = ((self.r+1)%self.max_queue_size)
else:
self.r+=1
self.queue[self.r] = value
self.sz+=1
else:
print("Queue Full Exception")
#@end-editable@
return
# returns first elt of the queue if not empty, else throws "Queue Empty Exception"
def dequeue(self):
if self.size()==0:
print("Queue Empty Exception")
else:
#@start-editable@
obj = self.queue[self.f]
self.queue[self.f] = None
if self.f+1 == self.max_queue_size:
self.f = (self.f+1)%self.max_queue_size
else:
self.f += 1
self.sz-=1
if self.sz==0:
self.r = -1
self.f = 0
#@end-editable@
return obj
# returns front element without removing it if the queue is not empty, else throws exception ("Queue Empty Exception")
def front(self):
#@start-editable@
if self.sz != 0:
return self.queue[self.f]
else:
print("Queue Empty Exception")
#@end-editable@
# returns True if queue is empty
def isEmpty(self):
#@start-editable@
if self.sz == 0:
return True
return False
#@end-editable@
# returns the number of elements currently in queue
def size(self):
#@start-editable@
return self.sz
#@end-editable@
def printQueue(self):
if (self.isEmpty()):
print ("Queue Empty")
else:
for i in range(self.max_queue_size):
if self.queue[i]!=None:
print(self.queue[i],end=" ")
print(" ")
def testqueue():
queuesize=int(input())
q1 = MyQueue(queuesize)
inputs=int(input())
while inputs>0:
command=input()
operation=command.split()
if(operation[0]=="S"):
print(q1.size())
elif(operation[0]=="I"):
print(q1.isEmpty())
elif(operation[0]=="E"):
q1.enqueue(int(operation[1]))
elif(operation[0]=="D"):
print(q1.dequeue())
q1.printQueue()
elif(operation[0]=="Fr"):
print(q1.front())
elif(operation[0]=="F"):
print(q1.f)
elif(operation[0]=="R"):
print(q1.r)
elif(operation[0]=="P"):
q1.printQueue()
inputs-=1
def main(): testqueue()
if _name_ == '_main_': main()
class Mystack(): def __init__(self,size): self.s = [] self.max = size self.size = 0 self.top = -1 for i in range (0,self.max): self.s.append(None)
def isempty(self):
if (self.size == 0):
return(True)
return (False)
def isfull(self):
if (self.size == self.max):
return(True)
return(False)
def push(self,value):
if(self.size == self.max):
print("stack is full")
else:
self.top = self.top+1
self.s[self.top] = value
self.size = self.size + 1
def pop(self):
if (not(self.isempty())):
self.s[self.top] = None
self.top = self.top - 1
else:
print("Stack is empty")
def stop(self):
return (self.s[self.top])
def size(self):
return (self.size)
def display(self):
if(self.isempty()):
print("Stack is empty")
else:
for i in range (0,self.size):
print(self.s[i],end = " ")
print(" ")
print("Enter the size") a = int(input()) s = Mystack(a)
for i in range (0,a): x = int(input()) s.push(x)
s.display() print(s.stop())