📅  最后修改于: 2020-09-28 02:35:46             🧑  作者: Mango

###### 在本教程中，您将了解链表数据结构及其在Python，Java，C和C++中的实现。

• 数据项
• 另一个节点的地址

``````struct node
{
int data;
struct node *next;
};``````

``````/* Initialize nodes */
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;

/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;

• 创建一个新的结构节点并为其分配内存。
• 将其数据值加为4
• 将其下一个指针指向包含2作为数据值的struct节点
• 将下一个指针“ 1″更改为我们刚刚创建的节点。

## Python，Java，C和C++示例中的链表实现

Python

C
C +
``````# Linked list implementation in Python

class Node:
# Creating a node
def __init__(self, item):
self.item = item
self.next = None

def __init__(self):

if __name__ == '__main__':

# Assign item values
second = Node(2)
third = Node(3)

# Connect nodes
second.next = third

# Print the linked list item
``````
``````// Linked list implementation in Java

// Creating a node

static class Node {
int value;
Node next;

Node(int d) {
value = d;
next = null;
}
}

public static void main(String[] args) {

// Assign value values
Node second = new Node(2);
Node third = new Node(3);

// Connect nodess
second.next = third;

// printing node-value
}
}
}``````
``````// Linked list implementation in C

#include
#include

// Creating a node
struct node {
int value;
struct node *next;
};

// print the linked list value
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}

int main() {
// Initialize nodes
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

// Assign value values
one->value = 1;
two->value = 2;
three->value = 3;

// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;

// printing node-value
}``````
``````// Linked list implementation in C++

#include
using namespace std;

// Creating a node
class Node {
public:
int value;
Node* next;
};

int main() {
Node* one = NULL;
Node* two = NULL;
Node* three = NULL;

// allocate 3 nodes in the heap
one = new Node();
two = new Node();
three = new Node();

// Assign value values
one->value = 1;
two->value = 2;
three->value = 3;

// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;

// print the linked list value
}
}``````

## 链表复杂度

Worst case Average Case
Search O(n) O(n)
Insert O(1) O(1)
Deletion O(1) O(1)

• 动态内存分配
• 在堆栈和队列中实现
• 撤消软件功能
• 哈希表，图形