-1

我无法在链表中追加新节点。我已经确定了问题区域,但经过大量研究并尝试了很多事情,我仍然无法解决问题。问题在于 insert_node(char,struct **) 函数和 traverse(struct *) 函数中的 for 循环,这两个函数似乎都不会终止:

    // program that stores name of the user using linkedlist

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

typedef struct LIST{
    int flag;
    char name;
    struct LIST *next;
} LISTNODE;

LISTNODE *head=NULL,*newnode=NULL;// global pointers

LISTNODE* initialize(); //initializes struct node with default values and returns a node
void insertNode(char c,LISTNODE** temp);
void traverselist(LISTNODE *temp);

int main(){
char ans,ch;
printf("\n\nEnter your name and hit enter-\n\n");
do{
printf("your name:");
fflush(stdin);
scanf("%c",&ch);

insertNode(ch,&head);

printf("\n\ninsertnode-back to main()");
printf("Want to continue?(Y?N):");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');

printf("\n\ntraverselist-leaving main()");

traverselist(head);

printf("\n\ntraverselist-back to main()");
return 0;
}

void insertNode(char c, LISTNODE **temp){

printf("\n\ninto insertnode: before initialize");

LISTNODE* temp2;

newnode=initialize();
printf("\n\nback to insertnode:after initialize");
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);

newnode->name=c;
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);

//for(;(*temp)!=NULL;temp=&(*temp)->next);
/*while((*temp)->next!=NULL){
    temp=&(*temp)->next;
    printf("\n\nIn while!");
}
*/

for(;*temp!=NULL;temp=&((*temp)->next))
    printf("\n\nIn for!") ;

//printf("\n\nout of while!");
(*temp)=newnode;

}

LISTNODE* initialize(){

static int count=0;
LISTNODE *tempnewnode;
printf("\n\nINto inintialize!");
tempnewnode=(LISTNODE*)malloc(sizeof(LISTNODE));
if(tempnewnode==NULL){
    printf("No memory available. Aborting!");
    exit(0);
}
else{
        tempnewnode->flag=0;
        tempnewnode->name='*';
        tempnewnode->next=NULL;
        if(count==0){
            head=tempnewnode;
            count++;
        }
}

return tempnewnode;
}

void traverselist(LISTNODE *temp){
printf("\n");
for(;temp!=NULL;temp=temp->next){

        printf("%c",temp->name);
}

}

请帮忙!

4

3 回答 3

0

问题出在 insert_node 函数内部,特别是循环:

for(;*temp!=NULL;temp=&((*temp)->next)) printf("\n\nIn for!");

最好不要在循环中使用参考temp,因为它会覆盖 head->next 回到自身。创建另一个临时指针。

于 2014-07-10T12:01:08.127 回答
0

问题是您的 insertNode 函数的这一部分

 for(;*temp!=NULL;temp=&((*temp)->next))
    printf("\n\nIn for!") ;

 //printf("\n\nout of while!");
     (*temp)=newnode;

在这里,您应该首先检查链接列表是否为空,如果为空,则可以创建新节点并将其地址分配给 temp。如果不是,则取决于您是否要在列表的末尾、开头或中间插入新元素,您应该遍历列表然后执行插入。例如,如果您想在开头执行插入,那么在创建新节点之后,您应该将列表的起始指针的地址分配给新创建的节点的下一个节点,并将起点移动到新节点,因为您必须跟踪的起始指针。

于 2014-07-10T12:02:07.030 回答
0

我将 insertNode(char, LISTNODE**) 更改为以下内容:

void insertNode(char c, LISTNODE *temp){
LISTNODE** temp2=&temp;
newnode=initialize();
printf("\n\nback to insertnode:after initialize");
newnode->name=c;
for(;(*temp2)!=NULL;temp2=&(*temp2)->next)
    printf("\n\nIn for!") ;
(*temp2)=newnode;
}

和函数是这样调用的:

insertNode(ch,head);

它工作得很好!

于 2014-07-11T05:41:36.617 回答