C 程序说明单链表的操作


2022年3月11日, Learn eTutorial
1826

为了更好地理解,我们始终建议您学习下面列出的C语言编程基础主题

在这个 C 程序中,我们必须说明单链表的操作。

什么是链表?

链表就是一组动态分配的节点,排列方式是每个节点包含一个值和一个指针。这被称为头和主体。换句话说,链表是动态数据结构的最佳示例,它使用指针来实现。

然后我们还必须了解链表在 C 语言中的表示。链表由指向链表第一个节点的指针表示。换句话说,链表是使用链接连接的数据结构序列。每个链接都包含与其他节点的连接。它是仅次于数组的最常用的数据结构。在 C 编程语言中,链表可以使用结构体和指针实现。

struct LinkedList
   {
      int data;
      struct LinkedList *next;
   }

在这个 C 程序中,在包含头文件之后,我们进行变量声明;此外,我们还必须声明结构体和链表。然后我们为用户显示菜单,让用户选择要对链表执行哪些操作。根据选择调用相应的函数。这些函数是:

  • 插入
  • 打印
  • 删除
  • 搜索
  • 显示

insert() 函数将用于接受用户输入的参数,这些参数将被添加到链表中,并在链表中插入一个节点。printnode() 打印节点。deleteNode() 函数删除节点。search 函数将在链表中执行搜索操作。display() 函数将显示链表。

算法

步骤 1:包含头文件以使用 C 程序中的内置函数。

步骤 2:声明 EMP 为结构体,其成员包括 empno, empName[max], char designation[max],以及一个类型为 EMP 的指针 *next

步骤 3:声明类型为 EMP 的指针 *linklist,选择为字符类型,eno 为整数类型。

步骤 4:设置 linkList=NULL

步骤 5:显示“欢迎来到单链表的演示”。

步骤 6:调用函数 menu()

步骤 7:使用 do-while 循环检查 choice 的值

步骤 8:如果选择是 1,则将员工编号、员工姓名、员工职位读入变量 eno, name, desig,并执行步骤 9。

步骤 9:调用函数 insert() 将详细信息插入到链表中。

步骤 10:如果选择是 2,则读取要删除的员工编号到变量 eno 中,并调用 deleteNode()

步骤 11:如果选择是 3,检查 linklist=NULL,然后显示空列表。否则调用函数 display(linkList)

步骤 12:如果选择是 4,则将要搜索的员工编号读入变量 eno,并调用函数 search()

步骤 13:如果选择是 5,则退出。

函数 struct EMP* insert(struct EMP *front, int id, char name[], char desg[])

步骤 1:声明类型为 EMP 的指针 *newnode

步骤 2:分配 newnode=(struct EMP*) malloc(sizeof(struct EMP))。

步骤 3:如果 newnode 为空则退出,否则设置 newnode->empnn=id,将 name 复制到 newnode->empName,将 desg 复制到 newnode->designation,并设置 newnode->next=front

步骤 4:返回 front。

函数 printNode(struct EMP *p)

步骤 1:使用 printf 函数显示员工详细信息。

步骤 2:显示员工编号、姓名、职位为 p->empno, p->empName, p->designation

函数 struct EMP* deleteNode(struct EMP *front, int id)

步骤 1:声明类型为 EMP 的指针 *ptr, *bptr

步骤 2:检查 front->empno=id,然后设置 ptr=front

步骤 3:显示“节点已删除”。

步骤 4:调用函数 printNode(front)。

步骤 5:将 fr 设置为 next 并 free(ptr)

步骤 6:返回 front。

步骤 7:设置 ptr=front->next, bptr=front。

步骤 8:使用 for 循环,条件为 ptr!=null,执行步骤 9。

步骤 9:检查 if ptr->empno=id,然后显示“节点已删除”。Else 执行步骤 16。

步骤 10:调用函数 printNode(ptr)

步骤 11:设置 bptr->next=ptr->next。

步骤 12:调用函数 free(ptr)

步骤 13:返回 front。

步骤 14:调用函数 printNode(ptr)

步骤 15:设置 bptr->next=ptr->next 并重复步骤 8。

步骤 16:显示“未找到员工编号”。

步骤 17:返回 front。

函数 void search(struct EMP *front, int key)

步骤 1:声明类型为 EMP 的指针 *ptr

步骤 2:设置 ptr=front

步骤 3:使用 for 循环,条件为 ptr!=null,执行步骤 4。否则执行步骤 6。

步骤 4:检查 ptr->empno=key,然后显示“找到键”。

步骤 5:调用函数 printNode(ptr) 并返回。

步骤 6:显示“未找到员工编号”。

函数 void display(struct EMP *front)  

步骤 1:声明类型为 EMP 的指针 *ptr

步骤 2:设置 ptr=front

步骤 3:使用 for 循环,条件为 ptr!=null,执行步骤 4。否则执行步骤 6。

步骤 4:调用函数 printNode(ptr)

步骤 5:重复步骤 3。

函数 void menu()  

步骤 1:为用户显示菜单:1 为插入节点,2 为删除节点,3 为显示列表,4 为搜索,5 为退出。

函数 char option()

步骤 1:声明字符类型变量 choice。

步骤 2:显示“请输入您的选择”。

步骤 3:调用函数 choice=getche()

步骤 4:返回 choice。

C 语言源代码

                                          #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
 
struct emp_data
{
    int  empno;
    char empName[MAX];
    char designation[MAX];
    struct emp_data *next;
};
 

/*  Function to insert a node at the front of the linked list.        */

