Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python code please Use linked lists in order to support the following operations

ID: 3777744 • Letter: P

Question

python code please

Use linked lists in order to support the following operations: Push (S, x). Pushes a value x into a stack S Pop (S, i). Gets a number i and pops i numbers of S Reverse(S). Reverse the priority of the elements in 5 (you might want to apply recursion). If for example S is a stack and x was the last inserted, from now on x is treated as the first inserted element. QUEUE (x, S). Declares that from this moment S becomes and acts like a queue ENQUEUE (S, x). DEQUEUE(S): adds and removes elements when 5 is a queue. ST AC K(S). Makes S into a stack Average(S). Returns the average of the numbers in S For every operation state its running time.

Explanation / Answer

Please find below the python code :

class Stack: //Class stack
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self,item): //Push(S, x)
self.items.append(item)
return self.items
def pop(self): //Pop(S, i)
if self.items == []:
return 0
self.items.pop()
def peek(self):
if self.items == []:
return 0
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)

def getStackItems(self): // STACK(S)
return self.items

def reverse(string): //Reverse(S)
stack = Stack()
ls = []
newstr = ""
for i in string:
stack.push(i)
ls = stack.getStackItems()
for j in range(len(ls)):
newstr += ls.pop()
print(newstr)

reverse("testing")

class Queue: //Queue Implementation
def __init__(self):
self.items=[]
def enqueue(self,item): //ENQUEUE(S, x)
self.items.insert(0,item)
def dequeue(self): //DEQUEUE(S)
if(not self.isEmpty()):
return self.items.pop()
def isEmpty(self):
return self.items==[]
def size(self):
return len(self.items)

All above operations are O(1)