I am trying to write a program that has to create a structure that has one variable called value and one pointer to the list(making it a linked list). It need to prompt the user for 5 values and store them into the linked list, then print out the list. I need to then ask again for one more value and display the new list. I understand the concept of the linked list, but writing it in the language is a bit confusing. So far this is what I have got. its not much, but from some of the tutorials and other posts, I can seem to make the program work. any thoughts?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
define MAXCHARS 5

struct NUMBERS
{
       char name[MAXCHARS];
       struct NUMBERS *value;
       };

int main()
{
    
          
     printf("Enter five numbers, one per line.\n");

Recommended Answers

All 4 Replies

A linked list node always looks like this:

typedef struct tag_node {
  int value;
  struct tag_node *next;
} node_t;

The value itself may be any type of thing, even though in this example I used an int. Also, you could have more than one value in each node. You could also have a pointer to the previous node for a two-way linked list. (If you wanted to make a binary tree you would have two "next" pointers, one to the A branch and one to the B branch. Etc.) The last node in the list has a next value of NULL.

Playing with linked lists is a little bit tricky. You have to think about how to insert, append, and delete nodes. Each operation deserves its own function.

Oh yeah, the list "head" pointer has the following type: node_t head; Hope this helps.

Beh, I'm feeling extra generous right now so here's how to traverse the linked list.

void print_list( node_t node )
  {
  printf( "The linked list:\n" );
  while (node != NULL)
    {
    printf( "%d\n", node->value );
    node = node->next;
    }
  printf( "done.\n" );
  }

You can print your list with: print_list( head ); Have fun now.

So here is an updated program, still getting errors (line21 and 26 invalid types `int[int]' for array subscript ; line28 invalid conversion from `int' to `void*' ) I am working on this right now trying to get it to do what I want, I am getting there, but still need assistance. Thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 5


typedef struct NUMBERS
{
       int value;
       struct NUMBERS *next;
       } node_t;

int main()
{
    int i, value;
   
    char name[MAXCHARS];
    
    for (i=0; i < MAXCHARS; i++) 
      printf("Enter five numbers, one per line.\n");
      scanf("%d", &value[i]);
    
    printf("Your five numbers are:\n");
    
    for (i = 0; i < MAXCHARS; i++)
      printf(" %d\n", value[i];
    
    free(value);
    scanf("%d", &i); 
    return 0;
    }

That's because in main() you defined value as an int, not an array. But you are treating it like an array.

Forget the array. Look over your class notes and textbook and stick the scanfed number in your linked list. Don't forget to make functions to add a node to the linked list like I asked you.

Also, watch your indentation and formatting. The for statement takes one statement following. If you want to have it take more than one then you must use { and }.

Think about it and post back later.

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.