Does python have built-in linkedList data structure?

后端 未结 5 1972
情深已故
情深已故 2021-02-07 05:05

Does anyone know if Python (maybe 2.7) has a built-in linkedList data structure? I know the queue is implemented using list, and there is no stack (there is LIFO qu

5条回答
  •  别跟我提以往
    2021-02-07 05:43

    Unless you actually want an explicit linked list structure for something specific, Python's built-in list has all the functionality that you get from a linked list. For example, you can use it as a stack, as follows:

    >>> x = []
    >>> x.append(1)
    >>> x.append(2)
    >>> x
    [1, 2]
    >>> x.pop()
    2
    >>> x
    [1]
    >>>
    

    Or, to insert an element after a given element:

    >>> x = [1,2,3,4,5,6,7]
    >>> x.insert(3,"a")
    >>> x
    [1, 2, 3, 'a', 4, 5, 6, 7]
    >>> 
    

    See, for example, the Python documentation on data structures.

    However, this is using the "list" abstract data type (ADT). In contrast, a "linked list" is not an ADT but one of many possible ways of implementing that ADT.

    If efficiency of prepending is an issue, Łukasz Rogalski's answer points out that collections.deque is implemented using a linked list. As Using Lists as Queues notes:

    It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).

    To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends.

    To test the efficiency impacts of using list vs. deque, I used the following program:

    import timeit, sys
    
    print ("append to list: %f" % (timeit.timeit ('x.append("x")', 'x = ["y"]')))
    print ("insert to list element 0: %f" % (timeit.timeit ('x.insert(0,"x")', 'x = ["y"]')))
    print ("append to deque: %f" % (timeit.timeit ('x.append("x")', 'import collections; x = collections.deque(["a","b","c"])')))
    print ("append left to deque: %f" % (timeit.timeit ('x.appendleft("x")', 'import collections; x = collections.deque(["a","b","c"])')))
    
    if (sys.version_info[0]+sys.version_info[1]/10) > 3.4999:
        print ("insert in deque: %f" % (timeit.timeit ('x.insert(2,"x")', 'import collections; x = collections.deque(["a","b","c"])')))
    

    ... and got the following results for Python 3.6 and Python 2.7:

    $ python3 testList.py
    append to list: 0.113031
    insert to list element 0: 191.147079
    append to deque: 0.058606
    append left to deque: 0.064640
    insert in deque: 0.160418
    
    $ python testList.py
    append to list: 0.102542
    insert to list element 0: 191.128508
    append to deque: 0.083397
    append left to deque: 0.064534
    

    Thus, list takes roughly twice as much time to append an element as does deque and deque takes less time to prepend (i.e. append to left) than to append.

提交回复
热议问题