简体   繁体   中英

LinkedList entering strings into nodes and printing list?

I am trying to use this example to implement a linkedlist in my function: http://www.sanfoundry.com/c-program-create-linked-list-display-elements/

SCROLL DOWN TO THE COMMENT:

   /** HERE. i don't want the "press 0 to exit",
    * i want to take the new message from global var. 
    * then enter into a new node of the linked list.                
    **/

My problem is that i implemented my code wrong, but don't know how to fix this. i presume i need a loop exit strategy but i am struggling on the how?

/**whenever i have a new message, 
this global variable copies that message**/

char msg[30]; 

struct node{        
   char num[30]; 
   struct node *ptr;
   };

typedef struct node NODE; //call struct NODE
NODE *head, *first, *temp = 0; //initialize head, temp value to 0 and first

void function(
    //if my message is available, then...
    if(strlen(msg)!=0) {



    while (choice){ //while true

        head  = (NODE *)malloc(sizeof(NODE));

        //i copy the new string from the global variable declared above into
        //the linked list....or that is what i am attempting to do

        strcpy(head->num,msg);
        printf("%s\n",s);

        if (first != 0){ 
                temp->ptr = head;
                temp = head;
            }

        else{
                first = temp = head;
            }
            i++;


        /** HERE. i don't want the "press 0 to exit",
        * i want to take the new message from global var. 
        * then enter into a new node of the linked list.                
        **/
        choice=0;

    }

    temp->ptr = 0;
    /*  reset temp to the beginning */
    temp = first;

    while (temp != 0){

        printf("%s\n", temp->num);
        count++;
        temp = temp -> ptr;

        }

        printf("No. of nodes in the list = %d\n", count);



    }}

Output on string1:

 string1

Output on new string2:

 string2

Basically on every new string, keep adding onto the linkedlist and printing the entire list:

 string1
 string2
 ...

appeciate any help.

void function(node*head)
{

  struct node* head = head;

   while(NULL != head) {
       printf(num);
       head = head->ptr; 
   }

}

That is something that will print all the content. To put them into this, you'll need something likes this:

struct node* head = NULL;

void putMsg()
{
   if (NULL == head)
   {
       head  = (node *)malloc(sizeof(NODE));
       temp = head;
   }
   else
   {
      temp2 = (node *)malloc(sizeof(NODE));
      temp->ptr = temp2;
      temp = temp->ptr;
   }
   char message[30];
   scanf("%s", message);
   strcpy(head->num, message);
}

Please declare all the variables. The solution is not precise but will navigate you at least.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM