Could you please help me?
what is wrong with this code?
it prints only D->D->D->D (I want A->B->C->D)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
 
struct node /* Declaration of structure "node" */ 
{ 
char letter[1]; 
struct node *next ; 
}; 
 
struct node *start = NULL; 
 
main(void) 
{ 
struct node *current = start; 
struct node *new_ptr; 
 
new_ptr = (struct node *)malloc(sizeof(struct node)); 
new_ptr->next = NULL; 
strcpy(new_ptr->letter, "A"); 
start = new_ptr; 
current = new_ptr; 
 
new_ptr = (struct node *)malloc(sizeof(struct node)); 
new_ptr->next = NULL; 
strcpy(new_ptr->letter, "B"); 
current->next = new_ptr; 
current = new_ptr; 
 
new_ptr = (struct node *)malloc(sizeof(struct node)); 
new_ptr->next = NULL; 
strcpy(new_ptr->letter, "C"); 
current->next = new_ptr; 
current = new_ptr; 
 
new_ptr = (struct node *)malloc(sizeof(struct node)); 
new_ptr->next = NULL; 
strcpy(new_ptr->letter, "D"); 
current->next = new_ptr; 
current = new_ptr; 
 
current = start; 
printf("%s->\n ",new_ptr->letter);  
while(current != NULL) 
{ 
printf("%s-> ",new_ptr->letter); 
current = current->next; 
} 
system("PAUSE"); 
}

line 47 is wrong. you should use current, not new_ptr

Thank you very much.
is there a way to fill the list with A,B,C,D,...Y,Z
Without typing it?

Yes. Each letter has a numeric value. 'A' = 65, 'B' = 66, ... 'Z' = 90. So you can use a loop of some kind.

moved. This is a C program, not C++. You could use a loop something like below which adds nodes for 'A' to 'Z'. Also not the change in the structure. It isn't necessary for letter to be an array.

struct node /* Declaration of structure "node" */ 
{ 
    char letter; 
    struct node *next ; 
}; 
struct node *tail = NULL;
int letter = 'A';
for(letter = 'A'; letter < 'Z'; letter++)
{
      new_ptr = malloc(sizeof(struct node)); 
      new_ptr->next = NULL; 
      new_ptr->letter = (char)letter; 
      if( start == NULL)
      {
          start = tail = new_ptr;
      }
     else
     {
          tail->next  = new_ptr; 
          tail = new_ptr;
      }
}
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.