-1

I am unable to append a new node in the linked list. I have already identified the problem area but after much research and trying many things I am still unable to resolve the issue. The problem is with the for loop in insert_node(char,struct **) function and traverse(struct *) function both of which never seem to terminate:

    // 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);
}

}

Please help!

1

3 Answers 3

0

The problem is inside the insert_node function, specifically with the loop:

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

You'd be better advised not to use the reference temp in your loop, as it overwrites the head->next back to itself. Create another temporary pointer.

0
0

I changed the insertNode(char, LISTNODE**) to the following:

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;
}

and function is called like this:

insertNode(ch,head);

It works just fine!

0

The problem is this portion of your insertNode function

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

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

Here you should first check if the link list is empty or not if it is empty then you can create the new node and assign the address of it to temp. If not then depending upon whether you want to insert the new element in the end, beginning or middle of the list you should traverse the list then perform the insertion. For example if you want to perform insertion at the beginning then after creating the new node you should assign the address of the start pointer of the list to the next of newly created node and move the start to the new node as you have to keep track of the start pointer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.