struct emp_data *insert(struct emp_data *front, int id, char name[],
char desg[])
{
    struct emp_data *newnode;
 
    newnode = (struct emp_data*)malloc(sizeof(struct emp_data));
 
    if (newnode == NULL)
    {
        printf("\n Allocation failed \n");
        exit(2);
    }
    newnode->empno = id;
    strcpy(newnode->empName, name);
    strcpy(newnode->designation, desg);
    newnode->next = front;     fr
    return(front);
}
/*  End of insert() */
 
/*  Function to display a node in a linked list */
void printNode(struct emp_data *p)
{
    printf("\n Employee Details...\n");
    printf("\n Emp No       : %d", p->empno);
    printf("\n Name           : %s", p->empName);
    printf("\n Designation    : %s\n", p->designation);
    printf("-------------------------------------\n");
}
/*  End of printNode() */
/*  Function to deleteNode a node based on employee number */

struct emp_data* deleteNode(struct emp_data *front, int id)
{
    struct emp_data *ptr;
    struct emp_data *bptr;
 
    if (front->empno == id)
    {
        ptr = front;
        printf("\n Node deleted:");
        printNode(front);         fr>next;
        free(ptr);
        return(front);
    }
    for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,
bptr = bptr->next)
    {
        if (ptr->empno == id)
        {
            printf("\n Node deleted:");
            printNode(ptr);
            bptr->next = ptr->next;
            free(ptr);
            return(front);
        }
    }
    printf("\n Employee Number %d not found ", id);
    return(front);
}
/*  End of deleteNode() */
/*  Function to search the nodes in a linear fashion based emp ID */

void search(struct emp_data *front, int key)
{
    struct emp_data *ptr;
 
    for (ptr = front; ptr != NULL; ptr = ptr -> next)
    {
        if (ptr->empno == key)
        {
            printf("\n Key found:");
            printNode(ptr);
            return;
        }
    }
    printf("\n Employee Number %d not found ", key);
}
/*  End of search() */
 
/*  Function to display the linked list */
void display(struct emp_data  *front)
{
    struct emp_data *ptr;
 
    for (ptr = front; ptr != NULL; ptr = ptr->next)
    {
        printNode(ptr);
    }
}
/*  End of display() */
 
/*  Function to display the menu of operations on a linked list */
void menu()
{
    printf("---------------------------------------------\n");
    printf("Press 1 to INSERT a node into the list       \n");
    printf("Press 2 to DELETE a node from the list       \n");
    printf("Press 3 to DISPLAY the list                 \n");
    printf("Press 4 to SEARCH the list                   \n");
    printf("Press 5 to EXIT                              \n");
    printf("---------------------------------------------\n");
}
/*  End of menu() */
 
/*  Function to select the option */
char option()
{
    char choice;
  
    printf("\n\n>> Enter your choice: ");

   
    switch(choice=getchar())
    {
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':   return(choice);
        default :   printf("\n Invalid choice.");
    }
    return choice;
}
/*  End of option() */
 
/*  The main() program begins */
void main()
{
    struct emp_data *linkList;
    char name[21], desig[51];
    char choice;
    int eno;
 
    linkList = NULL;
    printf("\n Welcome to demonstration of singly linked list \n");
    menu();
    do
    {
        /*  choose oeration to be performed */
        choice = option();
        switch(choice)
        {
        case '1':
            printf("\n Enter the Employee Number      : ");
            scanf("%d", & eno);
            printf("Enter the Employee name        : ");
            scanf("%s", & name);
            //gets(name);
            printf("Enter the Employee Designation : ");
            scanf("%s", & desig);
           // gets(desig);
            linkList = insert(linkList, eno, name, desig);
            break;
        case '2':
            printf("\n\n Enter the employee number to be deleted: ");
            scanf("%d", &eno;);
            linkList = deleteNode(linkList, eno);
            break;
        case '3':
            if (linkList == NULL)
            {
                printf("\n List empty.");
                break;
            }
            display(linkList);
            break;
        case '4':
            printf("\n\n Enter the employee number to be searched: ");
            scanf("%d", &eno;);
            search(linkList, eno);
            break;
        case '5': break;
        }
    } while (choice != '5');
}

                                      

输出

Welcome to a demonstration of a singly linked list
---------------------------------------------
Press 1 to INSERT a node into the list

Press 2 to DELETE a node from the list

Press 3 to DISPLAY the list

Press 4 to SEARCH the list

Press 5 to EXIT
---------------------------------------------


>> Enter your choice: 1
Enter the Employee Number: 1234
Enter the Employee name: Keerthi
Enter the Employee Designation: Engineer


>> Enter your choice: 1
Enter the Employee Number: 2345
Enter the Employee name: Srinivasan
Enter the Employee Designation: Specialist


>> Enter your choice: 1
Enter the Employee Number: 4567
Enter the Employee name: Annapurna
Enter the Employee Designation: Project Manager



>> Enter your choice: 3
Employee Details...
Emp No: 4567
Name: Annapoorna
Designation: Project Manager
-------------------------------------

Employee Details...

Emp No    : 2345
Name        : Srinivasan
Designation : Specilist
-------------------------------------

Employee Details...
Emp No: 1234
Name: Keerthi
Designation: Engineer
-------------------------------------


>> Enter your choice: 2
Enter the employee number to be deleted: 2345
Node deleted:

Employee Details...
Emp No: 2345
Name: Srinivasan
Designation: Specialist
-------------------------------------


>> Enter your choice: 3
Employee Details...
Emp No: 4567
Name: Annapurna
Designation: Project Manager
-------------------------------------

Employee Details...
Emp No: 1234
Name: Keerthi
Designation: Engineer
-------------------------------------


>> Enter your choice: 4
Enter the employee number to be searched: 2345
Employee Number 2345 not found
>> Enter your choice: 5