#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Question Help with a linked list


    Hi guys

    I'm writing a linked list to store a strand of DNA(just a string of Chars really). Having problems adding to end of the list while in a while loop getting char's from keyboard

    I know I could read into an array and the transfer to a linked list be that in my eyes would defeat the purpose, so here's what ive got

    Code:
    NODE_TYPE * addToList(NODE_TYPE *head) {
    	 int inchar , FrstPass = 1;
    	 NODE_TYPE *temp , *lstNode;
    	 system("cls");
    	 temp = head;
    	 lstNode = head;
     	 printf("Please enter a string of dna!\n-->") ;
    	 while((inchar=getchar())!= 10) {
    	 	if (head == NULL) { //
    			head = getNode() ;
    			head->Next = NULL  ;
    			head->letter = inchar ;
    			lstNode=head;
    		 } else {
    		 	if (FrstPass) {
    		 		while((lstNode = lstNode->Next)!=NULL);//find the last node!
    		 		FrstPass=0;
    		 	}
    		 	temp=getNode();
    		 	temp->letter = inchar;
    		 	temp->Next = NULL;
    		 	lstNode->Next = temp;
    		 	lstNode = temp;
    		 }
    	}
    	printf("List has been updated!" );
    	fflush(stdin);
    	getchar();
    	return head ;
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,581
    Rep Power
    1906
    Can you be more specific what is wrong? any error messages? unexpected behaviour? what is you expectation?

    One part that looks strange is this:
    Code:
    while((lstNode = lstNode->Next)!=NULL);//find the last node!
    If you want to get the last node, you should be searching for the node which has NULL as next node.

IMN logo majestic logo threadwatch logo seochat tools logo