Python data structure and algorithm __java

Source: Internet
Author: User
The data structure is the way that the computer stores and organizes it. Data structures are collections of elements that have one or more specific relationships between each other. Typically, carefully chosen data structures can lead to higher operational or storage efficiencies. Data structures are often associated with efficient retrieval algorithms and indexing techniques. The logical structure of data: a data structure that reflects the logical relationship between data elements, in which the logical relationship refers to the Pre-and post relationships between data elements, regardless of where they are stored in the computer. The logical structure includes:
1. Collection
There is no relation between elements in the data structure except the relationship between "one set" and the other.
2. Linear structure
The elements in the data structure have one-to-one interrelationship;
3. Tree-shaped structure
There is a one-to-many relationship between the elements in the data structure;
4. Graphic structure
There are many pairs of correlations between elements in data structures. the definition of a linked list
1. The list (linked list) is a common basic data structure, is a linear table, but not as sequential table as the continuous storage of data, but in each node (data storage unit) to store the next node location information (that is, address).

2. A linked list is a discontinuous, non sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the sequence of pointer links in the linked list. A list consists of a series of nodes (each of which is called a node), which can be generated dynamically at runtime. Each node consists of two parts: one is the data field where the data element is stored, and the other is the pointer field that stores the next node address. Compared with the linear table sequence structure, the operation is complex. Because it does not have to be stored sequentially, the list can achieve an O (1) complexity when inserted, is much faster than another linear table sequence table, but it takes O (n) time to find a node or to access a specific number of nodes, and the corresponding time complexity of the linear table and the sequential table is O (logn) and O (1) respectively.

3. The use of the linked list structure can overcome the problem that the array list needs to know the data in advance, the list structure can make full use of the memory space of the computer and realize the flexible memory dynamic management. But the link list loses the advantage of array random reading, meanwhile, the space overhead is larger because the link list increases the pointer field of the ending point. The most obvious benefit of a linked list is that regular arrays can be arranged in a way that is different from the sequence of data items on the memory or on the disk, and the access to the data is often converted in different permutations. A linked list allows you to insert and remove nodes at any location on the table, but does not allow random access. There are many different types of lists: one-way linked lists, two-way lists, and circular lists. Linked lists can be implemented in a variety of programming languages. The built-in data types of languages such as Lisp and scheme contain the access and manipulation of linked lists. Programming languages or Object-oriented languages, such as c,c++ and Java, rely on variable tools to generate linked lists.

1. Single Linked list

#python3 class Node (object): "" Node "" Def __init__ (self, Element): Self.element = Element self.ne XT = None Class Singlelinklist (object): "" "" One-way List "" "Def __init__ (self): Self._head = None def empty ( Self): return self._head = = None def length (self): cursor = Self._head count = 0 Whil
        E cursor!= None:count + = 1 cursor = Cursor.next return Count def traversal (self): cursor = self._head while cursor!= none:print (cursor.element, end= ",") cursor = C Ursor.next def add (self, Element): node = node (element) Node.next = Self._head Self._head = n
        Ode def append (self, Element): node = node (element) if Self.empty (): Self._head = node
            Else:cursor = Self._head while cursor.next!= none:cursor = Cursor.next Cursor.next =Node def insert (self, position, Element): if position = = 0:self.add (element) Elif Posi
            tion > (Self.length ()-1): Self.append (Element) Else:node = node (element)
                Count = 0 Previous = Self._head while Count < (position-1): Count + 1 previous = Previous.next Node.next = Previous.next previous.next = node def remove ( Self, Element): cursor = self._head previous = None while cursor!= none:if CURSOR.E
                    Lement = = Element:if not previous:self._head = Cur.next else:
                Previous.next = Cursor.next Break else:previous = cursor
            cursor = Cursor.next def search (self, Element): cursor = self._head while cursor None: If Cursor.element = = Element:return True cursor = cursor.next return False if __name__ = = "__main_ _ ": linklist = Singlelinklist () linklist.add (1) linklist.add (2) linklist.append (3) Linklist.insert (2, 4) # Print ("Length:", Linklist.length ()) linklist.traversal () # Print (Linklist.search (3)) # Print (linklist. Search (5)) Linklist.remove (1) # Print ("Length:", Linklist.length ()) # linklist.traversal ()

2. Two-way linked list

Added a pointer to one of the preceding elements. Each node has two links: one point to the previous node, when this node is the first node, the front pointer points to a null value, and the other points to the next node, when this node is the last node, the front pointer points to the previous value, and the pointer points to a null value.

Class Node (object): Def __init__ (self, item): Self.item = Item Self.next = None Self.previou

        s = None class Doublelinklist (object): Def __init__ (self): Self._head = None def is_empty (self): return Self._head = = None def length (self): cursor = Self._head count = 0 while cursor! = None:count + = 1 cursor = Cursor.next return count def travel (self): curso
        R = self._head while cursor!= none:print (cursor.item, end= "") cursor = Cursor.next Print ("") def Add (self, item): node = node (item) if Self.is_empty (): Self._head = n 

    Ode Else:node.next = Self._head self._head.previous = node Self._head = node
        def append (self, item): node = node (item) if Self.is_empty (): Self._head = node Else:cursor = Self._head while cursor.next!= none:cursor = cursor.next cursor.next = node 
            node.previous = cursor Def search (self, item): cursor = self._head while cursor None:

    if Cursor.item = = Item:return True cursor = cursor.next return False 
            def insert (self, POS, item): If POS <= 0:self.add (item) elif pos > (Self.length ()-1): Self.append (item) Else:node = node (item) cursor = Self._head cou NT = 0 While Count < (pos-1): Count = 1 cursor = Cursor.next n ode.previous = Cursor Node.next = Cursor.next cursor.next.previous = node Cursor.next = Node Def remove (self, item): If Self.is_empty (): Return else:cursor = sel F._head if CUrsor.item = = Item:if Cursor.next = = None:self._head = None Else:
            cursor.next.previous = None Self._head = cursor.next return
                    While cursor!= none:if Cursor.item = = Item:cursor.previous.next = Cursor.next

Cursor.next.previous = cursor.previous break cursor = Cursor.next
    if __name__ = = "__main__": LL = Doublelinklist () ll.add (1) ll.add (2) ll.append (3) Ll.insert (2, 4) Ll.insert (4, 5) Ll.insert (0, 6) print ("Length:", Ll.length ()) Ll.travel () Print (Ll.search (3)) print (LL.
 Search (4)) Ll.remove (1) print ("Length:", Ll.length ()) Ll.travel ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.