c - 在链表的实现中在函数之间传递指针

标签 c linked-list

问题解决了。一个人在评论中给出了它。问题是我正在使用 %d 读取 short int。我应该使用 %hd 或者我应该使用 `int'。


我试图创建一个单链表程序只使用局部变量。我能够通过使用全局变量来制作一个工作程序。

带有局部变量的程序可以编译,但是当我尝试遍历链表时它崩溃了。

我完全不知道局部变量的实现有什么问题。局部变量实现中存在什么问题?

关于类(class)结构:

我知道这些程序很大,所以我会加入一些关于程序结构的内容。

  • 该程序的结构是菜单驱动程序。所以对函数的初始调用是在 main() 函数中
  • main() 菜单中有 3 个选项 - 退出、遍历和插入
  • Exit返回0退出程序,其他2做函数调用

  • 插入功能本身被安排为菜单驱动程序。

  • 它有 3 个选项 - return、insert_begin 和 insert_end。最后两个是函数调用。

  • 我知道存在内存泄漏,因为我没有释放任何内存,但我会在了解当前程序中的问题后处理它。

//使用全局变量的工作实现

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

#define MIN 0
#define MAX 2

#define INS_MIN 0
#define INS_MAX 2

typedef struct node
{
    int data;
    struct node *next;
}sll_node;

sll_node *start = NULL;

void intro()
{
    system("cls");
    printf("\n\tThese are the various options:\n");
    printf("\n\t00 Exit");
    printf("\n\t01 Traverse the list");
    printf("\n\t02 Insertion into the list");
}

void insert_begin()
{
    sll_node *node = malloc(sizeof(sll_node));
    if(node == NULL)
    {
        printf("\n\tNot enough menory");
        exit(-1);
    }
    int data;
    printf("\n\tData to be entered: ");
    scanf("%d", &data);

    node->data = data;
    node-> next = start;
    start = node;
}

void insert_end()
{
    sll_node *node = malloc(sizeof(sll_node));
    if(node == NULL)
    {
        printf("\n\tNot enough menory");
        exit(-2);
    }

    if(start == NULL)
        insert_begin();
    else
    {
        printf("\n\tData to be entered: ");
        scanf("%d", &(node->data));
        node-> next = NULL;

        sll_node *node2;
        for(node2 = start; node2->next != NULL; node2 = node2->next)
            ;
        node2->next = node;
    }
}

void insert_intro()
{
    system("cls");
    printf("\n\tThese are the various options:\n");
    printf("\n\t00 Insertion Done");
    printf("\n\t01 Insert at beginning");
    printf("\n\t02 Insert at end");
}

void insertion()
{
    short choice;
    while(1)
    {
        choice = -1;
        while(choice < INS_MIN || choice > INS_MAX)
        {
            insert_intro();
            printf("\n\n\tEnter your chocie: ");
            scanf("%d", &choice);
        }

        switch(choice)
        {
        case 0:
            return;
        case 1:
            insert_begin();
            break;
        case 2:
            insert_end();
            break;
        }
    }
}

void traverse()
{
    if(start == NULL)
        printf("\n\n\tLinked list is empty");
    else
    {
        printf("\n\n\t");
        for(sll_node *node = start; node != NULL; node = node->next)
            printf("%d ", node->data);
    }
    getch();
}

int main()
{
    short choice;
    while(1)
    {
        choice = -1;
        while(choice < MIN || choice > MAX)
        {
            intro();
            printf("\n\n\tEnter your choice: ");
            scanf("%d", &choice);
        }

        switch(choice)
        {
        case 0:
            return 0;
        case 1:
            traverse();
            break;
        case 2:
            insertion();
            break;
        }
    }
    return 0;
}

//编译但崩溃 - 相同的程序,但具有局部变量启动和函数之间的变量传递

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

#define MIN 0
#define MAX 2

#define INS_MIN 0
#define INS_MAX 2

typedef struct node
{
    int data;
    struct node *next;
}sll_node;

void intro()
{
    system("cls");
    printf("\n\tThese are the various options:\n");
    printf("\n\t00 Exit");
    printf("\n\t01 Traverse the list");
    printf("\n\t02 Insertion into the list");
}

sll_node* insert_begin(sll_node *start)
{
    sll_node *node = malloc(sizeof(sll_node));
    if(node == NULL)
    {
        printf("\n\tNot enough menory");
        exit(-1);
    }
    int data;
    printf("\n\tData to be entered: ");
    scanf("%d", &data);

    node->data = data;
    node-> next = start;
    return node;
}

sll_node* insert_end(sll_node *start)
{
    sll_node *node = malloc(sizeof(sll_node));
    if(node == NULL)
    {
        printf("\n\tNot enough menory");
        exit(-2);
    }

    if(start == NULL)
        start = insert_begin(start);
    else
    {
        printf("\n\tData to be entered: ");
        scanf("%d", &(node->data));
        node-> next = NULL;

        sll_node *node2;
        for(node2 = start; node2->next != NULL; node2 = node2->next)
            ;
        node2->next = node;
    }
    return start;
}

void insert_intro()
{
    system("cls");
    printf("\n\tThese are the various options:\n");
    printf("\n\t00 Insertion Done");
    printf("\n\t01 Insert at beginning");
    printf("\n\t02 Insert at end");
}

sll_node* insertion(sll_node *start)
{
    short choice;
    while(1)
    {
        choice = -1;
        while(choice < INS_MIN || choice > INS_MAX)
        {
            insert_intro();
            printf("\n\n\tEnter your chocie: ");
            scanf("%d", &choice);
        }

        switch(choice)
        {
        case 0:
            return start;
        case 1:
            start = insert_begin(start);
            break;
        case 2:
            start = insert_end(start);
            break;
        }
    }
}

void traverse(sll_node *start)
{
    if(start == NULL)
        printf("\n\n\tLinked list is empty");
    else
    {
        printf("\n\n\t");
        for(sll_node *node = start; node != NULL; node = node->next)
            printf("%d ", node->data);
    }
    getch();
}

int main()
{
    sll_node *start = NULL;
    short choice;
    while(1)
    {
        choice = -1;
        while(choice < MIN || choice > MAX)
        {
            intro();
            printf("\n\n\tEnter your choice: ");
            scanf("%d", &choice);
        }

        switch(choice)
        {
        case 0:
            return 0;
        case 1:
            traverse(start);
            break;
        case 2:
            start = insertion(start);
            break;
        }
    }
    return 0;
}

最佳答案

将项目添加到列表时,您不会从 insertion() 函数返回任何内容。因此链表可能无法正确构建。

可能,你应该只在开始时返回 start ,否则 main() 中的 start 将不会指向 head of列表。

sll_node* insertion(sll_node *start)
{
        ...
        switch(choice)
        {
        case 0:
            return start;
        case 1:
            start = insert_begin(start);
            return start;  //<----- return node
            break;
        case 2:
            start = insert_end(start);
            break;
        }
    ...

}

关于c - 在链表的实现中在函数之间传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17258476/

相关文章:

java - 单链表和双向链表 (Java)

java - 在 Java 中将数组添加到队列(指针问题?)

c - 对 Data.Vector 进行基准测试时的时间出人意料地低

C链表追加到头还是尾?

c - 使用递归函数反转字符串

c - 文件名上的 Posix I/O 操作顺序一致吗?

c - C 中的链表包含结构体

java - 父类(super class)类型或子类类型

c - GCC 程序未定义对函数的引用(多个文件夹)

c++ - C/C++ Windows 或 Linux 将随机内存块映射为连续顺序