2015-11-05 56 views
2

我目前正在爲我的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; 
    } 
} 

當我嘗試運行該程序,然後輸入「你好」,「世界」,「做什麼」,「是」和「向上」來回答我的顯示功能輸出的提示單行�p並進入下一個輸入。我輸入最後一個字符串(說「你好」),我再次在終端獲得�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) 
+0

我可以得到一個示例輸出嗎? – LearningProcess

+0

不要使用'gets()'使用'fgets()'。 –

+0

我很困惑你如何編譯它。 – TriHard8

回答

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 
+0

由於這是發佈的唯一實際答案,我將接受它。然而,Kaylum確實幫助我解決了這個問題。 – firstofth300