def push(self, item):
        ## Regular stack
        n1 = Node(item)
        n1.next = self.top
        self.top = n1

        ##stack maintaing minimum
        if self.topMinimum == []:
            self.topMinimum.append(item)
        else:
            if item < self.topMinimum[len(self.topMinimum) - 1]:
                self.topMinimum.append(item)
            else:
                self.topMinimum.append(self.topMinimum[len(self.topMinimum) - 1])
def Partition(l,x):
     if(l.search(x)):
         current=l.head
         #index=1
         while(current.getData()!=x):
             current=current.getNext()
             #index=index+1
         print("Current Element is",current.getData())
         #print("Current Element index is",index)
         temp=l.head
         previous=Node()
         previous.setNext(temp)
         tempIndex=1
         
         while(temp!=None):
             print("************")
             print("Previous is",previous.getData())
             print("Temp is",temp.getData())
             #print("current.getNext() is",current.getNext())
             print("************")
             exchange=False
             if(temp.getData()>current.getData()):
                 if(temp==l.head):
                     l.head=temp.getNext()
                 previous.setNext(temp.getNext())
                 temp.setNext(current.getNext())
                 current.setNext(temp)       
                 temp=previous
                 exchange=True
                 #l.show
                 
             if(temp.getData()<current.getData()):
                 previous.setNext(temp.getNext())
                 temp.setNext(l.head)
                 l.head=temp
                 temp=previous
                 exchange=True
             
             
             if(not exchange):
                 previous=previous.getNext()
             temp=previous.getNext()
             
             
             l.show()
     else:
        print("Item Not Present in LinkedList")
        return -1  
 def _treeLinkList(self,Node,l):
     
     if(len(l)==0):
         ll=LinkedList()
         if(Node.getLeftChild()):
             ll.addNode(Node.getLeftChild())
             lChild=True
         else:
             print("End of Tree:No more left Child")
             lChild=False
             return
         if(Node.getRightChild()):
             ll.addNode(Node.getRightChild())
             rChild=True
         else:    
             print("End of Tree:No more right Child")
             rChild=False
             return
         l.append(ll)
         if(lChild):
             Node=Node.getLeftChild()
             self._treeLinkList(Node, l)
     
     if(len(l)>0):
         ll=LinkedList()
         self.completeTreeLL(l,ll,Node)
         if(Node.getLeftChild()):
             Node=Node.getLeftChild()
             self._treeLinkList(Node, l)
def  iSort(l): 
     current=l.head.getNext()
     previous=l.head
     previous.setNext(current)
     
     while(current!=None):
         exchange=False
         tempCurrent=l.head
         tempPrevious=Node()  
         tempPrevious.setNext(tempCurrent)
         #print("******************************")  
         #print("Current is",current.getData())
         #print("Previous is",previous.getData())
         
         while(tempCurrent!=current and (not exchange)):
             #print("tempCurrent is",tempCurrent.getData())
             #print("tempPrevious is",tempPrevious.getData())
             
             if (tempCurrent.getData()>current.getData()):
                 if(tempCurrent==l.head):
                     previous.setNext(current.getNext())
                     current.setNext(tempCurrent)
                     tempPrevious.setNext(current)
                     l.head=current
                     previous=current
                     current=current.getNext()
                     exchange=True
                     break
                 
                 previous.setNext(current.getNext())
                 current.setNext(tempCurrent)
                 tempPrevious.setNext(current)
                 current=previous.getNext()
                 exchange=True
                 break
             
             tempCurrent=tempCurrent.getNext()
             tempPrevious=tempPrevious.getNext()
         if(not exchange):
             current=current.getNext()
             previous=previous.getNext()
         
         #l.show()    
     return l            
def removeDuplicate(l):
     print("Original Linked list")
     l.show()
     l= iSort(l)
     print("Final Sorted Linked List is")
     l.show()
     current=l.head
     previous=Node()
     previous.setNext(current)
     while(current.getNext()!=None):
         print("Current is",current.getData())
         print("Previous is",previous.getData())
         if(current.getData()==current.getNext().getData()):
             while(current.getData()==current.getNext().getData()):
                 current=current.getNext()
             previous.setNext(current)
         previous=current
         current=current.getNext()    
         l.show()    
示例#6
0
 def push(self,item):
     self.size=self.size+1
     n1=Node(item) 
     n1.next=self.top
     self.top=n1