About
ADT in python:
class myStack(): def __init__ (self, size): self.s=[]; self.max=size; self.top=-1; for i in range(0, self.max): self.s.append(none); def push(self, value): self.top= self.top+1; self.s[self.top]=value; def pop(self): if(not(s.isEmpty())): self.s[self.top]=none; self.top=self.top-1; else: print("stack is empty"); def isEmpty(self): if (self.s.top==-1): return True; else: return False; def stackSize(self): return (self.top+1); def printStack(self): if (self.isEmpty()==True): print("stack is empty"); else: for i in (0,self.top+1): print(self.s[i], sep=" "); def topElement(self): print(self.s[self.top]); ''' def printStack(self): for i in (0, self.max): if (self.s[i]!=none): print(self.s[i], sep=" "); else: pass; '''
a=myStack(6) a.push(int(input())) print(k.isEmpty()) a.printStack();
class myqueue(): def __init__ (self, size): self.q=[]; self.f=0; self.r=-1; self.max=size; for i in range (0, self.max): self.q.append(None); self.size=0; def enqueue(self, value): if(self.size==self.max): print("queue is full"); else: self.r=self.r+1; self.q[self.r]=value; self.size=self.size+1;
def dequeue(self):
if(self.size==0):
print("queue is empty")
else:
self.q[self.f]=None;
self.f=self.f+1;
self.size=self.size-1;
def isEmpty(self):
return (self.size==0);
def queuesize(self):
print(self.size);
def front(self):
print(self.q[self.f]);
def rear(self):
print(self.q[self.r]);
k=myqueue(5); k.enqueue(20); k.enqueue(30); k.enqueue(40); k.front(); k.rear(); k.dequeue(); k.front();
class slist(): class node(): def _init_(self, data): self.element = data self.next = None
def _init_(self):
self.head = self.node(None)
self.size = 0
def insertfirst(self, item):
newnode = self.node(item)
if self.size == 0:
self.head.element = newnode.element
else:
newnode.next = self.head
self.head = newnode
self.size += 1
def isempty(self):
return (self.size == 0)
def printself(self):
if self.isempty():
print("list empty")
else:
currentnode = self.head
while currentnode is not None:
print(currentnode.element, end=" ")
currentnode = currentnode.next
def insertlast(self,item):
newnode=self.node(item)
currentnode=self.head
while(currentnode.next!=None):
currentnode=currentnode.next
currentnode.next=newnode
self.size += 1
def deletefirst(self):
if self.isempty():
print("list empty")
else:
if self.head.next==None:
self.head.element=None
else:
currentnode=self.head
del self.head
self.head=currentnode.next
self.size=self.size-1
def deletelast(self):
currentnode=self.head
previousnode=self.head
while(currentnode.next!=None):
previousnode=currentnode
currentnode=currentnode.next
previousnode.next=None
self.size-=1
l1 = slist()
l1.insertfirst(25) l1.insertfirst(35) l1.insertfirst(15) l1.insertfirst(20) l1.printlast(29) l1.deletefirst() l1.deletelast() l1.printself()