Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct aa {
- char title[10];
- int price;
- struct aa *next;
- };
- int main() {
- struct aa *first, *last, *temp;
- FILE *fp;
- char str[50];
- int p;
- first = last = NULL;
- fp = fopen("linked.txt", "rt");
- //파일로부터 읽어서 linked list구성
- int i = 0 ; while ( i < 10 )
- {
- if (fgets(str, 10, fp) == NULL) break;
- fscanf(fp, "%d\n", &p);
- temp = (struct aa *)malloc(sizeof(struct aa));
- strcpy(temp->title, str);
- temp->price = p;
- temp->next = NULL;
- if (first == NULL){
- first = last = temp;
- }
- else{
- last->next = temp;
- last = temp;
- }
- i++;
- }
- // linked list 의 내용을 모두 화면에 출력
- temp = first;
- while (temp != NULL) {
- printf("%s%d\n", temp->title, temp->price);
- temp = temp->next;
- }
- //사용한 메모리 모두 해제
- while (first != NULL) {
- temp = first;
- first = first->next;
- free(temp);
- }
- fclose(fp);
- return 0;
- }
Add Comment
Please, Sign In to add comment