Beispiel #1
0
 def enQueueRear(self, data):  # it takes 1 parameter as data
     node = Node(data)
     if self.front is None and self.rear is None:
         self.front = node  # if front and rear == None
     else:  #
         node.next = self.front  # add using front
         self.front = node
     print(node.data)
    def sPush(self, data):  # it takes 1 parameter
        node = Node(data)

        if self.top is None:  # if is -1 push node else increment top value then push it
            self.top = node
        else:
            node.next = self.top
            self.top = node  # assign node to top
        self.size += 1  # increment size
    def insert(self, data, pos):  # it takes two parameter as data and position
        prev = None  # prev is pointing to Null
        temp = self.first  # temp is pointing to first
        new_node = Node(data)  # creating Node class object and passing data

        if pos > 0 or pos < self.size:  # if pos > 0 and < size then insert node in between two nodes
            for i in range(0, self.size):
                if i == pos:  # if pos==i then we got position
                    self.size += 1  # increment size value
                    new_node.next = temp  # set new node to temp
                    prev.next = new_node  # prev to new node
                    break
                else:  # else point temp and prev to next node
                    prev = temp  # prev = temp
                    temp = temp.next  # temp = temp.next
 def enQueue(self, data):  # it takes 1 parameter
     node = Node(data)
     if self.rear is None:  # if rear == None assign front and rear to node
         self.front = node
         self.rear = node
     else:  # else increment Rear value
         self.rear.next = node
         self.rear = self.rear.next
     self.size += 1
 def add(self, data):  # it takes data as parameter
     n = Node(data)  # creating Node class object and passing data
     if self.size == 0:  # if size == 0 Add node at 1 position
         self.first = n
         self.last = n
     else:  # if size !=0 then add it to next
         self.last.next = n
         self.last = n
     self.size += 1  # incrementing size value
    def orderedAdd(self, data):  # it takes Data as parameter
        node = Node(data)  # creating Node class object and passing data
        temp = self.first  # temp is pointing to first
        if self.size == 0:  # if size == 0 Add node at 1 position
            self.first = node
            self.last = node
        else:  # if size !=0 then add it to next
            while temp is not None:  # it loops util temp == Null
                if temp.data > data:  # if temp.data > data use bubble sort technique to swap it
                    d1 = node.data
                    node.data = temp.data
                    temp.data = d1

                temp = temp.next
            self.last.next = node
            self.last = node

        self.size += 1  # increment size value
Beispiel #7
0
    def enQueueFront(self, data):  # it takes 1 parameter as data
        node = Node(data)

        if self.front is None and self.rear is None:
            self.rear = node  # # if front and rear == None

        else:

            self.rear.next = node  # add using rear
            self.rear = node