📜  菜单驱动的程序,用于C中的双链表上的所有操作

📅  最后修改于: 2021-05-28 03:36:24             🧑  作者: Mango

链表是一种线性数据结构,由两部分组成:一个是数据部分,另一个是地址部分。中的双链表包含三个部分:一个是数据部分,另外两个是列表中下一个和上一个节点的地址。在本文中,将在一个菜单驱动程序中讨论双链表的所有常见操作。

要执行的操作

  • traverse():要查看链表的内容,有必要遍历给定的双链表。给定的traverse()函数遍历并打印双向链表的内容。
  • insertAtFront():此函数只是在双向链表的开头/开头插入一个元素。
  • insertAtEnd():此函数在双向链表的末尾插入一个元素。
  • insertAtPosition():此函数在双向链表中的指定位置插入元素。
  • deleteFirst():此函数仅从双向链表的开头/开头删除元素。
  • deleteEnd():此函数仅从双向链表的末尾删除元素。
  • deletePosition():此函数从双向链表中的指定位置删除元素。

下面是上述操作的实现:

C
// C program for the all operations in
// the Doubly Linked List
#include 
#include 
  
// Linked List Node
struct node {
    int info;
    struct node *prev, *next;
};
struct node* start = NULL;
  
// Function to traverse the linked list
void traverse()
{
    // List is empty
    if (start == NULL)
        printf("\nList is empty\n");
    struct node* temp;
  
    // Else print the DLL
    else
    {
        temp = start;
        while (temp != NULL) {
            printf("Data = %d\n",
 temp->info);
            temp = temp->next;
        }
    }
}
  
// Function to insert at the front
// of the linked list
void insertAtFront()
{
    int data;
    struct node* temp;
    temp = (struct node*)malloc(
sizeof(struct node));
    printf("\nEnter number to be inserted: ");
    scanf("%d", &data);
    temp->info = data;
    temp->prev = NULL;
  
    // Pointer of temp will be
    // assigned to start
    temp->next = start;
    start = temp;
}
  
// Function to insert at the end of
// the linked list
void insertAtEnd()
{
    int data;
    struct node *temp, *trav;
    temp = (struct node*)malloc(
sizeof(struct node));
    temp->prev = NULL;
    temp->next = NULL;
    printf("\nEnter number to be inserted: ");
    scanf("%d", &data);
    temp->info = data;
    temp->next = NULL;
    trav = start;
  
    // If start is NULL
    if (start == NULL) {
  
        start = temp;
    }
  
    // Changes Links
    else {
        while (trav->next != NULL)
            trav = trav->next;
        temp->prev = trav;
        trav->next = temp;
    }
}
  
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
    int data, pos, i = 1;
    struct node *temp, *newnode;
    newnode = malloc(sizeof(struct node));
    newnode->next = NULL;
    newnode->prev = NULL;
  
    // Enter the position and data
    printf("\nEnter position : ");
    scanf("%d", &pos);
    printf("\nEnter number to be inserted: ");
    scanf("%d", &data);
    newnode->info = data;
    temp = start;
  
    // If start==NULL,
    if (start == NULL) {
        start = newnode;
        newnode->prev = NULL;
        newnode->next = NULL;
    }
  
    // If position==1,
    else if (pos == 1) {
        newnode->next = start;
        newnode->next->prev = newnode;
        newnode->prev = NULL;
        start = newnode;
    }
  
    // Change links
    else {
        while (i < pos - 1) {
            temp = temp->next;
            i++;
        }
        newnode->next = temp->next;
        newnode->prev = temp;
        temp->next = newnode;
        temp->next->prev = newnode;
    }
}
  
// Function to delete from the front
// of the linked list
void deleteFirst()
{
    struct node* temp;
    if (start == NULL)
        printf("\nList is empty\n");
    else {
        temp = start;
        start = start->next;
        if (start != NULL)
            start->prev = NULL;
        free(temp);
    }
}
  
// Function to delete from the end
// of the linked list
void deleteEnd()
{
    struct node* temp;
    if (start == NULL)
        printf("\nList is empty\n");
    temp = start;
    while (temp->next != NULL)
        temp = temp->next;
    if (start->next == NULL)
        start = NULL;
    else {
        temp->prev->next = NULL;
        free(temp);
    }
}
  
// Function to delete from any specified
// position from the linked list
void deletePosition()
{
    int pos, i = 1;
    struct node *temp, *position;
    temp = start;
  
    // If DLL is empty
    if (start == NULL)
        printf("\nList is empty\n");
  
    // Otherwise
    else {
        // Position to be deleted
        printf("\nEnter position : ");
        scanf("%d", &pos);
  
        // If the position is the first node
        if (pos == 1) {
            position = start;
            start = start->next;
            if (start != NULL) {
                start->prev = NULL;
            }
            free(position);
            return;
        }
  
        // Traverse till position
        while (i < pos - 1) {
            temp = temp->next;
            i++;
        }
        // Change Links
        position = temp->next;
        if (position->next != NULL)
            position->next->prev = temp;
        temp->next = position->next;
  
        // Free memory
        free(position);
    }
}
  
// Driver Code
int main()
{
    int choice;
    while (1) {
  
        printf("\n\t1  To see list\n");
        printf("\t2  For insertion at"
               " starting\n");
        printf("\t3  For insertion at"
               " end\n");
        printf("\t4  For insertion at "
               "any position\n");
        printf("\t5  For deletion of "
               "first element\n");
        printf("\t6  For deletion of "
               "last element\n");
        printf("\t7  For deletion of "
               "element at any position\n");
        printf("\t8 To exit\n");
        printf("\nEnter Choice :\n");
        scanf("%d", &choice);
  
        switch (choice) {
        case 1:
            traverse();
            break;
        case 2:
            insertAtFront();
            break;
        case 3:
            insertAtEnd();
            break;
        case 4:
            insertAtPosition();
            break;
        case 5:
            deleteFirst();
            break;
        case 6:
            deleteEnd();
            break;
        case 7:
            deletePosition();
            break;
  
        case 8:
            exit(1);
            break;
        default:
            printf("Incorrect Choice\n");
        }
    }
    return 0;
}


输出:

菜单:

在开始处插入:

最后插入:

在特定位置插入:

打印链接列表:

删除选项5和6的第一个和最后一个元素:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。