10/25/2022 0 Comments Linked list stack java questions csci![]() ![]() data, ) class DoubleLinkedList : def _init_ (self ) : ![]() # _str_ returns string equivalent of Object def _str_ (self ) : return "Node" % (self. find ( 20 ) if found = True : print "The value 20 is present in the Linked List" else : print "The value 20 is not present in the linked list"Ĭlass Node : # Constructor to initialize data # If data is not given by user,its taken as None def _init_ (self, data = None, next = None, prev = None ) : find ( 2 ) if found = True : print "The value 2 is present in the Linked List" else : print "The value 2 is not present in the linked list" # Try to find the value 20 in the linked list print "Try to find the value 20 in the linked list"įound = ll. display ( ) # Try to find the value 2 in the linked list (Demonstrating the Find function) print "Try to find the value 2 in the linked list"įound = ll. delete ( 3 ) print "Display the linked list again. Demonstrate the Delete function print "Delete an element (data = 3) from the linked list" display ( ) # Delete an element from the linked list. insert ( 9 ) # Display the linked list print "Displaying the linked list" Ll = LinkedList ( ) # Insert values in the linked list print "Inserting values 1,2,3,9 in the Linked List" # Initialize a new linked list print "Initializing linked list" String_representation + = str (current ) + "->" String_representation = " " while current ! = None : # Display the linked list # Traverse the linked list till you reach its end # Display each node which you traverse def display (self ) : While ( (current ! = None ) and (current. # Find a given data value in the linked list # Traverse the linked list till you either find the data value or you come to the end of the list def find (self ,data ) : If current = None : return False else : while (current ! = None ) and (current. next = Node (data ) # Deleting a given data value from the linked list # If the head contains this data value # Set head = node which comes next after the current head # Otherwise go to the node such that the node after it (next to it) contains the value we're looking for # set node.next = # so, the node which dontains the specified value/data is skipped def delete (self ,data ) : next ! = None) and (current.data = data): current = current. # A new node is created for this data element # And the last pointer points to this def insert ( self, data ) : if ( self. head = None # Inserting new data at the end of the list # Iterate through the list till we encounter the last node. # You will notice how the first two cities have been removed from the Queue, in FIFO order print "Current state of the queue after dequeing two cities:" print queueĬlass Node : def _init_ ( self, data = None, next = None ) :ĭef _str_ ( self ) : return "Node" class LinkedList : def _init_ ( self ) : Print "First Dequeued city:" print dequedElement2 # Given that a Queue is a First In First Out (FIFO) data structure the de-queued elements will be London and Paris print "Two cities were de-queued" print "First Dequeued city:" print dequedElement1 popleft ( ) # Let us display the Dequeued Elements. # You will observe that Mumbai and Kolkata have been en-queued at the end of the queue # Now let us start to De-que element from this queueĭequedElement2 = queue. ![]() ![]() append ( "Kolkata" ) # Now display the queue after en-queueing these print queue Queue = deque ( ) print "The Original Queue:" print queue # Deques from collections are convenient to use as Queues # It is not efficient to use lists because they are efficient for reading/appending/popping from the end # But they are not so efficient for dequeing from the beginning from collections import deque ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |