当使用C链表 [英] Using linked list in C

查看:120
本文介绍了当使用C链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的链表的话题,我只是用链表创建了我的第一个程序,问题是它没有任何数据保存到结构。它运行正常,没有错误,但没有打印数据时显示。这里是我的code。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;结构节点{
    诠释的NID;
    焦炭chTitle;
    结构节点*接下来的;
};无效addList(结构节点*头);
无效的printList(结构节点*头);
INT checkID(结构节点*头,诠释T);INT主(INT ARGC,为const char * argv的[])
{
    INT nInput;
    结构节点*头= NULL;
    而(1)
    {
        的printf(\\ n \\ t \\ t ~~ MENU ~~ \\ n);
        的printf(1添加新书\\ n);
        的printf(2打印所有数据\\ n);
        的printf(3退出\\ n);
        的printf(请选择:);
        scanf函数(%d个,&安培; nInput);        开关(nInput)
        {
            情况1:
                addList(头);
                打破;
            案例2:
                的printList(头);
                打破;
            案例3:
                的printf(\\ nGoodby!感谢您使用的程序\\ n);
                出口(1);
                打破;
            默认:
                的printf(\\ n \\ t \\ t ~~无效的输入~~ \\ n);
                打破;
        }
    }
    返回0;
}无效addList(结构节点*头)
{
    INT BOOKID; //用来存储BOOK ISBN因此它可以,如果它已经存在进行检查
    结构节点*温度;    TEMP =(结构节点*)malloc的(的sizeof(结构节点));
    的printf(\\ n输入图书详细\\ n);
    的printf(请输入书ISBN:);
    scanf函数(%d个,&安培; BOOKID);
    INT BIND = checkID(头,BOOKID);
    如果(BIND == 0)
    {
        的printf(请输入标题:);
        scanf函数(%S,&安培; TEMP-GT&; chTitle);
        TEMP->接下来=头;
        头=温度;
    }
    其他
    {
        的printf(\\ nSorry使用ID另一本书\\ n!);
    }
}无效的printList(结构节点*头)
{
    而(头!= NULL)
    {
        的printf(%S,&安培;流浆> chTitle);
        头=流浆>接下来,
    }
}INT checkID(结构节点*头,诠释T)
{
    头= NULL;
    而(头!= NULL)
    {
        如果(流浆>的NID == T)
            返回1;
        头=流浆>接下来,
    }
    返回0;
}


解决方案

一个问题就在这里:

 无效addList(结构节点*头)

addList()是越来越头指针的副本,所以,当你在这个函数修改它,你只修改本地副本。呼叫者的版本不会被修改。解决这个的一种方式是通过使用一个双指针:

 无效addList(结构节点**头)
{
    INT BOOKID; //用来存储BOOK ISBN因此它可以,如果它已经存在进行检查
    结构节点*温度;    TEMP =(结构节点*)malloc的(的sizeof(结构节点));
    的printf(\\ n输入图书详细\\ n);
    的printf(请输入书ISBN:);
    scanf函数(%d个,&安培; BOOKID);
    INT BIND = checkID(*头,BOOKID);
    如果(BIND == 0)
    {
        的printf(请输入标题:);
        scanf函数(%S,&安培; TEMP-GT&; chTitle);
        TEMP->接下来= *头;
        *头=温度;
    }
    其他
    {
        的printf(\\ nSorry使用ID另一本书\\ n!);
    }
}

那么你的来电者有可能改变过:

  addList(安培;头);

此外,如@ 5gon12eder提到的,字符只拥有一个字符。你需要一个字符数组来保存你的标题:

 结构节点{
    诠释的NID;
    炭chTitle [100]; / *或怎么过长时间你的标题可* /
    结构节点*接下来的;
};

I'm new to the linked list topic and I just created my first program using linked lists, the problem is it's not saving any data to the structure. It runs fine, no error, but when printing no data is displayed. Here is my code.

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

struct node {
    int     nID;
    char    chTitle;
    struct node* next;
};

void addList(struct node *head);
void printList(struct node *head);
int checkID(struct node *head, int t);

int main(int argc, const char * argv[])
{
    int nInput;
    struct node *head = NULL;
    while (1)
    {
        printf("\n\t\t~~MENU~~\n");
        printf("1. Add a new book\n");
        printf("2. Print all data\n");
        printf("3. Exit\n");
        printf("Make your selection: ");
        scanf("%d", &nInput);

        switch (nInput)
        {
            case 1:
                addList(head);
                break;
            case 2:
                printList(head);
                break;
            case 3:
                printf("\nGoodby!!! Thanks for using the program\n");
                exit(1);
                break;
            default:
                printf("\n\t\t~~Invalid Input~~\n");
                break;
        }
    }
    return 0;
}

void addList(struct node *head)
{
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist
    struct node *temp;

    temp = (struct node *)malloc(sizeof(struct node));


    printf("\n Enter Book Details\n");
    printf("Enter book ISBN: ");
    scanf("%d", &bookId);
    int bInd = checkID(head, bookId);
    if (bInd == 0)
    {
        printf("Enter title: ");
        scanf("%s", &temp->chTitle);
        temp->next = head;
        head = temp;
    }
    else
    {
        printf("\nSorry another book using that id!\n" );
    }
}

void printList(struct node* head)
{
    while (head != NULL)
    {
        printf("%s", &head->chTitle);
        head = head->next;
    }
}

int checkID(struct node *head, int t)
{
    head = NULL;
    while (head != NULL)
    {
        if (head->nID == t)
            return 1;
        head = head->next;
    }
    return 0;
} 

解决方案

One problem is right here:

void addList(struct node *head)

addList() is getting a COPY of the head pointer, so when you modify it in this function you are only modifying that local copy. The caller's version is not modified. One way to solve this is by using a double pointer:

void addList(struct node **head)
{
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist
    struct node *temp;

    temp = (struct node *)malloc(sizeof(struct node));


    printf("\n Enter Book Details\n");
    printf("Enter book ISBN: ");
    scanf("%d", &bookId);
    int bInd = checkID(*head, bookId);
    if (bInd == 0)
    {
        printf("Enter title: ");
        scanf("%s", &temp->chTitle);
        temp->next = *head;
        *head = temp;
    }
    else
    {
        printf("\nSorry another book using that id!\n" );
    }
}

Then your caller has to change too:

addList(&head);

Also, as @5gon12eder mentioned, a char holds only one character. You'll need a char array to hold your title:

struct node {
    int     nID;
    char    chTitle[100]; /* or how ever long your title can be */
    struct node* next;
};

这篇关于当使用C链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