Guest User

Untitled

a guest
Mar 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. typedef struct record record;
  6. struct record {
  7. char name[32];
  8. float score;
  9. };
  10.  
  11. typedef struct node node;
  12. struct node {
  13. record data;
  14. node *next;
  15. };
  16.  
  17. int main() {
  18. char data[100];
  19. char name[32];
  20. float x;
  21. node *p, *head;
  22. int counter = 0;
  23.  
  24. head = 0;
  25.  
  26. while ((fgets(data, 60, stdin) != NULL) && counter <= 3) {
  27. p = (node *) malloc (sizeof(node));
  28.  
  29. sscanf(data, "%s %f", name, &x);
  30. strcpy(p->data.name,name);
  31. p->data.score = x;
  32. p->next = head;
  33. head = p;
  34.  
  35. counter++;
  36. }
  37.  
  38. printf("-----------------------n");
  39. while (p) {
  40. printf("%s %fn",p->data.name, p->data.score);
  41. p = p->next ;
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. //input
  48. bob 10
  49. shiela 5
  50. john 1
  51. elisa 10
  52.  
  53. //print input
  54. elisa 10.000000
  55. john 1.000000
  56. shiela 5.000000
  57. bob 10.000000
  58.  
  59. p->next = head;
  60. head = p;
  61.  
  62. head -> null
  63. head -> A -> null
  64. head -> B -> A -> null
  65. head -> C -> B -> A -> null
  66.  
  67. p->next = null; // new node will always be end of list
  68. if (head == NULL) // special trap for empty list
  69. head = p; // means set up head
  70. else // otherwise
  71. tail->next = p; // current tail now points to new node
  72. tail = p; // make new node the tail for next time
Add Comment
Please, Sign In to add comment