易百教程

以交互方式构建链表

以交互方式构建链表,参考以下代码:

示例代码

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 

struct Node {
    int value;
    struct Node *next;
};
struct Node *first;
struct Node *current;
struct Node *newCurrent;

int menu(void);
void add(void);
void show(void);
void deleteNode(void);
struct Node *create(void);

int main()
{
    int choice = '\0';    
    first = NULL;

    while (choice != 'Q')
    {
        choice = menu();
        switch (choice)
        {
            case 'S':
                show();
                break;
            case 'A':
                add();
                break;
            case 'R':
                deleteNode();
                break;
            case 'Q':
                break;
            default:
                break;
        }
    }

    return(0);
}

/* 显示主菜单并收集输入 */
int menu(void)
{
    int ch;

    printf("S)how, A)dd, R)emove, Q)uit: ");
    ch = getchar();
    while (getchar() != '\n')     /* 删除多余的输入 */
        ;
    return(toupper(ch));
}

/* 将项添加到链表的末尾 */
void add(void)
{
    if (first == NULL)
    {
        first = create();
        current = first;
    }
    else
    {
        current = first;
        while (current->next)
            current = current->next;
        newCurrent = create();
        current->next = newCurrent;
        current = newCurrent;
    }
    printf("输入一个整数值: ");
    scanf("%d", ¤t->value);
    current->next = NULL;
    while (getchar() != '\n')
        ;
}

void show(void)
{
    int count = 1;

    if (first == NULL)
    {
        puts("没有内容可以显示");
        return;
    }
    puts("显示所有记录信息:");
    current = first;
    while (current)
    {
        printf("记录 %d: %d\n", count, current->value);
        current = current->next;
        count++;
    }
}
void deleteNode(void)
{
    struct Node *previous;
    int r, c;

    if (first == NULL)
    {
        puts("没有记录可以删除");
        return;
    }
    puts("选择一个要删除的记录:");
    show();
    printf("记录: ");
    scanf("%d", &r);
    while (getchar() != '\n')
        ;
    c = 1;
    current = first;
    previous = NULL;
    while (c != r)
    {
        if (current == NULL)
        {
            puts("未找到记录");
            return;
        }
        previous = current;
        current = current->next;
        c++;
    }
    if (previous == NULL)
        first = current->next;
    else
        previous->next = current->next;
    printf("记录 %d 被删除了.\n", r);
    free(current);
}

struct Node *create(void)
{
    struct Node *a;

    a = (struct Node *)malloc(sizeof(struct Node));
    if (a == NULL)
    {
        puts("某种 malloc()错误");
        exit(1);
    }
    return(a);

}

执行上面示例代码,得到以下结果:

S)how, A)dd, R)emove, Q)uit: a
输入一个整数值: 123
S)how, A)dd, R)emove, Q)uit: a
输入一个整数值: 234
S)how, A)dd, R)emove, Q)uit: a
输入一个整数值: 345
S)how, A)dd, R)emove, Q)uit: a
输入一个整数值: 456
S)how, A)dd, R)emove, Q)uit: s
显示所有记录信息:
记录 1: 123
记录 2: 234
记录 3: 345
记录 4: 456
S)how, A)dd, R)emove, Q)uit: r
选择一个要删除的记录:
显示所有记录信息:
记录 1: 123
记录 2: 234
记录 3: 345
记录 4: 456
记录: