Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Use Python to Implement a Linked List with Sorting and Searching

Use Python to Implement a Linked List with Sorting and Searching

Document Object Reference Manual Python3 Examples

We will use Python to implement a simple linked list and add sorting and searching functionality to it. A linked list is a common data structure consisting of a series of nodes, each containing data and a pointer to the next node.

Example

class

Node:

   

def

init

(

self

,

data

)

:

       

self

.

data

=

data

       

self

.

next

=

None

class

LinkedList:

   

def

init

(

self

)

:

       

self

.

head

=

None

   

def

append

(

self

,

data

)

:

        new_node

=

Node

(

data

)

       

if

not

self

.

head

:

           

self

.

head

=

new_node

           

return

        last_node

=

self

.

head

       

while

last_node.

next

:

            last_node

=

last_node.

next

        last_node.

next

=

new_node

   

def

display

(

self

)

:

        current

=

self

.

head

       

while

current:

           

print

(

current.

data

,

end

=

" -> "

)

            current

=

current.

next

       

print

(

"None"

)

   

def

sort

(

self

)

:

       

if

not

self

.

head

:

           

return

        sorted_list

=

[

]

        current

=

self

.

head

       

while

current:

            sorted_list.

append

(

current.

data

)

            current

=

current.

next

        sorted_list.

sort

(

)

       

self

.

head

=

None

       

for

item

in

sorted_list:

           

self

.

append

(

item

)

   

def

search

(

self

,

key

)

:

        current

=

self

.

head

       

while

current:

           

if

current.

data

==

key:

               

return

True

            current

=

current.

next

       

return

False

Example usage

ll

=

LinkedList

(

)

ll.

append

(

3

)

ll.

append

(

1

)

ll.

append

(

4

)

ll.

append

(

2

)

print

(

"Original linked list:"

)

ll.

display

(

)

ll.

sort

(

)

print

(

"Sorted linked list:"

)

ll.

display

(

)

print

(

"Search for element 4:"

,

ll.

search

(

4

)

)

print

(

"Search for element 5:"

,

ll.

search

(

5

)

)

Code analysis:

  1. Node class: represents a node in the linked list, containing data data and a pointer to the next node next。
  2. LinkedList class: represents the linked list, containing a pointer to the head head。
  3. append method: add a new node at the end of the linked list.
  4. display method: print all elements in the linked list.
  5. sort method: sort the elements in the linked list. First extract the data in the linked list into a list, then sort the list, and finally reinsert the sorted data back into the linked list.
  6. search method: search for a specified element in the linked list; if found, return True; otherwise, returns False。

Output result:

原始链表:
3 -> 1 -> 4 -> 2 -> None
Sorted linked list:
1 -> 2 -> 3 -> 4 -> None
查找元素 4: True
查找元素 5: False

Document Object Reference Manual Python3 Examples