2016-11-26 131 views
0

我在这里创建了一个TurboC++ C程序。这个简单的程序目标是创建一个链表,在列表的末尾插入每个元素,然后打印节点的值。(编辑该程序有所改变,使我更容易理解,但仍然是问题节点不是priniting存在) 的问题是,一些节点元素不是从错误的元素打印节点元素不打印在单链表列表中

#include<stdio.h> 
#include<stdlib.h> 
#include<malloc.h> 
#include<ctype.h> 
#include<conio.h> 
struct node 
{ 
    int data; 
    struct node *link; 
}; 
typedef struct node ND; 
void main() 
{ 
    ND *start,*current,*temp; 
    start=NULL; 
    do 
    { 
    temp=(ND*)malloc(sizeof(ND)); 
    printf("\n Enter Node Value"); 
    scanf(" %d",&temp->data); 
    temp->link=NULL; 
    if(start==NULL) 
    { 
     start=current=temp; 
    } 
    else 
    { 
     current->link=temp; 
     current=temp; 
    } 
    fflush(stdin); 
    printf("\nDo You Want TO Continue?(Y/N)"); 
    }while(toupper(getchar())!='N'); 
    current=start; 
    printf("\nThe Elements OF Linked List ARE:"); 
    while(current!=NULL) 
    { 
    printf(" %d",current->data); 
    current=current->link; 
    } 
} 
+0

即使[多十年之久的Turbo C++控制台IDE](http://stackoverflow.com/questions/1961828/why-not-to-use-turbo-c)有一个集成的调试器。你应该用它来追踪问题出在哪里。不相关的是,你在包含列表中缺少'ctype.h'和'stdlib.h',这两者都应该分别包含在'toupper'和'malloc'支持中。 – WhozCraig

+0

寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。请参阅:[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。简而言之,这不是“免费调试我的代码”服务。 –

+0

我写了上面最短的代码,我已经说明了我的问题,列表中的节点元素不打印... – Mihir

回答

2

您打印清单。你应该从头开始。 like:

temp1 = head; 
while(temp1!=NULL) 
{ 
    printf(" %d",temp1->data); 
    temp1=temp1->link; 
} 

顺便说一下,你的head元素将始终为NULL。 这里是将元素添加到列表中正确的方法:

if (head == NULL) 
{ 
    head = temp; 
} 
else 
{ 
    temp1 = head; 
    while (temp1->link != NULL) 
    { 
     temp1 = temp1->link; 
    } 
    temp1->link = temp; 
} 
+2

'scanf(“%d”,temp-> data)'也不正确,并调用未定义的行为。 Prolly想提到这一点。 – WhozCraig

+0

是啊,我已经纠正了这一点,但它仍然不打印,我还添加了文件stdlib.h和ctype.h中 – Mihir

+0

请更新你的代码,你现在有什么:) –

0

该程序现在正常工作我已经做了一些改动,使这个方案更容易understand.Here的代码:

#include<stdio.h> 
#include<conio.h> 
struct node 
{ 
    int data; 
    struct node *link; 
}; 
typedef struct node ND; 
void main() 
{ 
    ND *head,*tail,*temp1; 
    int n; 
    char ch; 
    printf("\nEnter Data?(y/n)\n"); 
    scanf(" %c",&ch); 
    fflush(stdin); 
    if(ch=='y' || ch=='Y') 
    { 
    tail=(ND*)malloc(sizeof(ND)); 
    printf("\n Enter Node Value"); 
    scanf(" %d",&n); 
    tail->data=n; 
    tail->link=NULL; 
    head=tail; 
    printf("\nDo You Want TO Continue?(Y/N)"); 
    scanf(" %c",&ch); 
    fflush(stdin); 
    } 
    while(ch=='y' || ch=='Y') 
    { 
    printf("\n Enter Node Value"); 
    scanf(" %d",&n); 
    tail->link=(ND*)malloc(sizeof(ND)); 
    tail->link->data=n; 
    tail->link->link=NULL; 
    tail=tail->link; 
    printf("\nEnter More Data?(y/n)\n"); 
    scanf(" %c",&ch); 
    fflush(stdin); 
    } 
    printf("\nElements Of Linked List Are:"); 
    temp1=head; 
    while(temp1!=NULL) 
    { 
    printf(" %d",temp1->data); 
    temp1=temp1->link; 
    } 
    printf("\n"); 
    fflush(stdout); 
    getch(); 
}