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

Recommended Answers

All 5 Replies

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;
}
commented: :) +1

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

on another note.. how do you search through nodes?

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

show us how you called make_list() from main().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.