#Linked List

18 messages · Page 1 of 1 (latest)

mystic bay
#

I want to add a new value to my linked list, through the console but the last name wont print.

tiny breachBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mystic bay
#

!f

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

typedef struct {
  char first_name[20];
  char last_name[20];
  int math_grade;
  int german_grade;
  int english_grade;
  struct pupils* next;
} pupils;

pupils* pupil;  // change while the programm is running
pupils* head;   // doesnt change --> is the head of the list

void print() {
  pupil = head;
  while (pupil != NULL) {
    printf("%s %s %d %d %d\n", pupil->first_name, pupil->last_name,
           pupil->math_grade, pupil->german_grade, pupil->english_grade);
    pupil = pupil->next;
  }
  printf("---------\n");
}

void create_head(char first_name_[20],
                 char last_name_[20],
                 int mathe_grade_,
                 int german_grade_,
                 int english_grade_) {
  pupils* temp = malloc(sizeof *temp);
  strcpy(temp->first_name, first_name_);
  strcpy(temp->last_name, last_name_);
  temp->math_grade = mathe_grade_;
  temp->german_grade = german_grade_;
  temp->english_grade = english_grade_;

  temp->next = NULL;

  pupil = temp;
  head = temp;
}
void add_pupil(char new_first_name[20],
               char new_last_name[20],
               int new_mathe_grade,
               int new_german_grade,
               int new_english_grade) {
  pupils* new = malloc(sizeof(*new));
  strcpy(new->first_name, new_first_name);
  strcpy(new->last_name, new_last_name);
  new->math_grade = new_mathe_grade;
  new->german_grade = new_german_grade;
  new->english_grade = new_english_grade;
  new->next = NULL;

  pupil = head;

  while (pupil != NULL) {
    if (pupil->next == NULL) {
      pupil->next = new;
      break;
    } else {
      pupil = pupil->next;
    }
    pupil->next = new;
  }
}
Monke
mystic bay
#

!f

tiny breachBOT
#
int main() {
  int selection = 0, mathe_grade, german_grade, english_grade;
  char first_name[20];
  char last_name[20];
  char input;

  create_head("Ralph", "Reichts", 1, 3, 5);

  printf("What do you want to do?\n");
  printf("0 - Close program\n");
  printf("1 - Add a new pupil\n");
  scanf("%d", &selection);

  switch (selection) {
    case 0:
      return 0;
    case 1:
      printf("Insert the first name of the pupil.\n");
      scanf("%s", &first_name);
      printf("Insert the last name of the pupil\n");
      scanf("%s", &last_name);

      printf(
          "Do you got a math, german and english grade for the pupil?(y/n)\n");
      scanf("%s", &input);
      if (input == 'y' || input == 'Y') {
        printf("Plese insert the math grade of the pupil.\n");
        scanf("%d", &mathe_grade);
        printf("Plese insert the german grade of the pupil.\n");
        scanf("%d", &german_grade);
        printf("Plese insert the english grade of the pupil.\n");
        scanf("%d", &english_grade);

        add_pupil(first_name, last_name, mathe_grade, german_grade,
                  english_grade);
      } else if (input == 'n' || input == 'N') {
        add_pupil(first_name, last_name, 0, 0, 0);
      } else {
        printf(
            "Your input was not accepted by the System, the programm will shut "
            "down.\n");
        return 0;
      }
  }
  print();
  free(pupil);
  return 0;
}
Monke
visual current
#

The while loop in add_pupil seems wrong

#

I don't think you should have c pupil->next = new
outside of the if statement

#

It traverses the list and always sets the next pointer to be the new node - which you only want to do when you've reached the end of the list

mystic bay
#

thank you, but the problem is still there

visual current
#

Also with the scanf for the first and last name, it shoudn't be scanf("%s", &first_name) just scanf("%s", first_name)

#

same for last_name

mystic bay
#

okay, but the last name still wont print

visual current
#

Oh and it looks like your typedef is wrong

#

In your struct you use struct pupils, but you never declare this as your struct is anonymous

#

Instead I think you want something like:

typedef struct pupils pupils;
struct pupils {
  char first_name[20];
  char last_name[20];
  int math_grade;
  int german_grade;
  int english_grade;
  pupils* next;
};
tiny breachBOT
#

@mystic bay Has your question been resolved? If so, type !solved :)

hallow bear
#

I agree with JamesZ suggestions. If you use &first_name or &last_name, make it &first_name[0] and &last_name[0] otherwise just use the character array name first_name, last_name as suggested by JameZ.

For your create_head function, I would change like this:

void create_head(char *first_name_,
                 char *last_name_,

WIth these changes:

$ ./a.out
What do you want to do?
0 - Close program
1 - Add a new pupil
1
Insert the first name of the pupil.
Joe
Insert the last name of the pupil
Blow
Do you got a math, german and english grade for the pupil?(y/n)
y
Plese insert the math grade of the pupil.
4
Plese insert the german grade of the pupil.
3
Plese insert the english grade of the pupil.
2
Ralph Reichts 1 3 5
Joe Blow 4 3 2