链接列表未按预期显示

时间:2015-11-05 02:28:10

标签: c linked-list

我目前正在为我的C编程课程解决一个问题,并且我的程序遇到了一些问题。我设法让程序编译,但显示功能实际上并没有显示列表的内容。这是我的代码:

/**
 * Create a structure that has one variable called value and one pointer to the list (making it a
 * linked list). Prompt for 5 values from the keyboard as input and store them in the linked list. Print
 * out the current contents of the list. Allow the user to add one more value to the linked list, and
 * print the contents of the list again.
 */

#include <stdio.h>

struct ValueStore
{
    char value[100];
    struct ValueStore *nextItem;
};
typedef struct ValueStore ValueStore;

void display(ValueStore *);

int main()
{
    printf("This program will ask you for five words or phrases and store them in a list before repeating those integers back to you.\n");
    printf("You will then be able to add a final value (for a total of six) before the program repeats the list back to you and finishes.\n");

    /**
     * Initialize the first five structs in the list.
     */
    char inChar1[100];
    char inChar2[100];
    char inChar3[100];
    char inChar4[100];
    char inChar5[100];
    printf("Please enter a word or phrase: ");
    gets(inChar1);
    printf("Please enter a word or phrase: ");
    gets(inChar2);
    printf("Please enter a word or phrase: ");
    gets(inChar3);
    printf("Please enter a word or phrase: ");
    gets(inChar4);
    printf("Please enter a word or phrase: ");
    gets(inChar5);
    ValueStore fifth = { inChar5 };
    ValueStore fourth = { inChar4, &fifth };
    ValueStore third = { inChar3, &fourth };
    ValueStore second = { inChar2, &third };
    ValueStore first = { inChar1, &second };

    display(&first); //print out the list starting at first

    /*
     * add final item to list
     */
    char inChar6[100];
    printf("Please enter a word or phrase: ");
    gets(inChar6);
    ValueStore sixth = { inChar6 };
    fifth.nextItem = &sixth; //link fifth and sixth together

    display(&first);
}

void display(ValueStore *listStart)
{
    while(listStart)
    {
        printf("%s\n", listStart->value);
        listStart = listStart->nextItem;
    }
}

当我尝试运行该程序并输入&#34; hello&#34;,&#34; world&#34;,&#34; what&#34;,&#34;&#34;和& #34;向上&#34;为了回答提示我的显示功能输出一行�p并进入下一个输入。我输入了最后一个字符串(说&#34;你好&#34;),我再次在终端获得�p

我知道不推荐使用gets()而不支持fgets(),但是我的指令明确地使用了gets(),所以我的手被束缚在那里。

我的gcc编译器完全是最新的

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.2.1-22ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2)

1 个答案:

答案 0 :(得分:1)

事实上,编译器在代码中给出了很多关于该问题的指示(也就是警告):

test41.c: In function ‘main’:
test41.c:35: warning: initialization makes integer from pointer without a cast
test41.c:36: warning: initialization makes integer from pointer without a cast
test41.c:36: warning: initialization makes integer from pointer without a cast
test41.c:37: warning: initialization makes integer from pointer without a cast
test41.c:37: warning: initialization makes integer from pointer without a cast
test41.c:38: warning: initialization makes integer from pointer without a cast
test41.c:38: warning: initialization makes integer from pointer without a cast
test41.c:39: warning: initialization makes integer from pointer without a cast
test41.c:39: warning: initialization makes integer from pointer without a cast
test41.c:49: warning: initialization makes integer from pointer without a cast

但是,此代码更改将起作用。

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

struct ValueStore
{
    char value[100];
    struct ValueStore *nextItem;
};
typedef struct ValueStore ValueStore;

void display(ValueStore *);

int main()
{
    printf("This program will ask you for five words or phrases and store them in a list before repeating those integers back to you.\n");
    printf("You will then be able to add a final value (for a total of six) before the program repeats the list back to you and finishes.\n");

    /**
     * Initialize the first five structs in the list.
     */
    char inChar1[100];
    char inChar2[100];
    char inChar3[100];
    char inChar4[100];
    char inChar5[100];
    printf("Please enter a word or phrase: ");
    gets(inChar1);
    printf("Please enter a word or phrase: ");
    gets(inChar2);
    printf("Please enter a word or phrase: ");
    gets(inChar3);
    printf("Please enter a word or phrase: ");
    gets(inChar4);
    printf("Please enter a word or phrase: ");
    gets(inChar5);
    ValueStore fifth;
    strcpy(fifth.value, inChar5 );
    fifth.nextItem = NULL;

    ValueStore fourth;
    strcpy(fourth.value, inChar4);
    fourth.nextItem =  &fifth;

    ValueStore third;
    strcpy(third.value, inChar3);
    third.nextItem =  &fourth;
    ValueStore second;
    strcpy(second.value, inChar2);
    second.nextItem =  &third;

    ValueStore first;
    strcpy(first.value, inChar1);
    first.nextItem =  &second;


    display(&first); //print out the list starting at first

    /*
     * add final item to list
     */
    char inChar6[100];
    printf("Please enter a word or phrase: ");
    gets(inChar6);
    ValueStore sixth;
    strcpy(sixth.value, inChar6);
    sixth.nextItem = NULL;
    fifth.nextItem = &sixth; //link fifth and sixth together

    display(&first);
}

void display(ValueStore *listStart)
{
    while(listStart)
    {
        printf("%s\n", listStart->value);
        listStart = listStart->nextItem;
    }
}

输出:根据您的问题。

./a.out
This program will ask you for five words or phrases and store them in a list before repeating those integers back to you.
You will then be able to add a final value (for a total of six) before the program repeats the list back to you and finishes.
Please enter a word or phrase: hello
Please enter a word or phrase: world
Please enter a word or phrase: what
Please enter a word or phrase: is
Please enter a word or phrase: up
hello
world
what
is
up
Please enter a word or phrase: to
hello
world
what
is
up
to