![]() ![]() Now that we have a conceptual model of a singly-linked list, let's explore the operations of a singly-linked list. When we start sequencing these individual steps to form a sequence of steps, we are creating a scavenger hunt. "Visit these latitude and longitude coordinates"). "You've reached France") and pointers to the next step (e.g. The nodes of a singly-linked list are very similar to the steps in a scavenger hunt. ![]() Each node, in turn, contains data and a pointer, which can point to another node. In computer science, a singly-linked list is a data structure that holds a sequence of linked nodes. If you're curious about the relationship between a scavenger hunt and a linked list, then read below for the answer! A Singly-Linked List A grocery list was more analogous to a queue a train was more analogous to an array.Īs more time passed, I eventually discovered an analogy that accurately described a singly-linked list and a doubly-linked list: a scavenger hunt. These analogies, as I eventually learned, were inaccurate. What I heard were several examples, such as a list of groceries and a train. When I was taught these data structures, I asked my peers for analogies to conceptualize them. get_node (index ) if node is None:Ī_cdllist.Two of the most commonly taught data structures in computer science are the singly-linked list and doubly-linked list. insert_before (ref_node, new_node ) elif operation = 'remove': insert_after (ref_node, new_node ) elif suboperation = 'before':Ī_cdllist. Print ( 'No such index.' ) continue if suboperation = 'after':Ī_cdllist. insert_at_beg (new_node ) elif position = 'end':Ī_cdllist. lower ( ) if suboperation = 'at':Ī_cdllist. ![]() first is None:Ī_cdllist = CircularDoublyLinkedList ( ) print ( 'Menu' ) print ( 'insert after ' ) print ( 'insert before ' ) print ( 'insert at beg' ) print ( 'insert at end' ) print ( 'remove ' ) print ( 'quit' ) while True:ĭo = input ( 'What would you like to do? ' ). next def display ( self ): # This method is for showing elements present in the list if self. first = new_nodeĭef remove ( self, node ): #This method is for deleting elements from the list if self. prev, new_node ) def insert_at_beg ( self, new_node ): #This method is for inserting elements into the list when the user selects insert at beginning option self. prev, new_node ) def insert_at_end ( self, new_node ): #This method is for inserting elements into the list when the user selects insert at end option if self. first:ĭef insert_after ( self, ref_node, new_node ): #This method is for inserting elements into the list when the user selects insert after optionĭef insert_before ( self, ref_node, new_node ): #This method is for inserting elements into the list when the user selects insert before option self. first for i in range (index ):Ĭurrent = current. first = None def get_node ( self, index ):Ĭurrent = self. prev = None class CircularDoublyLinkedList: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |