C/구조체

도서관리1

june__Park 2021. 3. 29. 17:08
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 50

typedef struct NODE{
	char title[SIZE];
	int year;
	struct NODE *link;
}NODE;

int main(void){
	NODE *list = NULL;
	NODE *prev, *p, *next;
	char buffer[SIZE];
	int year;

	while(1){

		printf("책의 제목을 입력하세요(종료하려면 엔터): ");
		gets_s(buffer,SIZE);
		if(buffer[0] == NULL){
			break;
		}

		p = (NODE*)malloc(sizeof(NODE));
		strcpy(p->title, buffer);
		printf("책의 출판년도는?: ");
		gets_s(buffer,SIZE);
		year = atoi(buffer);
		p->year = year;

		if(list == NULL){
			list = p;
		}
		else{
			prev->link = p;
		}
		p->link = NULL;
		prev = p;
	
	}
	printf("\n");

	p = list;
	while(p){
		printf("[%s,%d]->",p->title,p->year);
		p = p->link;
	}

	printf("\n");
	p = list;
	
	while(p){
		next = p->link;
		free(p);
		p = next;
	}

	return 0;
}