链接列表以递归语言C添加元素

时间:2016-07-19 15:27:49

标签: 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);

}

添加功能有问题,它无法正常添加新项目到链表,我不知道有什么不对请帮助 添加新项目并执行打印列表后,它仅显示第一项

1 个答案:

答案 0 :(得分:0)

在你main函数 -

 pnode = addition(pnode, numAdd);

而不是pnode您需要传递pnode->next -

 pnode = addition(pnode->next, numAdd);

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

因此,未创建新节点。