so im tryna save input from stdin to a node. but every time i run the loop it resets whats in my node. idk if that makes sense.. heres my code..

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

struct queue_node
   {
      char word[11];  
	  struct queue_node *next;
   } node;

struct queue_node *make_list(void);
struct queue_node *add_node(void);
void accept_input (struct queue_node *, char[]);
int menu();



int main(void)
{
   int choice = 0;
   struct queue_node *start;   //pointer to start of list
  
 
   
   do   
   {
      choice = menu();
      if(choice == 1)
	  {
	     start = make_list();
		 continue;
	  }
	  else 
	  {
	     printf("pee");
		 continue;
	  }
   } while ( choice != 5 );

  return 0;  
}
int menu()
{
   int choice;
   printf("Enter a Number Based on what you would like to do:\n");
   
      printf(" 1. Add a Word \n 2. Delete a Word \n 3. Display the Data in Queue Order \n 4. Search the Queue for a Word \n 5. Quit \n");
      scanf("%i", &choice);
  

   return choice;  
}
struct queue_node *make_list(void)
{
  char buffer[11];
  struct queue_node * start = NULL, * current, * new;
  int j;
  for (j=0;j<2;j++)
  {
  new = add_node();
  if (start==NULL)
     {
	    start = current = new;
	 }
  else 
     {
	    current->next = new;
		current = current->next;
	 }
  accept_input (current, buffer );
  }
  printf("%s<current>", current->word);
  printf("%s<start>", start->word);
  return start;
}
void accept_input (struct queue_node * local_node, char str[])
{
   int i;
   char input[11];
   printf ("enter something\n");
 
      scanf ("%s",&input);
      strcpy(str, input);
      sscanf(str,"%s",local_node->word);

   return;

}

struct queue_node * add_node (void)
{
   struct queue_node * new_ptr;   // local pointer
   new_ptr = (struct queue_node *) malloc (sizeof (struct queue_node));
   new_ptr->next = NULL;
   return new_ptr;
}

line 56 is wrong because the linked list is initialied to NULL every time that function is entered. What you have to do is pass variable start from main() as a parameter into make_list(), like this (Note the double asterisk, or double pointer).

struct queue_node *make_list(struct queue_node** start)
{
  char buffer[11];
  struct queue_node * current, * new;
  int j;
  for (j=0;j<2;j++)
  {
     new = add_node();
     if (*start==NULL)
     {
        *start = new;
     }
     else 
     {
         // locate end of linked list
         current = *start;
         while( current->next != NULL)
            current = current->next;
         current->next = new;
     }
     accept_input (new, buffer );
  }
  printf("%s<current>", current->word);
  printf("%s<start>", start->word);
  return start;
}
Comments
:)

omg thank you so much. i was looking for hours.. always a simple fix:)

ok so im trying to display the contents of my linked list... but its only displaying the first word. ive kinda narrowed it down with gdb to my function not being able to access anything but start.. heres my code

void display_list(struct queue_node *q)
{

   while ( q!=NULL)
   {
      printf("<%s>\n", q->word);
   
	  q=q->next;

	  
   }
   return;
}

its passing the start pointer from main

This article has been dead for over six months. Start a new discussion instead.