鱼C论坛

 找回密码
 立即注册
查看: 632|回复: 1

[已解决]单链表,为什么无法打印节点,看子函数PrintTable即可

[复制链接]
发表于 2022-3-17 15:00:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪猪虾 于 2022-3-17 15:05 编辑
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/******************************************************
要求:
        定义一个结构体Node,结构体由两部分组成,第一个是它本身的值,第二个是下一个元素的位置
        数组有3个节点,3个节点的关系如下
                0                       1                       2
        node1(5,2)                                node2(7,-1)                                node3(8,1)

        node1是第一个节点,指向的下一个节点的位置是2,也即是node3,node3指向的节点是位置1,也即是node2

********************************************************/

#define Size 10
void getInput(struct Node* node);
void HeadInsert(struct Node** Ptable);
void PrintTable(struct Node* Ptable);
void initialTable(struct Node* Table[]);
void ReleaseTable(struct Node** Ptable);


struct Node
{
        int value;
        int nextNodeIndex;
        struct Node* next;
};

void getInput(struct Node* node)
{
        printf("enter node'value:");
        scanf_s("%d", &node->value);  //这是scanf的基本用法,要传入输入变量的地址,node->value是结构体对应的成员
        printf("enter node'value:");  //当输入的是是字符串的时候,字符串变量名本身就是首地址
        scanf_s("%d", &node->nextNodeIndex);
}

void HeadInsert(struct Node** Ptable)
{
        //头插法:永远将数据插入链表的头部,称为头插法
        struct Node* temp;

        struct Node* node = new Node;
        if (node == NULL)
        {
                printf("failed to distribute memory....\n");
                exit(1);
        }

        getInput(node);


        if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
        {
                temp = *Ptable;
                *Ptable = node;
                node->next = temp;
        }
}

void PrintTable(struct Node* Ptable)
{
        struct Node* node;
        int count = 1;
        node = Ptable;

        while (node != NULL)
        {
                printf("node'value: %d\n", node->value);
                printf("enter node'value: %d\n", node->nextNodeIndex);
                printf("\n");
                node = node->next;
                count++;
        }
}

void initialTable(struct Node* Table[])
{
        for (int i = 0; i < Size; i++)
        {
                Table[i] = NULL;
        }
}

void ReleaseTable(struct Node** Ptable)
{
        struct Node* temp;
        for (int i = 0; i < Size; i++)
        {
                if (*Ptable != NULL) //*Ptable这里是解引用,对应第一个节点的地址
                {
                        temp = *Ptable;
                        *Ptable = (*Ptable)->next;
                        free(temp);
                }
        }
}

int main()
{
        struct Node* Table[Size];
        struct Book* ptr = NULL;
        initialTable(Table);

        HeadInsert(Table);

        PrintTable(*Table);

        //Table 的定义是   struct Node* Table[Size];
        //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
        //是一个指针数组,存放的是 struct Node* 类型的指针
        //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
        ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
        return 0;
}
最佳答案
2022-3-17 15:53:46
本帖最后由 jhq999 于 2022-3-17 15:58 编辑
void HeadInsert(struct Node** Ptable)///////主函数传进来的* Ptable=NULL;
{
        //头插法:永远将数据插入链表的头部,称为头插法
        struct Node* temp;

        struct Node* node = new Node;
        if (node == NULL)
        {
                printf("failed to distribute memory....\n");
                exit(1);
        }

        getInput(node);


        if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
        {
                temp = *Ptable;
                *Ptable = node;
                node->next = temp;
        }
}
int main()
{
        struct Node* Table[Size];
        struct Book* ptr = NULL;
        initialTable(Table);//Node指针数组Table所有的指针初始化成NULL

        HeadInsert(Table);//*Table=NULL传进去的也是NULL

        PrintTable(*Table);

        //Table 的定义是   struct Node* Table[Size];
        //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
        //是一个指针数组,存放的是 struct Node* 类型的指针
        //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
        ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-17 15:53:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-3-17 15:58 编辑
void HeadInsert(struct Node** Ptable)///////主函数传进来的* Ptable=NULL;
{
        //头插法:永远将数据插入链表的头部,称为头插法
        struct Node* temp;

        struct Node* node = new Node;
        if (node == NULL)
        {
                printf("failed to distribute memory....\n");
                exit(1);
        }

        getInput(node);


        if (*Ptable != NULL)  //判断传进来的单链表是不是空的,即判断第第一个节点的地址是不是空的
        {
                temp = *Ptable;
                *Ptable = node;
                node->next = temp;
        }
}
int main()
{
        struct Node* Table[Size];
        struct Book* ptr = NULL;
        initialTable(Table);//Node指针数组Table所有的指针初始化成NULL

        HeadInsert(Table);//*Table=NULL传进去的也是NULL

        PrintTable(*Table);

        //Table 的定义是   struct Node* Table[Size];
        //这是一个指针数组,实际上与  struct Node** 是相同的数据类型
        //是一个指针数组,存放的是 struct Node* 类型的指针
        //        Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node* 的地址
        ReleaseTable(Table); //* Table[]是指针数组,存放的是指针,Table指代的第一个元素的地址
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 12:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表