Guest User

Untitled

a guest
Jul 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct aa {
  6. char title[10];
  7. int price;
  8. struct aa *next;
  9. };
  10.  
  11. int main() {
  12. struct aa *first, *last, *temp;
  13. FILE *fp;
  14. char str[50];
  15. int p;
  16.  
  17. first = last = NULL;
  18.  
  19. fp = fopen("linked.txt", "rt");
  20.  
  21. //파일로부터 읽어서 linked list구성
  22. int i = 0 ; while ( i < 10 )
  23. {
  24. if (fgets(str, 10, fp) == NULL) break;
  25. fscanf(fp, "%d\n", &p);
  26.  
  27. temp = (struct aa *)malloc(sizeof(struct aa));
  28. strcpy(temp->title, str);
  29. temp->price = p;
  30. temp->next = NULL;
  31.  
  32. if (first == NULL){
  33. first = last = temp;
  34.  
  35. }
  36. else{
  37. last->next = temp;
  38. last = temp;
  39. }
  40. i++;
  41. }
  42.  
  43. // linked list 의 내용을 모두 화면에 출력
  44. temp = first;
  45. while (temp != NULL) {
  46. printf("%s%d\n", temp->title, temp->price);
  47. temp = temp->next;
  48. }
  49.  
  50. //사용한 메모리 모두 해제
  51. while (first != NULL) {
  52. temp = first;
  53. first = first->next;
  54. free(temp);
  55. }
  56.  
  57. fclose(fp);
  58.  
  59. return 0;
  60. }
Add Comment
Please, Sign In to add comment