简单的单链列表问题-节点值不显示

时间:2019-03-01 17:12:06

标签: c linked-list

我是Noob,尝试根据C字符串数组(其中数组可以具有不同的长度,项目的大小也不同)在C中构建单链接列表。在修改后的代码中,我似乎获得了正确数量的指针,但是没有显示Node值。控制台所需的输出是head->一个->两个->等(现在,我只是在head->->->)

// starter code from https://www.tutorialspoint.com/learn_c_by_examples/simple_linked_list_program_in_c.htm

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

struct node {
    const char *data;
    struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {

    struct node *ptr = head;

    printf("\n[head] =>");
    //start from the beginning
    while(ptr != NULL) {  
        printf(" %s =>", ptr -> data);
        ptr = ptr -> next;
    }

    printf(" [null]\n");
}

//insert link at the first location
void insert(const char data) {
    //create a link
    struct node *link = (struct node*) malloc(sizeof(struct node));

    //link->key = key;
    link -> data = &data;

    //point it to old first node
    link -> next = head;

    //point first to new first node
    head = link;
    }

int main() {
    // create an array of strings, to be used to dynamically populate linked list
    const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};

    // below line to find number of items in an array of strings, assign to n
    int n = sizeof(strings) / (2 * sizeof(int));

    for (int j = 0; j < n; j++){
        const char *temp = strings[j];
        insert(*temp);
        //printf("%s\n", temp);

    }

    printList();
    return 0;
}

3 个答案:

答案 0 :(得分:1)

08:00 Shift
08:01 Shift
.
.
.
08:09 Shift
08:10 WorkEvent
.
.
.
09:59 WorkEvent
10:00 Break

这是错误的。

您要插入一个字符。好吧,具体地说,您要插入一个指向该单个字符的指针:

void insert(const char data) 

...但是(a)该字符在堆栈上,因此指针不会在很长一段时间内保持有效,并且(b)它不是您认为的字符串。

此:

link -> data = &data;

应该是:

insert(*temp);

...并且您应该更改原型和实现以匹配。

还请注意,您可能想要复制字符串(使用insert(temp); ,但要注意内存泄漏),而不要指向原始副本。

答案 1 :(得分:1)

尝试使用此代码

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

struct node {
    char *data;
    struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {

    struct node *ptr = head;

    printf("\n[head] =>");
    //start from the beginning
    while(ptr != NULL) {  
        printf(" %s =>", ptr -> data);
        ptr = ptr -> next;
    }

    printf(" [null]\n");
}

//insert link at the first location
void insert(char *data) {
    //create a link
    struct node *link = (struct node*) malloc(sizeof(struct node));
link->data=malloc(strlen(data)*sizeof(char)); //don't forget malloc for string : link->data
    strcpy(link->data,data); //copy  parameter named data to link->data

    //point it to old first node
    link -> next = head;

    //point first to new first node
    head = link;

    }

int main() {
    // create an array of strings, to be used to dynamically populate linked list
    const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};

    // below line to find number of items in an array of strings, assign to n
    int n = sizeof(strings) / (2 * sizeof(int));
    char temp[20]={0};
    for (int j =n-1; j >=0; j--){
        //const char *temp = strings[j];
        strcpy(temp,strings[j]);
        insert(temp);
        //printf("%s\n", temp);

    }

    printList();
    return 0;
}

答案 2 :(得分:0)

由于以下3种语法,我的代码无法正常工作

// starter code from https://www.tutorialspoint.com/learn_c_by_examples/simple_linked_list_program_in_c.htm

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

struct node {
    const char *data;
    struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {

    struct node *ptr = head;

    printf("\n[head] =>");
    //start from the beginning
    while(ptr != NULL) {  
        printf(" %s =>", ptr -> data);
        ptr = ptr -> next;
    }

    printf(" [null]\n");
}

//insert link at the first location
// first change insert *
void insert(const char *data) {
    //create a link
    struct node *link = (struct node*) malloc(sizeof(struct node));

    //link->key = key;
    // 2nd change remove &
    link -> data = data;

    //point it to old first node
    link -> next = head;

    //point first to new first node
    head = link;
    }

int main() {
    // create an array of strings, to be used to dynamically populate linked list
    const char *strings[] = {"one" ,"two","three", "four", "somewhereselse", "onemore"};

    // below line to find number of items in an array of strings, assign to n
    int n = sizeof(strings) / (2 * sizeof(int));

    for (int j = 0; j < n; j++){
        const char *temp = strings[j];
        // 3rd change remove * before temp
        insert(temp);
        //printf("%s\n", temp);

    }

    printList();
    return 0;
}