📜  C中循环链表上所有操作的程序

📅  最后修改于: 2021-09-06 11:25:45             🧑  作者: Mango

在循环链表中,每个元素都有一个到序列中下一个元素的链接,最后一个元素有一个到第一个元素的链接。循环链表与单向链表类似,只是最后一个节点指向第一个节点。下面是说明相同的图像:

循环链表的一些常见操作实现如下:

开头插入:插入一个新节点作为第一个节点。 last 的下一个指针将指向这个节点,这个新节点将指向前一个第一个节点。

C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in the list
struct node* last = NULL;
  
// Function to insert a node in the
// starting of the list
void insertAtFront()
{
    // Stores the number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be "
           "inserted: \n");
    scanf("%d", &data);
  
    // If the new node is the only
    // node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else last node contains the
    // reference of the new node and
    // new node contains the reference
    // of the previous first node
    else {
        temp->info = data;
        temp->next = last->next;
  
        // last node now has reference
        // of the new node temp
        last->next = temp;
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
  
        // While first node is not
        // reached again, print,
        // since the list is circular
        do {
            printf("\nData = %d", temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    insertAtFront();
    insertAtFront();
    insertAtFront();
  
    // Print list
    viewList();
  
    return 0;
}


C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in the list
struct node* last = NULL;
  
// Function to add a new node at the
// end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be "
           "inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    addatlast();
    addatlast();
    addatlast();
  
    // Print list
    viewList();
  
    return 0;
}


C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to insert after any
// specified element
void insertafter()
{
    // Stores data and element after
    // which new node is to be inserted
    int data, value;
  
    // Initialize a new node
    struct node *temp, *n;
  
    // Input data
    printf("\nEnter number after which"
           " you want to enter number: \n");
    scanf("%d", &value);
    temp = last->next;
  
    do {
  
        // Element after which node is
        // to be inserted is found
        if (temp->info == value) {
            n = (struct node*)malloc(sizeof(struct node));
  
            // Input Data
            printf("\nEnter data to be"
                   " inserted : \n");
            scanf("%d", &data);
            n->info = data;
            n->next = temp->next;
            temp->next = n;
  
            // If temp is the last node
            // so now n will become the
            // last node
            if (temp == last)
                last = n;
            break;
        }
        else
            temp = temp->next;
    } while (temp != last->next);
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    insertafter();
  
    // Print list
    viewList();
  
    return 0;
}


C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be"
           " inserted: \n");
    scanf("%d", &data);
  
    // If the new node is the only
    // node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to delete the first
