链表

像数组一样,链表是一个线性数据结构。与数组不同,链表元素不存储在连续的位置; 元素使用指针链接。

为什么要用链表?
数组可以用来存储相似类型的线性数据,但是数组有以下限制。
1)数组的大小是固定的:所以我们必须事先知道元素数量的上限。而且,通常,分配的存储器等于上限,而与用途无关。
2)在一个元素数组中插入一个新的元素是很昂贵的,因为必须为新的元素创建空间,并且要创建有空间的元素必须移位。

例如,在一个系统中,如果我们维护数组id中的ID的排序列表。

id [] = [1000,1010,1050,2000,2040]。

如果我们想要插入一个新的ID 1005,那么为了保持排序的顺序,我们必须移动所有在1000之后的元素(不包括1000)。
除非使用一些特殊的技术,否则数组的删除也是昂贵的。例如,要删除id []中的1010,必须移动1010之后的所有内容。

与阵列相比的优点
1)动态大小
2)易于插入/删除

缺点:
1)不允许随机访问。我们必须从第一个节点开始按顺序访问元素。所以我们不能用链表进行二分搜索。
2)列表中的每个元素都需要指针的额外内存空间。

在C中的表示:
链表由指向链表的第一个节点的指针表示。第一个节点叫做头。如果链表是空的,那么head的值是NULL。
列表中的每个节点至少由两部分组成:
1)数据
2)指向下一个节点的指针
在C中,我们可以使用结构体来表示一个节点。下面是一个带有整型数据的链表节点的例子。
在Java中,LinkedList可以表示为一个类和一个Node作为一个单独的类。LinkedList类包含Node类型

# Node class
class Node:
  
    # Function to initialize the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize
                          # next as null
  
# Linked List class
class LinkedList:
    
    # Function to initialize the Linked
    # List object
    def __init__(self):
        self.head = None
我们用3个节点创建一个简单的链表
 
# A simple Python program to introduce a linked list
 
# Node class
class Node:
 
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize next as null
 
 
# Linked List class contains a Node object
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
 
# Code execution starts here
if __name__=='__main__':
 
    # Start with the empty list
    llist = LinkedList()
 
    llist.head  = Node(1)
    second = Node(2)
    third  = Node(3)
 
    '''
    Three nodes have been created.
    We have references to these three blocks as first,
    second and third
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  | None |     | 2  | None |     |  3 | None |
    +----+------+     +----+------+     +----+------+
    '''
 
    llist.head.next = second; # Link first node with second
 
    '''
    Now next of first Node refers to second.  So they
    both are linked.
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  |  o-------->| 2  | null |     |  3 | null |
    +----+------+     +----+------+     +----+------+
    '''
 
    second.next = third; # Link second node with the third node
 
    '''
    Now next of second Node refers to third.  So all three
    nodes are linked.
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  |  o-------->| 2  |  o-------->|  3 | null |
    +----+------+     +----+------+     +----+------+
    '''
链接列表遍历
在前面的程序中,我们创建了一个带有三个节点的简单链表。让我们遍历创建的列表并打印每个节点的数据。为了遍历,让我们写一个通用函数printList()来打印任何给定的列表。
# A simple Python program for traversal of a linked list
 
# Node class
class Node:
 
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize next as null
 
 
# Linked List class contains a Node object
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
    # This function prints contents of linked list
    # starting from head
    def printList(self):
        temp = self.head
        while (temp):
            print temp.data,
            temp = temp.next
 
 
# Code execution starts here
if __name__=='__main__':
 
    # Start with the empty list
    llist = LinkedList()
 
    llist.head  = Node(1)
    second = Node(2)
    third  = Node(3)
 
    llist.head.next = second; # Link first node with second
    second.next = third; # Link second node with the third node
 
    llist.printList()
原文地址:https://www.cnblogs.com/ylHe/p/7846046.html