Example #1
0
 def enqueue(self,value):
     if self.front == None and self.rear == None:
         self.front = Node(value)
         self.rear = self.front
     else:
         self.rear.setNext(Node(value))
         self.rear = self.rear.getNext()
 def insert(self,pos,value):
     if pos == 0:
         self.add(value)
         return 0
     prev = self.head
     curr = prev.getNext()
     count = 1
     while not count == pos:
         prev = curr
         curr = curr.getNext()
         count += 1
     tempNode = Node(value)
     tempNode.setNext(curr)
     prev.setNext(tempNode)
 def push(self, value):
     if self.top == None:
         self.top = Node(value)
     else:
         temp = Node(value)
         temp.setNext(self.top)
         self.top = temp
class LinkedStack:
    def __init__(self):
        self.top = None

    def push(self, value):
        if self.top == None:
            self.top = Node(value)
        else:
            temp = Node(value)
            temp.setNext(self.top)
            self.top = temp

    def pop(self):
        if self.top != None:
            self.top = self.top.getNext()
        else:
            print("Cannot perform pop() : Stack is Empty")

    def isEmpty(self):
        return self.top == None

    def size(self):
        curr, count = self.top, 0
        while curr != None:
            count += 1
            curr = curr.getNext()
        return count

    def topStack(self):
        return self.top.getData()

    def __str__(self):
        curr, string = self.top, ""
        while curr != None:
            string += str(curr.getData()) + " "
            curr = curr.getNext()
        return string
Example #5
0
class LinkedQueue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self,value):
        if self.front == None and self.rear == None:
            self.front = Node(value)
            self.rear = self.front
        else:
            self.rear.setNext(Node(value))
            self.rear = self.rear.getNext()

    def dequeue(self):
        if self.front == None:
            print("The Queue is Empty")
        else:
            self.front = self.front.getNext()
            if self.front == None:
                self.rear = None

    def isEmpty(self):
        return self.front == None

    def size(self):
        curr,count = self.front,0
        while curr != None:
            count += 1
            curr = curr.getNext()
        return count

    def __str__(self):
        curr,string = self.front,""
        while curr != None:
            string += str(curr.getData()) + " "
            curr = curr.getNext()
        return string
 def add(self,newvalue):
     temp = Node(newvalue)
     temp.setNext(self.head)
     self.head = temp
 def append(self,value):
     curr = self.head
     while not curr.getNext() == None:
         curr = curr.getNext()
     curr.setNext(Node(value))