YorKnEz

Untitled

May 13th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct Student {
  7. char *name;
  8. int grade;
  9. Student *next;
  10. } Student;
  11.  
  12. void add (Student *&list, char *name, int grade) {
  13. Student *current;
  14. current = list;
  15.  
  16. while (current->next != NULL) {
  17. current = current->next;
  18. }
  19.  
  20. Student *new_student;
  21. new_student = new Student;
  22. strcpy(new_student->name, name);
  23. new_student->grade = grade;
  24. new_student->next = NULL;
  25.  
  26. current->next = new_student;
  27. }
  28.  
  29. void display (Student *list) {
  30. Student *current;
  31. current = list;
  32.  
  33. while (current != NULL) {
  34. printf("%s %d\n", current->name, current->grade);
  35. current = current->next;
  36. }
  37. }
  38.  
  39. int main(int argc, char* argv[]) {
  40. Student *list;
  41. list = new Student;
  42.  
  43. int n;
  44. char name[21];
  45. float grade;
  46.  
  47. scanf("%d", &n);
  48.  
  49. scanf("%s %f", &list->name, &list->grade);
  50. list->next = NULL;
  51.  
  52. for (int i = 1; i < n; i++) {
  53. scanf("%s %f", &name, &grade);
  54. add(list, name, grade);
  55. }
  56.  
  57. display(list);
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment