- 数据结构与算法
- DSA - 首页
- DSA - 概述
- DSA - 环境搭建
- DSA - 算法基础
- DSA - 渐进分析
- 数据结构
- DSA - 数据结构基础
- DSA - 数据结构和类型
- DSA - 数组数据结构
- 链表
- DSA - 链表数据结构
- DSA - 双向链表数据结构
- DSA - 循环链表数据结构
- 栈与队列
- DSA - 栈数据结构
- DSA - 表达式解析
- DSA - 队列数据结构
- 搜索算法
- DSA - 搜索算法
- DSA - 线性搜索算法
- DSA - 二分搜索算法
- DSA - 插值搜索
- DSA - 跳跃搜索算法
- DSA - 指数搜索
- DSA - 斐波那契搜索
- DSA - 子列表搜索
- DSA - 哈希表
- 排序算法
- DSA - 排序算法
- DSA - 冒泡排序算法
- DSA - 插入排序算法
- DSA - 选择排序算法
- DSA - 归并排序算法
- DSA - 希尔排序算法
- DSA - 堆排序
- DSA - 桶排序算法
- DSA - 计数排序算法
- DSA - 基数排序算法
- DSA - 快速排序算法
- 图数据结构
- DSA - 图数据结构
- DSA - 深度优先遍历
- DSA - 广度优先遍历
- DSA - 生成树
- 树数据结构
- DSA - 树数据结构
- DSA - 树的遍历
- DSA - 二叉搜索树
- DSA - AVL树
- DSA - 红黑树
- DSA - B树
- DSA - B+树
- DSA - 伸展树
- DSA - 字典树
- DSA - 堆数据结构
- 递归
- DSA - 递归算法
- DSA - 使用递归实现汉诺塔
- DSA - 使用递归实现斐波那契数列
- 分治法
- DSA - 分治法
- DSA - 最大最小问题
- DSA - Strassen矩阵乘法
- DSA - Karatsuba算法
- 贪心算法
- DSA - 贪心算法
- DSA - 旅行商问题(贪心法)
- DSA - Prim最小生成树
- DSA - Kruskal最小生成树
- DSA - Dijkstra最短路径算法
- DSA - 地图着色算法
- DSA - 分数背包问题
- DSA - 带截止时间的作业排序
- DSA - 最优合并模式算法
- 动态规划
- DSA - 动态规划
- DSA - 矩阵链乘法
- DSA - Floyd-Warshall算法
- DSA - 0-1背包问题
- DSA - 最长公共子序列算法
- DSA - 旅行商问题(动态规划法)
- 近似算法
- DSA - 近似算法
- DSA - 顶点覆盖算法
- DSA - 集合覆盖问题
- DSA - 旅行商问题(近似算法)
- 随机算法
- DSA - 随机算法
- DSA - 随机快速排序算法
- DSA - Karger最小割算法
- DSA - Fisher-Yates洗牌算法
- DSA 有用资源
- DSA - 问答
- DSA - 快速指南
- DSA - 有用资源
- DSA - 讨论
链表数据结构
什么是链表?
链表是一种线性数据结构,可以存储通过链接(即指针)连接在一起的“节点”集合。链表节点并非存储在连续的位置,而是使用指针链接到不同的内存位置。一个节点由数据值和指向链表中下一个节点地址的指针组成。
链表是一种动态线性数据结构,其内存大小可以在运行时根据插入或删除操作进行分配或释放,这有助于有效利用系统内存。链表可以用来实现各种数据结构,例如栈、队列、图、哈希映射等。
链表以一个头节点开始,它指向第一个节点。每个节点都包含一个存储与节点相关联的实际数据(值)的数据部分和一个指向链表中下一个节点的内存地址的 next 指针。最后一个节点称为链表的尾节点,它指向null,表示链表的结尾。
链表与数组
对于数组,大小是在创建时给定的,因此数组是固定长度的,而链表的大小是动态的,可以动态地在链表中添加任意数量的节点。数组可以容纳类似类型的数据类型,而链表可以存储不同数据类型的各种节点。
链表类型
以下是各种类型的链表。
单链表
单链表在一个节点中包含两个“桶”;一个桶存储数据,另一个桶存储链表中下一个节点的地址。遍历只能在一个方向上进行,因为同一链表的两个节点之间只有一个单向链接。
双向链表
双向链表在一个节点中包含三个“桶”;一个桶存储数据,其他桶存储列表中前一个和下一个节点的地址。由于列表中的节点从两侧相互连接,因此列表会被遍历两次。
循环链表
循环链表可以存在于单链表和双向链表中。
由于循环链表的最后一个节点和第一个节点连接在一起,因此在这个链表中的遍历将永远持续下去,直到它被中断。
链表的基本操作
链表的基本操作包括插入、删除、搜索、显示和删除给定键的元素。这些操作在单链表上执行,如下所示:
插入 - 在列表的开头添加一个元素。
删除 - 删除列表开头的一个元素。
显示 - 显示整个列表。
搜索 - 使用给定的键搜索元素。
删除 - 使用给定的键删除元素。
链表 - 插入操作
在链表中添加一个新节点是一个多步骤的操作。我们将在此处用图表学习它。首先,使用相同的结构创建一个节点,并找到它必须插入的位置。
假设我们在节点 A (LeftNode) 和 C (RightNode) 之间插入节点 B (NewNode)。然后将 B.next 指向 C:
NewNode.next -> RightNode;
它应该看起来像这样:
现在,左侧的下一个节点应该指向新节点。
LeftNode.next -> NewNode;
这将把新节点放在这两个节点的中间。新的列表应该如下所示:
链表中的插入可以以三种不同的方式进行。解释如下:
头部插入
在此操作中,我们在列表的开头添加一个元素。
算法1. START 2. Create a node to store the data 3. Check if the list is empty 4. If the list is empty, add the data to the node and assign the head pointer to it. 5. If the list is not empty, add the data to a node and link to the current head. Assign the head to the newly added node. 6. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); printf("Linked List: "); // print list printList(); }输出
Linked List: [ 50 44 30 22 12 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); }输出
Linked List: [ 50 44 30 22 12 ]
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); System.out.print("Linked List: "); // print list printList(); } }
输出
Linked List: [50 44 30 22 12 ]
class Node: def __init__(self, data=None): self.data = data self.next = None class SLL: def __init__(self): self.head = None # Print the linked list def listprint(self): printval = self.head print("Linked List: ") while printval is not None: print (printval.data) printval = printval.next def AddAtBeginning(self,newdata): NewNode = Node(newdata) # Update the new nodes next val to existing node NewNode.next = self.head self.head = NewNode l1 = SLL() l1.head = Node("731") e2 = Node("672") e3 = Node("63") l1.head.next = e2 e2.next = e3 l1.AddAtBeginning("122") l1.listprint()
输出
Linked List: 122 731 672 63
尾部插入
在此操作中,我们在列表的结尾添加一个元素。
算法1. START 2. Create a new node and assign the data 3. Find the last node 4. Point the last node to new node 5. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertatend(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; struct node *linkedlist = head; // point it to old first node while(linkedlist->next != NULL) linkedlist = linkedlist->next; //point first to new first node linkedlist->next = lk; } void main(){ int k=0; insertatbegin(12); insertatend(22); insertatend(30); insertatend(44); insertatend(50); printf("Linked List: "); // print list printList(); }输出
Linked List: [ 12 22 30 44 50 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertatend(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; struct node *linkedlist = head; // point it to old first node while(linkedlist->next != NULL) linkedlist = linkedlist->next; //point first to new first node linkedlist->next = lk; } int main(){ insertatbegin(12); insertatend(22); insertatend(30); insertatend(44); insertatend(50); cout << "Linked List: "; // print list printList(); }输出
Linked List: [ 12 22 30 44 50 ]
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void insertatend(int data) { //create a link node lk = new node(data); node linkedlist = head; // point it to old first node while(linkedlist.next != null) linkedlist = linkedlist.next; //point first to new first node linkedlist.next = lk; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatend(22); insertatend(30); insertatend(44); insertatend(50); System.out.print("Linked List: "); // print list printList(); } }输出
Linked List: [ 12 22 30 44 50 ]
class Node: def __init__(self, data=None): self.data = data self.next = None class LL: def __init__(self): self.head = None def listprint(self): val = self.head print("Linked List:") while val is not None: print(val.data) val = val.next l1 = LL() l1.head = Node("23") l2 = Node("12") l3 = Node("7") l4 = Node("14") l5 = Node("61") # Linking the first Node to second node l1.head.next = l2 # Linking the second Node to third node l2.next = l3 l3.next = l4 l4.next = l5 l1.listprint()
输出
Linked List: 23 12 7 14 61
指定位置插入
在此操作中,我们在列表中的任何位置添加一个元素。
算法1. START 2. Create a new node and assign data to it 3. Iterate until the node at position is found 4. Point first to new first node 5. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertafternode(head->next, 30); printf("Linked List: "); // print list printList(); }输出
Linked List: [ 22 12 30 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertafternode(head->next,44); insertafternode(head->next->next, 50); cout << "Linked List: "; // print list printList(); }输出
Linked List: [ 30 22 44 50 12 ]
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void insertafternode(node list, int data) { node lk = new node(data); lk.next = list.next; list.next = lk; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertafternode(head.next, 50); insertafternode(head.next.next, 33); System.out.println("Linked List: "); // print list printList(); } }输出
Linked List: [44 30 50 33 22 12 ]
class Node: def __init__(self, data=None): self.data = data self.next = None class SLL: def __init__(self): self.head = None # Print the linked list def listprint(self): printval = self.head print("Linked List: ") while printval is not None: print (printval.data) printval = printval.next # Function to add node def InsertAtPos(self,nodeatpos,newdata): if nodeatpos is None: print("The mentioned node is absent") return NewNode = Node(newdata) NewNode.next = nodeatpos.next nodeatpos.next = NewNode l1 = SLL() l1.head = Node("731") e2 = Node("672") e3 = Node("63") l1.head.next = e2 e2.next = e3 l1.InsertAtPos(l1.head.next, "122") l1.listprint()
输出
Linked List: 731 672 122 63
链表 - 删除操作
删除也是一个多步骤的过程。我们将用图解来学习。首先,使用搜索算法找到要删除的目标节点。
目标节点的左(前一个)节点现在应该指向目标节点的下一个节点:
LeftNode.next -> TargetNode.next;
这将删除指向目标节点的链接。现在,使用以下代码,我们将删除目标节点指向的内容。
TargetNode.next -> NULL;
我们需要使用已删除的节点。我们可以将其保存在内存中,否则我们可以简单地释放内存并完全清除目标节点。
如果在列表的开头插入节点,则应采取类似的步骤。在结尾插入时,列表的倒数第二个节点应指向新节点,新节点将指向 NULL。
链表中的删除也以三种不同的方式执行。如下所示:
头部删除
在此链表的删除操作中,我们从列表的开头删除一个元素。为此,我们将头指针指向第二个节点。
算法1. START 2. Assign the head pointer to the next node in the list 3. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatbegin(){ head = head->next; } int main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deleteatbegin(); printf("\nLinked List after deletion: "); // print list printList(); }输出
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 40 30 22 12 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatbegin(){ head = head->next; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); deleteatbegin(); cout << "\nLinked List after deletion: "; printList(); }输出
Linked List: [ 50 44 30 22 12 ] Linked List after deletion: [ 44 30 22 12 ]
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void deleteatbegin() { head = head.next; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); insertatbegin(33); System.out.print("Linked List: "); // print list printList(); deleteatbegin(); System.out.print("\nLinked List after deletion: "); // print list printList(); } }输出
Linked List: [ 33 50 44 30 22 12 ] Linked List after deletion: [ 50 44 30 22 12 ]
#python code for deletion at beginning using linked list. from typing import Optional class Node: def __init__(self, data: int, next: Optional['Node'] = None): self.data = data self.next = next class LinkedList: def __init__(self): self.head = None #display the list def print_list(self): p = self.head print("\n[", end="") while p: print(f" {p.data} ", end="") p = p.next print("]") #Insertion at the beginning def insert_at_begin(self, data: int): lk = Node(data) #point it to old first node lk.next = self.head #point firt to new first node self.head = lk def delete_at_begin(self): self.head = self.head.next if __name__ == "__main__": linked_list = LinkedList() linked_list.insert_at_begin(12) linked_list.insert_at_begin(22) linked_list.insert_at_begin(30) linked_list.insert_at_begin(44) linked_list.insert_at_begin(50) #print list print("Linked List: ", end="") linked_list.print_list() linked_list.delete_at_begin() print("Linked List after deletion: ", end="") linked_list.print_list()输出
Linked List: [ 50 44 30 22 12 ] Linked List after deletion: [ 44 30 22 12 ]
尾部删除
在此链表的删除操作中,我们从列表的结尾删除一个元素。
算法1. START 2. Iterate until you find the second last element in the list. 3. Assign NULL to the second last element in the list. 4. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatend(){ struct node *linkedlist = head; while (linkedlist->next->next != NULL) linkedlist = linkedlist->next; linkedlist->next = NULL; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deleteatend(); printf("\nLinked List after deletion: "); // print list printList(); }输出
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 30 22 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // Displaying the list void printList(){ struct node *p = head; while(p != NULL) { cout << " " << p->data << " "; p = p->next; } } // Insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatend(){ struct node *linkedlist = head; while (linkedlist->next->next != NULL) linkedlist = linkedlist->next; linkedlist->next = NULL; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); deleteatend(); cout << "\nLinked List after deletion: "; printList(); }输出
Linked List: 50 44 30 22 12 Linked List after deletion: 50 44 30 22
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void deleteatend() { node linkedlist = head; while (linkedlist.next.next != null) linkedlist = linkedlist.next; linkedlist.next = null; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); insertatbegin(33); System.out.print("Linked List: "); // print list printList(); //deleteatbegin(); deleteatend(); System.out.print("\nLinked List after deletion: "); // print list printList(); } }输出
Linked List: [ 33 50 44 30 22 12 ] Linked List after deletion: [ 33 50 44 30 22 ]
#python code for deletion at beginning using linked list. class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None #Displaying the list def printList(self): p = self.head print("\n[", end="") while p != None: print(" " + str(p.data) + " ", end="") p = p.next print("]") #Insertion at the beginning def insertatbegin(self, data): #create a link lk = Node(data) #point it to old first node lk.next = self.head #point first to new first node self.head = lk def deleteatend(self): linkedlist = self.head while linkedlist.next.next != None: linkedlist = linkedlist.next linkedlist.next = None if __name__ == "__main__": linked_list = LinkedList() linked_list.insertatbegin(12) linked_list.insertatbegin(22) linked_list.insertatbegin(30) linked_list.insertatbegin(40) linked_list.insertatbegin(55) #print list print("Linked List: ", end="") linked_list.printList() linked_list.deleteatend() print("Linked List after deletion: ", end="") linked_list.printList()输出
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 30 22 ]
指定位置删除
在此链表的删除操作中,我们删除列表中任何位置的一个元素。
算法1. START 2. Iterate until find the current node at position in the list. 3. Assign the adjacent node of current node in the list to its previous node. 4. END示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deletenode(int key){ struct node *temp = head, *prev; if (temp != NULL && temp->data == key) { head = temp->next; return; } // Find the key to be deleted while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deletenode(30); printf("\nLinked List after deletion: "); // print list printList(); }输出
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 22 12 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deletenode(int key){ struct node *temp = head, *prev; if (temp != NULL && temp->data == key) { head = temp->next; return; } // Find the key to be deleted while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); deletenode(30); cout << "\nLinked List after deletion: "; printList(); }输出
Linked List: [ 50 44 30 22 12 ] Linked List after deletion: [ 50 44 22 12 ]
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void deletenode(int key) { node temp = head; node prev = null; if (temp != null && temp.data == key) { head = temp.next; return; } // Find the key to be deleted while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } // If the key is not present if (temp == null) return; // Remove the node prev.next = temp.next; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); insertatbegin(33); System.out.print("Linked List: "); // print list printList(); //deleteatbegin(); //deleteatend(); deletenode(12); System.out.print("\nLinked List after deletion: "); // print list printList(); } }输出
Linked List: [ 33 50 44 30 22 12 ] Linked List after deletion: [ 33 50 44 30 22 ]
#python code for deletion at given position using linked list. class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None # display the list def printList(self): p = self.head print("\n[", end="") #start from the beginning while(p != None): print(" ", p.data, " ", end="") p = p.next print("]") #insertion at the beginning def insertatbegin(self, data): #create a link lk = Node(data) # point it to old first node lk.next = self.head #point first to new first node self.head = lk def deletenode(self, key): temp = self.head if (temp != None and temp.data == key): self.head = temp.next return # Find the key to be deleted while (temp != None and temp.data != key): prev = temp temp = temp.next # If the key is not present if (temp == None): return # Remove the node prev.next = temp.next llist = LinkedList() llist.insertatbegin(12) llist.insertatbegin(22) llist.insertatbegin(30) llist.insertatbegin(40) llist.insertatbegin(55) print("Original Linked List: ", end="") # print list llist.printList() llist.deletenode(30) print("Linked List after deletion: ", end="") # print list llist.printList()输出
Original Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 22 12 ]
链表 - 反转操作
此操作是一个彻底的操作。我们需要使头节点指向最后一个节点并反转整个链表。
首先,我们遍历到列表的末尾。它应该指向 NULL。现在,我们将使其指向其前一个节点:
我们必须确保最后一个节点不是最后一个节点。因此,我们将有一些临时节点,它看起来像头节点指向最后一个节点。现在,我们将一个接一个地使所有左侧节点指向它们的前一个节点。
除了头节点指向的节点(第一个节点)之外,所有节点都应该指向它们的前驱节点,使其成为它们新的后继节点。第一个节点将指向 NULL。
我们将使用临时节点使头节点指向新的第一个节点。
算法
反转链表的逐步过程如下:
1. START 2. We use three pointers to perform the reversing: prev, next, head. 3. Point the current node to head and assign its next value to the prev node. 4. Iteratively repeat the step 3 for all the nodes in the list. 5. Assign head to the prev node.
示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void reverseList(struct node** head){ struct node *prev = NULL, *cur=*head, *tmp; while(cur!= NULL) { tmp = cur->next; cur->next = prev; prev = cur; cur = tmp; } *head = prev; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); reverseList(&head); printf("\nReversed Linked List: "); printList(); }输出
Linked List: [ 55 40 30 22 12 ] Reversed Linked List: [ 12 22 30 40 55 ]
#include <bits/stdc++.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void reverseList(struct node** head){ struct node *prev = NULL, *cur=*head, *tmp; while(cur!= NULL) { tmp = cur->next; cur->next = prev; prev = cur; cur = tmp; } *head = prev; } int main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); reverseList(&head); printf("\nReversed Linked List: "); printList(); return 0; }输出
Linked List: [ 55 40 30 22 12 ] Reversed Linked List: [ 12 22 30 40 55 ]
public class Linked_List { static Node head; static class Node { int data; Node next; Node (int value) { data = value; next = null; } } // display the list static void printList(Node node) { System.out.print("\n["); //start from the beginning while(node != null) { System.out.print(" " + node.data + " "); node = node.next; } System.out.print("]"); } static Node reverseList(Node head) { Node prev = null; Node cur = head; Node temp = null; while (cur != null) { temp = cur.next; cur.next = prev; prev = cur; cur = temp; } head = prev; return head; } public static void main(String args[]) { Linked_List list = new Linked_List(); list.head = new Node(33); list.head.next = new Node(50); list.head.next.next = new Node(44); list.head.next.next.next = new Node(22); list.head.next.next.next.next = new Node(12); System.out.print("Linked List: "); // print list list.printList(head); head = list.reverseList(head); System.out.print("\nReversed linked list "); list.printList(head); } }输出
Linked List: [ 33 50 44 22 12 ] Reversed linked list [ 12 22 44 50 33 ]
class Node: def __init__(self, data=None): self.data = data self.next = None class SLL: def __init__(self): self.head = None # Print the linked list def listprint(self): printval = self.head print("Linked List: ") while printval is not None: print (printval.data) printval = printval.next def reverse(self): prev = None curr = self.head while(curr is not None): next = curr.next curr.next = prev prev = curr curr = next self.head = prev l1 = SLL() l1.head = Node("731") e2 = Node("672") e3 = Node("63") l1.head.next = e2 e2.next = e3 l1.listprint() l1.reverse() print("After reversing: ") l1.listprint()
输出
Linked List: 731 672 63 After reversing: Linked List: 63 672 731
链表 - 搜索操作
使用关键元素在列表中搜索元素。此操作与数组搜索方式相同;将列表中的每个元素与给定的关键元素进行比较。
算法
1 START 2 If the list is not empty, iteratively check if the list contains the key 3 If the key element is not present in the list, unsuccessful search 4 END
示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } int searchlist(int key){ struct node *temp = head; while(temp != NULL) { if (temp->data == key) { return 1; } temp=temp->next; } return 0; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); int ele = 30; printf("\nElement to be searched is: %d", ele); k = searchlist(30); if (k == 1) printf("\nElement is found"); else printf("\nElement is not found in the list"); }
输出
Linked List: [ 55 40 30 22 12 ] Element to be searched is: 30 Element is found
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } int searchlist(int key){ struct node *temp = head; while(temp != NULL) { if (temp->data == key) { return 1; } temp=temp->next; } return 0; } int main(){ int k = 0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); int ele = 16; cout<<"\nElement to be searched is: "<<ele; k = searchlist(ele); if (k == 1) cout << "\nElement is found"; else cout << "\nElement is not found in the list"; }
输出
Linked List: [ 50 44 30 22 12 ] Element to be searched is: 16 Element is not found in the list
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static int searchlist(int key) { node temp = head; while(temp != null) { if (temp.data == key) { return 1; } temp=temp.next; } return 0; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); insertatbegin(33); System.out.print("Linked List: "); // print list printList(); int ele = 44; System.out.print("\nElement to be searched is: " + ele); k = searchlist(ele); if (k == 1) System.out.println("\nElement is found"); else System.out.println("\nElement is not found in the list"); } }
输出
Linked List: [ 33 50 44 30 22 12 ] Element to be searched is: 44 Element is found
class Node: def __init__(self, data=None): self.data = data self.next = None class SLL: def listprint(self): printval = self.head print("Linked List: ") while printval is not None: print (printval.data) printval = printval.next def __init__(self): self.head = None def search(self, x): count = 0 # Initialize current to head current = self.head # loop till current not equal to None while current != None: if current.data == x: print("Element is found") count = count + 1 current = current.next if count == 0: print("Element is not found in the list") l1 = SLL() l1.head = Node("731") e2 = Node("672") e3 = Node("63") l1.head.next = e2 e2.next = e3 l1.listprint() ele = "63" print("Element to be searched is: ", ele); l1.search(ele)
输出
Linked List: 731 672 63 Element to be searched is: 63 Element is found
链表 - 遍历操作
遍历操作按顺序遍历列表中的所有元素,并按该顺序显示这些元素。
算法
1. START 2. While the list is not empty and did not reach the end of the list, print the data in each node 3. END
示例
以下是各种编程语言中此操作的实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); printf("Linked List: "); // print list printList(); }
输出
Linked List: [ 30 22 12 ]
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // Displaying the list void printList(){ struct node *p = head; while(p != NULL) { cout << " " << p->data << " "; p = p->next; } } // Insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); cout << "Linked List: "; // print list printList(); }
输出
Linked List: 50 44 30 22 12
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); insertatbegin(33); System.out.print("Linked List: "); // print list printList(); } }
输出
Linked List: [ 33 50 44 30 22 12 ]
class Node: def __init__(self, data=None): self.data = data self.next = None class SLL: def __init__(self): self.head = None # Print the linked list def listprint(self): printval = self.head print("Linked List: ") while printval is not None: print (printval.data) printval = printval.next l1 = SLL() l1.head = Node("731") e2 = Node("672") e3 = Node("63") l1.head.next = e2 e2.next = e3 l1.listprint()
输出
Linked List: 731 672 63
链表 -完整实现
以下是各种编程语言中链表的完整实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertatend(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; struct node *linkedlist = head; // point it to old first node while(linkedlist->next != NULL) linkedlist = linkedlist->next; //point first to new first node linkedlist->next = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } void deleteatbegin(){ head = head->next; } void deleteatend(){ struct node *linkedlist = head; while (linkedlist->next->next != NULL) linkedlist = linkedlist->next; linkedlist->next = NULL; } void deletenode(int key){ struct node *temp = head, *prev; if (temp != NULL && temp->data == key) { head = temp->next; return; } // Find the key to be deleted while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; } int searchlist(int key){ struct node *temp = head; while(temp != NULL) { if (temp->data == key) { return 1; } temp=temp->next; } return 0; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatend(30); insertatend(44); insertatbegin(50); insertafternode(head->next->next, 33); printf("Linked List: "); // print list printList(); deleteatbegin(); deleteatend(); deletenode(12); printf("\nLinked List after deletion: "); // print list printList(); insertatbegin(4); insertatbegin(16); printf("\nUpdated Linked List: "); printList(); k = searchlist(16); if (k == 1) printf("\nElement is found"); else printf("\nElement is not present in the list"); }
输出
Linked List: [ 50 22 12 33 30 44 ] Linked List after deletion: [ 22 33 30 ] Updated Linked List: [ 16 4 22 33 30 ] Element is found
#include <bits/stdc++.h> #include <string> using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertatend(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; struct node *linkedlist = head; // point it to old first node while(linkedlist->next != NULL) linkedlist = linkedlist->next; //point first to new first node linkedlist->next = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } void deleteatbegin(){ head = head->next; } void deleteatend(){ struct node *linkedlist = head; while (linkedlist->next->next != NULL) linkedlist = linkedlist->next; linkedlist->next = NULL; } void deletenode(int key){ struct node *temp = head, *prev; if (temp != NULL && temp->data == key) { head = temp->next; return; } // Find the key to be deleted while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; } int searchlist(int key){ struct node *temp = head; while(temp != NULL) { if (temp->data == key) { temp=temp->next; return 1; } else return 0; } return key; } int main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatend(30); insertatend(44); insertatbegin(50); insertafternode(head->next->next, 33); cout << "Linked List: "; // print list printList(); deleteatbegin(); deleteatend(); deletenode(12); cout << "\nLinked List after deletion: "; // print list printList(); insertatbegin(4); insertatbegin(16); cout << "\nUpdated Linked List: "; printList(); k = searchlist(16); if (k == 1) cout << "\nElement is found"; else cout << "\nElement is not present in the list"; return 0; }
输出
Linked List: [ 50 22 12 33 30 44 ] Linked List after deletion: [ 22 33 30 ] Updated Linked List: [ 16 4 22 33 30 ] Element is found
public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p.next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data);; // point it to old first node lk.next = head; //point first to new first node head = lk; } static void insertatend(int data) { //create a link node lk = new node(data); node linkedlist = head; // point it to old first node while(linkedlist.next != null) linkedlist = linkedlist.next; //point first to new first node linkedlist.next = lk; } static void insertafternode(node list, int data) { node lk = new node(data); lk.next = list.next; list.next = lk; } static void deleteatbegin() { head = head.next; } static void deleteatend() { node linkedlist = head; while (linkedlist.next.next != null) linkedlist = linkedlist.next; linkedlist.next = null; } static void deletenode(int key) { node temp = head; node prev = null; if (temp != null && temp.data == key) { head = temp.next; return; } // Find the key to be deleted while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } // If the key is not present if (temp == null) return; // Remove the node prev.next = temp.next; } static int searchlist(int key) { node temp = head; while(temp != null) { if (temp.data == key) { temp=temp.next; return 1; } } return 0; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatend(30); insertatend(44); insertatbegin(50); insertafternode(head.next.next, 33); System.out.print("Linked List: "); // print list printList(); deleteatbegin(); deleteatend(); deletenode(12); System.out.print("\nLinked List after deletion: "); // print list printList(); insertatbegin(4); insertatbegin(16); System.out.print("\nUpdated Linked List: "); printList(); k = searchlist(16); if (k == 1) System.out.print("\nElement is found"); else System.out.print("\nElement is not present in the list"); } }
输出
Linked List: [ 50 22 12 33 30 44 ] Linked List after deletion: [ 22 33 30 ] Updated Linked List: [ 16 4 22 33 30 ] Element is found
class LLNode: def __init__(self, data=None): self.data = data self.next = None class LL: def __init__(self): self.head = None def listprint(self): printval = self.head while printval is not None: print(printval.data) printval = printval.next def AddAtBeginning(self,newdata): NewNode = LLNode(newdata) # Update the new nodes next val to existing node NewNode.next = self.head self.head = NewNode # Function to add node at a position def InsertAtPos(self,nodeatpos,newdata): if nodeatpos is None: print("The mentioned node is absent") return NewNode = LLNode(newdata) NewNode.next = nodeatpos.next nodeatpos.next = NewNode def reverse(self): prev = None curr = self.head while(curr is not None): next = curr.next curr.next = prev prev = curr curr = next self.head = prev def search(self, x): count = 0 # Initialize current to head current = self.head # loop till current not equal to None while current != None: if current.data == x: print("Element is found") count = count + 1 current = current.next if count == 0: print("Element is not found in the list") l1 = LL() l1.head = LLNode("23") l2 = LLNode("12") l3 = LLNode("7") l4 = LLNode("14") l5 = LLNode("61") # Linking the first Node to second node l1.head.next = l2 # Linking the second Node to third node l2.next = l3 l3.next = l4 l4.next = l5 print("Original Linked List: ") l1.listprint() l1.AddAtBeginning("45") l1.InsertAtPos(l1.head.next.next, "4") print("Updated Linked List:") l1.listprint() l1.reverse() print("Reversed Linked List:") l1.listprint() l1.search("7")
输出
Original Linked List: 23 12 7 14 61 Updated Linked List: 45 23 12 4 7 14 61 Reversed Linked List: 61 14 7 4 12 23 45 Element is found