// element of the list
void deletefirst()
{
    struct node* temp;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    // Else last node now contains
    // reference of the second node
    // in the list because the
    // list is circular
    else {
        temp = last->next;
        last->next = temp->next;
        free(temp);
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deletefirst();
  
    // Print list
    viewList();
  
    return 0;
}


C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the only
    // node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Funtion to delete the last node
// in the list
void deletelast()
{
    struct node* temp;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    temp = last->next;
  
    // Traverse the list till
    // the second last node
    while (temp->next != last)
        temp = temp->next;
  
    // Second last node now contains
    // the reference of the first
    // node in the list
    temp->next = last->next;
    last = temp;
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deletelast();
  
    // Print the list
    viewList();
  
    return 0;
}


C
// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to delete an element
// at a specified index in the list
void deleteAtIndex()
{
    // Stores the index at which
    // the element is to be deleted
    int pos, i = 1;
    struct node *temp, *position;
    temp = last->next;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    // Else
    else {
  
        // Input Data
        printf("\nEnter index : ");
        scanf("%d", &pos);
  
        // Traverse till the node to
        // be deleted is reached
        while (i <= pos - 1) {
            temp = temp->next;
            i++;
        }
  
        // After the loop ends, temp
        // points at a node just before
        // the node to be deleted
  
        // Reassigning links
        position = temp->next;
        temp->next = position->next;
  
        free(position);
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d", temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deleteAtIndex();
  
    // Print the list
    viewList();
  
    return 0;
}


输出:

末尾插入:插入一个新节点作为最后一个节点。 last 的 next 指针将指向这个节点,而这个新节点将指向第一个节点。

C

// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in the list
struct node* last = NULL;
  
// Function to add a new node at the
// end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be "
           "inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    addatlast();
    addatlast();
    addatlast();
  
    // Print list
    viewList();
  
    return 0;
}

输出:

在特定元素之后插入下面是在链表中的指定节点之后插入节点的程序。

C

// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to insert after any
// specified element
void insertafter()
{
    // Stores data and element after
    // which new node is to be inserted
    int data, value;
  
    // Initialize a new node
    struct node *temp, *n;
  
    // Input data
    printf("\nEnter number after which"
           " you want to enter number: \n");
    scanf("%d", &value);
    temp = last->next;
  
    do {
  
        // Element after which node is
        // to be inserted is found
        if (temp->info == value) {
            n = (struct node*)malloc(sizeof(struct node));
  
            // Input Data
            printf("\nEnter data to be"
                   " inserted : \n");
            scanf("%d", &data);
            n->info = data;
            n->next = temp->next;
            temp->next = n;
  
            // If temp is the last node
            // so now n will become the
            // last node
            if (temp == last)
                last = n;
            break;
        }
        else
            temp = temp->next;
    } while (temp != last->next);
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    insertafter();
  
    // Print list
    viewList();
  
    return 0;
}

输出:

删除第一个元素删除链表的第一个节点。为此,last 的下一个指针将指向链表的第二个节点。以下是相同的程序:

C

// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be"
           " inserted: \n");
    scanf("%d", &data);
  
    // If the new node is the only
    // node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be the
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to delete the first
// element of the list
void deletefirst()
{
    struct node* temp;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    // Else last node now contains
    // reference of the second node
    // in the list because the
    // list is circular
    else {
        temp = last->next;
        last->next = temp->next;
        free(temp);
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deletefirst();
  
    // Print list
    viewList();
  
    return 0;
}

输出:

删除最后一个元素删除链表的最后一个节点。为此,倒数第二个节点将指向列表的第一个节点。以下是相同的程序:

C

// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the only
    // node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Funtion to delete the last node
// in the list
void deletelast()
{
    struct node* temp;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    temp = last->next;
  
    // Traverse the list till
    // the second last node
    while (temp->next != last)
        temp = temp->next;
  
    // Second last node now contains
    // the reference of the first
    // node in the list
    temp->next = last->next;
    last = temp;
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d",
                   temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deletelast();
  
    // Print the list
    viewList();
  
    return 0;
}

输出:

在给定位置删除:从链表中的指定位置删除元素。以下是相同的程序:

C

// C program for the above operation
#include 
#include 
  
// Structure of a linked list node
struct node {
    int info;
    struct node* next;
};
  
// Pointer to last node in list
struct node* last = NULL;
  
// Function to add a new node
// at the end of the list
void addatlast()
{
    // Stores number to be inserted
    int data;
  
    // Initialize a new node
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
  
    // Input data
    printf("\nEnter data to be inserted : \n");
    scanf("%d", &data);
  
    // If the new node is the
    // only node in the list
    if (last == NULL) {
        temp->info = data;
        temp->next = temp;
        last = temp;
    }
  
    // Else the new node will be
    // last node and will contain
    // the reference of head node
    else {
        temp->info = data;
        temp->next = last->next;
        last->next = temp;
        last = temp;
    }
}
  
// Function to delete an element
// at a specified index in the list
void deleteAtIndex()
{
    // Stores the index at which
    // the element is to be deleted
    int pos, i = 1;
    struct node *temp, *position;
    temp = last->next;
  
    // If list is empty
    if (last == NULL)
        printf("\nList is empty.\n");
  
    // Else
    else {
  
        // Input Data
        printf("\nEnter index : ");
        scanf("%d", &pos);
  
        // Traverse till the node to
        // be deleted is reached
        while (i <= pos - 1) {
            temp = temp->next;
            i++;
        }
  
        // After the loop ends, temp
        // points at a node just before
        // the node to be deleted
  
        // Reassigning links
        position = temp->next;
        temp->next = position->next;
  
        free(position);
    }
}
  
// Function to print the list
void viewList()
{
    // If list is empty
    if (last == NULL)
        printf("\nList is empty\n");
  
    // Else print the list
    else {
        struct node* temp;
        temp = last->next;
        do {
            printf("\nData = %d", temp->info);
            temp = temp->next;
        } while (temp != last->next);
    }
}
  
// Driver Code
int main()
{
    // Initialize the list
    addatlast();
    addatlast();
    addatlast();
  
    // Function Call
    deleteAtIndex();
  
    // Print the list
    viewList();
  
    return 0;
}

输出:

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live