Guest User

Untitled

a guest
Jun 10th, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. unsigned n;
  6.  
  7. struct node
  8. {
  9. char *key;
  10. struct node *next, *prev;
  11. };
  12.  
  13. void addBack(struct node *root,struct node *end, char *newkey)
  14. {
  15. struct node *newnode;
  16. newnode = malloc(sizeof(struct node));
  17.  
  18. end->next = newnode;
  19. newnode->key = newkey;
  20. newnode->prev = end;
  21. newnode->next = root;
  22.  
  23.  
  24. }
  25.  
  26. struct node *createList(FILE *fp)
  27. {
  28. char *name = malloc(512 * sizeof(char));
  29. struct node *root = malloc(sizeof(struct node));
  30. fscanf(fp, "%s", name);
  31. root->key = name;
  32. root->next = root->prev = NULL;
  33. struct node *end = root;
  34. for(int i = 1 ; i < n; i++)
  35. {
  36. fscanf(fp , "%s", name);
  37. addBack(root, end, name);
  38. end = end->next;
  39. }
  40.  
  41. return root;
  42. }
  43.  
  44. void printList(struct node *root)
  45. {
  46. printf("%s ", root->key);
  47.  
  48. printf("\n");
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54. FILE *fp;
  55. fp = fopen("name.txt", "r");
  56. printf("Enter the number of students:\n");
  57. scanf("%u", &n);
  58. struct node *root = createList(fp);
  59. printList(root);
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment