数据结构和算法

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 施特拉森矩阵乘法 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 快速指南


循环链表数据结构

什么是循环链表?

循环链表是链表的一种变体,其中第一个元素指向最后一个元素,最后一个元素指向第一个元素。单链表和双链表都可以组成循环链表。

循环单链表

在单链表中,最后一个节点的下一个指针指向第一个节点。

循环单链表

循环双链表

在双链表中,最后一个节点的下一个指针指向第一个节点,第一个节点的上一个指针指向最后一个节点,从而形成双向循环。

循环双链表循环

根据上图,以下是需要考虑的要点。

  • 单链表和双链表的最后一个链接的 next 指向链表的第一个链接。

  • 双链表的第一个链接的 previous 指向链表的最后一个链接。

循环链表的基本操作

以下是循环链表支持的重要操作。

  • insert − 在链表开头插入一个元素。

  • delete − 从链表开头删除一个元素。

  • display −显示列表。

循环链表 - 插入操作

循环链表的插入操作仅将元素插入到列表的开头。这与通常的单链表和双链表不同,因为该列表没有特定的起点和终点。插入操作可以在开头进行,也可以在列表中的特定节点(或给定位置)之后进行。

算法

1. 开始
2. 检查列表是否为空
3. 如果列表为空,则添加节点并将头节点
指向此节点
4. 如果列表不为空,则将现有头节点链接为
新节点的下一个节点。
5. 将新节点作为新的头节点。
6. 结束

示例

以下是此操作在各种编程语言中的实现 −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}

//在第一个位置插入链接
void insertFirst(int key, int data){

   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//显示列表
void printList(){
   struct node *ptr = head;
   printf("
[ ");

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
   printf(" ]");
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");

   //打印列表
   printList();
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}
//在第一个位置插入链接
void insertFirst(int key, int data){

   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//显示列表
void printList(){
   struct node *ptr = head;
   printf("
[ ");

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
   printf(" ]");
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");
   
   //打印列表
   printList();
   return 0;
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
//循环链接列表的Java程序
import java.util.*;
class Node {
    int data;
    int key;
    Node next;
}
public class Main {
    static Node head = null;
    static Node current = null;
    static boolean isEmpty() {
        return head == null;
    }
    //在第一个位置插入链接
    static void insertFirst(int key, int data) {
        //创建链接
        Node link = new Node();
        link.key = key;
        link.data = data;
        if (isEmpty()) {
            head = link;
            head.next = head;
        } else {
            //将其指向旧的第一个节点
            link.next = head;
            //将first指向新的第一个节点
            head = link;
        }
    }
    //显示列表
    static void printList() {
        Node ptr = head;
        System.out.print("
[ ");
        //从头开始
        if (head != null) {
            while (ptr.next != ptr) {
                System.out.print("(" + ptr.key + "," + ptr.data + ") ");
                ptr = ptr.next;
            }
        }
        System.out.print(" ]");
    }
    public static void main(String[] args) {
        insertFirst(1, 10);
        insertFirst(2, 20);
        insertFirst(3, 30);
        insertFirst(4, 1);
        insertFirst(5, 40);
        insertFirst(6, 56);
        System.out.print("循环链表: ");
        //打印列表
        printList();
    }
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]
#python program for circular linked list
class Node:
    def __init__(self, key, data):
        self.key = key
        self.data = data
        self.next = None
head = None
current = None
def is_empty():
    return head is None
#在第一个位置插入链接
def insert_first(key, data):
    #创建链接
    global head
    new_node = Node(key, data)
    if is_empty():
        head = new_node
        head.next = head
    else:
        #将其指向旧的第一个节点
        new_node.next = head
        #指向新的第一个节点
        head = new_node
        #显示列表
def print_list():
    global head
    ptr = head
    print("[", end=" ")
    #start from the beginning
    if head is not None:
        while ptr.next != ptr:
            print("({}, {})".format(ptr.key, ptr.data), end=" ")
            ptr = ptr.next
        print("]")
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print("循环链表: ")
print_list()

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]

循环链表 - 删除操作

循环链表中的删除操作是从链表中移除某个节点。此类链表中的删除操作可以在链表的开头、指定位置或结尾执行。

算法

1. 开始
2. 如果链表为空,则返回程序。
3. 如果链表不为空,则使用
current 指针遍历链表,并将该指针设置为头指针,并创建
另一个指向最后一个节点的 previous 指针。
4. 假设链表只有一个节点,则通过将 head 指针设置为 NULL 来删除该节点。
5. 如果链表包含多个节点,并且要删除第一个节点,则将 head 设置为下一个节点,并将 previous 指针链接到新的 head。
6. 如果要删除的节点是最后一个节点,则将最后一个节点的前一个节点链接到头节点。
7. 如果该节点既不是第一个节点也不是最后一个节点,则通过
将其前一个节点链接到其后一个节点来删除该节点。
8. 结束

示例

以下是此操作在各种编程语言中的实现 −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}

//在第一个位置插入链接
void insertFirst(int key, int data){

   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//删除第一项
struct node * deleteFirst(){

   //保存对第一个链接的引用
   struct node *tempLink = head;
   if(head->next == head) {
      head = NULL;
      return tempLink;
   }

   //将第一个链接旁边的标记为第一个
   head = head->next;

   //返回已删除的链接
   return tempLink;
}

//显示列表
void printList(){
   struct node *ptr = head;

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");

   //打印列表
   printList();
   deleteFirst();
   printf("
删除第一项后的列表: ");
   printList();
}

输出

循环链表: (6,56) (5,40) (4,1) (3,30) (2,20) 
删除第一项后的列表: (5,40) (4,1) (3,30) (2,20) 
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}

//在第一个位置插入链接
void insertFirst(int key, int data){

   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//删除第一项
struct node * deleteFirst(){
   
   //保存对第一个链接的引用
   struct node *tempLink = head;
   if(head->next == head) {
      head = NULL;
      return tempLink;
   }

   //将第一个链接旁边的标记为第一个
   head = head->next;

   //返回已删除的链接
   return tempLink;
}

//显示列表
void printList(){
   struct node *ptr = head;

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");

   //打印列表
   printList();
   deleteFirst();
   printf("
删除第一项后的列表: ");
   printList();
   return 0;
}

输出

循环链表: (6,56) (5,40) (4,1) (3,30) (2,20) 
删除第一项后的列表: (5,40) (4,1) (3,30) (2,20) 
//循环链表的Java程序
import java.util.*;
public class Main {
    static class Node {
        int data;
        int key;
        Node next;
    }
    static Node head = null;
    static Node current = null;

    static boolean isEmpty() {
        return head == null;
    }
    //在第一个位置插入链接
    static void insertFirst(int key, int data) {
        //创建链接
        Node link = new Node();
        link.key = key;
        link.data = data;
        if (isEmpty()) {
            head = link;
            head.next = head;
        } else {
            //将其指向旧的第一个节点
            link.next = head;
            //将first指向新的第一个节点
            head = link;
        }
    }
//删除第一项
    static Node deleteFirst() {
        //保存对第一个链接的引用
        Node tempLink = head;
        if (head.next == head) {
            head = null;
            return tempLink;
        }
        //将第一个链接旁边的标记为第一个
        head = head.next;
        //返回已删除的链接
        return tempLink;
    }
    //显示列表
    static void printList() {
        Node ptr = head;
         //从头开始
        if (head != null) {
            while (ptr.next != ptr) {
                System.out.printf("(%d,%d) ", ptr.key, ptr.data);
                ptr = ptr.next;
            }
        }
    }
    public static void main(String[] args) {
        insertFirst(1, 10);
        insertFirst(2, 20);
        insertFirst(3, 30);
        insertFirst(4, 1);
        insertFirst(5, 40);
        insertFirst(6, 56);
        System.out.print("循环链表: ");
        //打印列表
        printList();
        deleteFirst();
		System.out.print("
删除第一项后的列表: ");
        printList();
  }
}

输出

循环链表: (6,56) (5,40) (4,1) (3,30) (2,20) 
删除第一项后的列表: (5,40) (4,1) (3,30) (2,20)
#python program for circular linked list
class Node:
    def __init__(self, key, data):
        self.key = key
        self.data = data
        self.next = None
head = None
current = None
def is_empty():
    return head is None
#在第一个位置插入链接
def insert_first(key, data):
    #创建链接
    global head
    new_node = Node(key, data)
    if is_empty():
        head = new_node
        head.next = head
    else:
        #将其指向旧的第一个节点
        new_node.next = head
        #指向新的第一个节点
        head = new_node
        
def print_list():
    global head
    ptr = head
    print("[", end=" ")
    #start from the beginning
    if head is not None:
        while ptr.next != ptr:
            print("({}, {})".format(ptr.key, ptr.data), end=" ")
            ptr = ptr.next
        print("]")
def delete_first():
    global head
    temp_link = head
    if head.next == head:
        head = None
        return temp_link
    head = head.next
    return temp_link  
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print("循环链表: ")
print_list()
delete_first()
print("
删除第一项后的列表: ")
print_list();

输出

循环链表: 
[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) ]

删除第一项后的列表: 
[ (5, 40) (4, 1) (3, 30) (2, 20) ]

循环链表 - 显示列表

显示列表操作会访问列表中的每个节点,并在输出中打印所有节点。

算法

1. 开始
2. 遍历列表的所有节点并打印它们
3. 结束

示例

以下是此操作在各种编程语言中的实现 −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}

//在第一个位置插入链接
void insertFirst(int key, int data){

   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//显示列表
void printList(){
   struct node *ptr = head;
   printf("
[ ");

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
   printf(" ]");
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");

   //打印列表
   printList();
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}

//在第一个位置插入链接
void insertFirst(int key, int data){
   
   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {

      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}

//显示列表
void printList(){
   struct node *ptr = head;
   printf("
[ ");

   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
   printf(" ]");
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("循环链表: ");

   //打印列表
   printList();
   return 0;
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
//Java program for circular link list
import java.util.*;
class Node {
    int data;
    int key;
    Node next;
}
public class Main {
    static Node head = null;
    static Node current = null;
    static boolean isEmpty() {
        return head == null;
    }
    //在第一个位置插入链接
    static void insertFirst(int key, int data) {
        //创建链接
        Node link = new Node();
        link.key = key;
        link.data = data;
        if (isEmpty()) {
            head = link;
            head.next = head;
        } else {
            //将其指向旧的第一个节点
            link.next = head;
            //将first指向新的第一个节点
            head = link;
        }
    }
    //显示列表
    static void printList() {
        Node ptr = head;
        System.out.print("
[ ");
        //从头开始
        if (head != null) {
            while (ptr.next != ptr) {
                System.out.print("(" + ptr.key + "," + ptr.data + ") ");
                ptr = ptr.next;
            }
        }
        System.out.print(" ]");
    }
    public static void main(String[] args) {
        insertFirst(1, 10);
        insertFirst(2, 20);
        insertFirst(3, 30);
        insertFirst(4, 1);
        insertFirst(5, 40);
        insertFirst(6, 56);
        System.out.print("循环链表: ");
        //打印列表
        printList();
    }
}

输出

循环链表: 
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]
#python program for circular linked list
class Node:
    def __init__(self, key, data):
        self.key = key
        self.data = data
        self.next = None
head = None
current = None
def is_empty():
    return head is None
#在第一个位置插入链接
def insert_first(key, data):
    #创建链接
    global head
    new_node = Node(key, data)
    if is_empty():
        head = new_node
        head.next = head
    else:
        #将其指向旧的第一个节点
        new_node.next = head
        #指向新的第一个节点
        head = new_node
        #显示列表
def print_list():
    global head
    ptr = head
    print("[", end=" ")
    #start from the beginning
    if head is not None:
        while ptr.next != ptr:
            print("({}, {})".format(ptr.key, ptr.data), end=" ")
            ptr = ptr.next
        print("]")
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print("循环链表: ")
print_list()

输出

循环链表: [ (6,56) (5,40) (4,1) (3,30) (2,20) ]

循环链表 - 完整实现

以下是各种编程语言中循环链表的完整实现 −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}
int length(){
   int length = 0;
   //如果列表为空
   if(head == NULL) {
      return 0;
   }
   current = head->next;
   while(current != head) {
      length++;
      current = current->next;
   }
   return length;
}
//在第一个位置插入链接
void insertFirst(int key, int data){
   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {
      //将其指向旧的第一个节点
      link->next = head;

      //将first指向新的第一个节点
      head = link;
   }
}
//删除第一项
struct node * deleteFirst(){

   //保存对第一个链接的引用
   struct node *tempLink = head;
   if(head->next == head) {
      head = NULL;
      return tempLink;
   }
   //将第一个链接旁边的标记为第一个
   head = head->next;

   //返回已删除的链接
   return tempLink;
}
//显示列表
void printList(){
   struct node *ptr = head;
   printf("
[ ");
   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         printf("(%d,%d) ",ptr->key,ptr->data);
         ptr = ptr->next;
      }
   }
   printf(" ]");
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("原始列表:");
   //打印列表
   printList();
   while(!isEmpty()) {
      struct node *temp = deleteFirst();
      printf("
Deleted value:");
      printf("(%d,%d) ",temp->key,temp->data);
   }
   printf("
删除所有项目后的列表:");
   printList();
}

输出

原始列表:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
Deleted value:(6,56) 
Deleted value:(5,40) 
Deleted value:(4,1) 
Deleted value:(3,30) 
Deleted value:(2,20) 
Deleted value:(1,10) 
删除所有项目后的列表:
[  ]
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
using namespace std;
struct node {
   int data;
   int key;
   struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
bool isEmpty(){
   return head == NULL;
}
int length(){
   int length = 0;
   
   //如果列表为空
   if(head == NULL) {
      return 0;
   }
   current = head->next;
   while(current != head) {
      length++;
      current = current->next;
   }
   return length;
}
//在第一个位置插入链接
void insertFirst(int key, int data){
   //创建链接
   struct node *link = (struct node*) malloc(sizeof(struct node));
   link->key = key;
   link->data = data;
   if (isEmpty()) {
      head = link;
      head->next = head;
   } else {
      //将其指向旧的第一个节点
      link->next = head;
      
      //将first指向新的第一个节点
      head = link;
   }
}
//删除第一项
struct node * deleteFirst(){   
   //保存对第一个链接的引用
   struct node *tempLink = head;
   if(head->next == head) {
      head = NULL;
      return tempLink;
   }
   //将第一个链接旁边的标记为第一个
   head = head->next;
   
   //返回已删除的链接
   return tempLink;
}
//显示列表
void printList(){
   struct node *ptr = head;
   cout << "
[ ";
   //从头开始
   if(head != NULL) {
      while(ptr->next != ptr) {
         cout << "(" << ptr->key << "," << ptr->data << ") ";
         ptr = ptr->next;
      }
   }
   cout << " ]";
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   cout << "原始列表:";
   //打印列表
   printList();
   while(!isEmpty()) {
      struct node *temp = deleteFirst();
      cout << "
 Deleted value:";
      cout << "(" << temp->key << "," << temp->data << ") ";
   }
   cout << "
 删除所有项目后的列表:";
   printList();
   return 0;
}

输出

原始列表:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
 Deleted value:(6,56) 
 Deleted value:(5,40) 
 Deleted value:(4,1) 
 Deleted value:(3,30) 
 Deleted value:(2,20) 
 Deleted value:(1,10) 
 删除所有项目后的列表:
[  ]
class Node {
    int data;
    int key;
    Node next;
    Node(int key, int data) {
        this.key = key;
        this.data = data;
        this.next = null;
    }
}
public class LinkedList {
    private Node head;
    private Node current;
    boolean isEmpty() {
        return head == null;
    }
    int length() {
        int length = 0;
        //如果列表为空
        if (head == null) {
            return 0;
        }
        current = head.next;
        while (current != head) {
            length++;
            current = current.next;
        }
        return length;
    }
    //在第一个位置插入链接
    void insertFirst(int key, int data) {
        //创建链接
        Node link = new Node(key, data);
        if (isEmpty()) {
            head = link;
            head.next = head;
        } else {
            //将其指向旧的第一个节点
            link.next = head;
            //将first指向新的第一个节点
            head = link;
        }
    }
    //删除第一项
    Node deleteFirst() {
        if (head.next == head) {
            //保存对第一个链接的引用
            Node tempLink = head;
            head = null;
            return tempLink;
        }
        Node tempLink = head;
        //将第一个链接旁边的标记为第一个
        head = head.next;
        //返回已删除的链接
        return tempLink;
    }
    //显示列表
    void printList() {
        Node ptr = head;
        System.out.print("
[ ");

        //从头开始
        if (head != null) {
            while (ptr.next != ptr) {
                System.out.print("(" + ptr.key + "," + ptr.data + ") ");
                ptr = ptr.next;
            }
        }
        System.out.print(" ]");
    }
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.insertFirst(1, 10);
        linkedList.insertFirst(2, 20);
        linkedList.insertFirst(3, 30);
        linkedList.insertFirst(4, 1);
        linkedList.insertFirst(5, 40);
        linkedList.insertFirst(6, 56);
        System.out.print("原始列表:");
        linkedList.printList();
        //打印列表
        while (!linkedList.isEmpty()) {
            Node temp = linkedList.deleteFirst();
            System.out.println("
Deleted value: (" + temp.key + "," + temp.data + ")");
        }
        System.out.print("
删除所有项目后的列表:");
        linkedList.printList();
    }
}

输出

原始列表:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
Deleted value: (6,56)

Deleted value: (5,40)

Deleted value: (4,1)

Deleted value: (3,30)

Deleted value: (2,20)

Deleted value: (1,10)

删除所有项目后的列表:
[  ]
class Node:
    def __init__(self, key, data):
        self.key = key
        self.data = data
        self.next = None
class LinkedList:
    def __init__(self):
        self.head = None
        self.current = None
    def is_empty(self):
        return self.head is None
    def length(self):
        length = 0
        # 如果列表为空
        if self.head is None:
            return 0
        self.current = self.head.next
        while self.current != self.head:
            length += 1
            self.current = self.current.next
        return length
    # 在第一个位置插入链接
    def insert_first(self, key, data):
        # 创建链接
        new_node = Node(key, data)
        if self.is_empty():
            self.head = new_node
            self.head.next = self.head
        else:
            # 将其指向旧的第一个节点
            new_node.next = self.head
            # 指向新的第一个节点
            self.head = new_node
    # 删除第一项
    def delete_first(self):
        # 保存对第一个链接的引用
        if self.head.next == self.head:
            temp_link = self.head
            self.head = None
            return temp_link
        # 将第一个链接旁边的标记为第一个
        temp_link = self.head
        self.head = self.head.next
        # 返回已删除的链接
        return temp_link
    # 显示列表
    def print_list(self):
        ptr = self.head
        print("[", end=" ")
        # start from the beginning
        if self.head is not None:
            while ptr.next != ptr:
                print("({}, {})".format(ptr.key, ptr.data), end=" ")
                ptr = ptr.next
        print("]")
# Main function
if __name__ == '__main__':
    linked_list = LinkedList()
    linked_list.insert_first(1, 10)
    linked_list.insert_first(2, 20)
    linked_list.insert_first(3, 30)
    linked_list.insert_first(4, 1)
    linked_list.insert_first(5, 40)
    linked_list.insert_first(6, 56)
    print("原始列表:", end="")
    linked_list.print_list()
    while not linked_list.is_empty():
        temp = linked_list.delete_first()
        print("
Deleted value: ({}, {})".format(temp.key, temp.data))
    # print list
    print("删除所有项目后的列表:", end="")
    linked_list.print_list()

输出

原始列表:[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) ]

Deleted value: (6, 56)

Deleted value: (5, 40)

Deleted value: (4, 1)

Deleted value: (3, 30)

Deleted value: (2, 20)Deleted value: (1, 10)
删除所有项目后的列表:[ ]