0
class Node:
    def __init__(self,data):
        self.data = data
        self.pref = None
        self.nref = None

class DoublyLL:
    def __init__(self):
        self.head=None
    def print_LL_forward(self):       
        if self.head is None:
            print("Linked List is Empty! ")
        else:
            n = self.head
            while n is not None:
                print(n.data , "-->" ,end=" ")
                n = n.nref
    def print_LL_reverse(self):
        print()
        if self.head is None:
            print("Linked List is Empty! ")
        else:
            n=self.head
            while n.nref is not None:
                n = n.nref
            while n is not None:
                print(n.data , "-->" ,end=" ")
                n = n.pref
    def insert_empty(self,data):
        if self.head is None:
            new_node = Node(data)
            self.head = new_node
        else:
            print("LL is not Empty")
    def add_begin(self,data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
        else:
            new_node.nref = self.head
            self.head.pref = new_node
            self.head = new_node 
    def add_end(self,data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
        else:
            n = self.head
            while n.nref is not None:             
                 n = n.nref
            n.nref = new_node
            new_node.pref = n
    def add_after(self,data,x):
        if self.head is None:
            print("LL is empty , so Node cannot be added! ")
        else:
            n = self.head
            while n is not None:
                if x==n.data:
                    break
                n = n.nref
            if n is None:
                print("Node is not present in LL")
            else:
                new_node = Node(data)
                new_node.nref = n.nref
                new_node.pref = n
                if n.nref is not None:
                    n.nref.pref = new_node
                n.nref = new_node
        
    def add_before(self,data,x):
         if self.head is None:
            print("LL is empty , so Node cannot be added! ")        
         else:
             n = self.head
             while n is not None:
                if x==n.data:
                    break
                n = n.nref
             if n is None:
                print("Node is not present in LL")
             else:                
                 new_node = Node(data)
                 new_node.pref = n
                 new_node.nref = n.pref
                 if n.pref is not None:
                    n.pref.nref = new_node
                 else:                   
                     self.head = new_node
                 n.pref = new_node
                
                
         
 
dl1 = DoublyLL()
dl1.add_begin(30)
dl1.add_end(50)
dl1.add_before(40,30)
dl1.print_LL_forward()
dl1.print_LL_reverse()

Why is this program printing continuously in a loop rather than displaying it for only a single time?

It always crashes my compiler.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 2
    Welcome to Stack Overflow. Sadly, too much code to debug. Your expectation for the output is not clear too. Please take a [tour] and see [How to ask](https://stackoverflow.com/help/how-to-ask). – doneforaiur Aug 09 '23 at 17:23
  • 2
    If it "crashes your compiler", how can it produce any output at all? – Scott Hunter Aug 09 '23 at 17:23
  • 1
    Hello and welcome to StackOverflow! Could you edit your post and show us what output you're getting and what crash you're seeing? – Daniel Walker Aug 09 '23 at 17:25
  • `add_before` looks wonky. Focus your debugging on that. It will probably be helpful if you write down a visual representation of the list before and after the operation. – 001 Aug 09 '23 at 17:40
  • 1
    @ScottHunter, I think the OP means that it's crashing the interpreter. – Daniel Walker Aug 09 '23 at 17:40
  • 1
    You might want to learn [how to step through your code to help debug](/q/4929251/4518341). More resources: [How to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, [mre] for Stack Overflow questions. – wjandrea Aug 09 '23 at 17:50

0 Answers0