c - 递归语言C中的链接列表添加元素

标签 c list recursion hyperlink

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

typedef struct nodeNum
{
    int num;
    struct nodeNum *next;
} t_nodeNum;


// Functions declaration  ----------------------------
int menu();  // display menu and return choice

t_nodeNum* addition(t_nodeNum *node, int n);

void print_list(t_nodeNum *node);
// ----------------------------------------------------

// Main program to test link list functions
int main()
{
    int choice;

    t_nodeNum *pnode = NULL;
    t_nodeNum *head = NULL;
    t_nodeNum *temp = NULL;

    int numAdd = 0;
    int len = 0;
    int first = 1;

    do
    {
        choice = menu();

        switch (choice)
        {
            case 1:
            {
                printf("Please enter number : \n");
                scanf("%d", &numAdd);
                if (first)
                {
                    pnode = (t_nodeNum *)malloc(sizeof(t_nodeNum));
                    if (pnode == NULL)
                    {
                        printf("\n Error in allocation\n");
                        exit(0);
                    }

                    pnode->num = numAdd;
                    pnode->next = NULL;
                    first = 0;
                    head = pnode;
                }
                pnode = addition(pnode, numAdd);
                break;
            }

            case 4:
            {
                printf("\n Print List: ");
                print_list(head);
                break;
            }
        }
    }
    while (choice != 5);

    return 0;
}

// function menu display menu and return choice
int menu()
{
    int choice = 0;

    do
    {
        printf("Please choose option to do: \n");
        printf("1. addition\n");
        printf("2. deletion\n");
        printf("3. search\n");
        printf("4. print\n");
        printf("5. exit\n");
        printf("\n option = ");

        scanf("%d", &choice);
    }
    while (choice < 1 || choice > 5);

    return choice;
}

// function addition to add item to linked list in recursion
t_nodeNum* addition(t_nodeNum *p, int numAdd)
{
    int len = 0;

    if (p == NULL)
    {
        p = (t_nodeNum *)malloc(sizeof(t_nodeNum));
        if (p == NULL)
        {
            printf("\n Error in allocation\n");
            exit(0);
        }

        p->num = numAdd;
        p->next = NULL;
    }
    else
    {
        p = addition(p->next, numAdd);
    }
    return (p);

}

// function print_list to print linked list in recursion
void print_list(t_nodeNum *head)
{       
        printf("%d    ", head->num);
        if (head->next == NULL)
        {
            printf("\n");
            return;
        }

        print_list(head->next);

}

添加功能有问题,无法正常向链接列表添加新项目,我不知道出了什么问题,请帮忙 添加新项目并打印列表后,它仅显示第一个项目

最佳答案

在你的main函数中 -

 pnode = addition(pnode, numAdd);

您需要传递 pnode->next -

而不是 pnode
 pnode = addition(pnode->next, numAdd);

第一次调用的问题是pnode不是NULL,所以它只是在头部位置添加新元素替换以前的值并返回。

因此,不会创建新节点。

关于c - 递归语言C中的链接列表添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462754/

相关文章:

c - 如何使用 C 中的 for 循环打印答案序列?

c - C中简单函数的问题

python - 如何在Python中将字符串转换为列表?

c - 我们如何在 C 中使用递归打印系列 1+ 11 +111+........ 最多 N 项的总和

java - Java 中的斐波那契内存/动态编程

c - X86 64 位程序集 Linux 'Hello World' 链接问题

c - 是否可以使用 scanf 检测新行输入?

python - 在 python 中写入二进制输出最有效的方法是什么?

python - 理解列表理解以展平 python 中的列表列表

r - R 未使用可用堆栈大小,返回 "Error: node stack overflow"