Coada

class Nod: def __init__(self,data): self.next=None; self.data=data class Coada: def __init__(self,primu=None): self.primu=primu self.lungime=0 def ENQUEUE(self,data): #adauga un element la finalul cozi nod_nou=Nod(data) nod_nou.next = None if self.primu is None: nod_nou.anteriorul = None self.primu = nod_nou self.lungime+=1 return ultimul = self.primu while ultimul.next is not None: self.lungime+=1 ultimul = ultimul.next ultimul.next = nod_nou nod_nou.anteriorul = ultimul return def DEQUEUE(self): #sterge primul element din fata si returneaza elementul sters if self.primu is None: return None curent=self.primu self.primu=self.primu.next self.lungime-=1 return curent.data def size(self): #returneza lunginmea cozi return self.lungime def front(self): #returneaza primul element din fata fara sa stearga return self.primu.data def print(self): curent=self.primu while curent is not None: print(curent.data) curent=curent.next c=Coada() c.ENQUEUE(1) c.ENQUEUE(2) c.ENQUEUE(3) c.ENQUEUE(4) c.print() print("primul element a cozi este {}".format(c.front())) print("\n") c.DEQUEUE() print("\n") c.print() print("\n") print("Lungime cozi este {}".format(c.size())) c.DEQUEUE() c.DEQUEUE() print("Lungime cozi este {}".format(c.size())) c.DEQUEUE() print("Lungime cozi este {}".format(c.size())) c.DEQUEUE() print("Lungime cozi este {}".format(c.size()))
Implementarea unei Cozi

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.