#include <stdio.h>
#include <stdlib.h>
struct Node
{
int n;
struct Node *next;
};
void showMenu();
void printNodes(struct Node* p);
void freeNodes(struct Node* p);
void main(){
// 메인함수에서 사용할 기본 변수 선언
int flag = 1;
int input;
struct Node *head;//연결 리스트의 첫 원소를 저장할 포인터 변수 head
struct Node *last;//마지막 원소의 주소를 저장할 last 변수
struct Node *node;//추가될 노드의 주소를 임시로 저장할 포인터 변수 node
//아직 추가된 노드가 없기 때문에, Head 와 last 의 초기값을 NULL 로 설정
head = NULL;
last = NULL;
//사용자 입력이 0 이 될 때까지, 반복 수행
while(flag){
//메뉴를 출력 후, 사용자의 입력을 input 변수에 저장
showMenu();
//사용자 입력이 0이면, flag 값을 0으로 바꾸고, while 반복문을 종료
if(input ==0){
flag = 0;
}else{//사용자 입력이 0이 아니면, 노드를 연결 리스트에 추가
//Node 구조체를 동적 할당
node
= (struct Node
*)malloc(sizeof(struct Node
));
//생성된 Node 구조체에 값을 설정
node->n = input;
node->next = NULL;
//연결 리스트가 비어있을 경우, head에 생성된 node 의 주소를 저장
if(head == NULL){
head = node;
}else{ //연결 리스트가 비어있지 않을 경우, 마지막 노드의 next에 생성된 node의 주소를 저장
last->next=node;
}
//연결 리스트에 추가된 node의 주소를 last에 저장
last = node;
}
}
//연결 리스트에 저장된 값을 화면에 출력하고, 사용된 동적 메모리를 모두 해제
printNodes(head);
freeNodes(head);
}
void showMenu(){
}
void printNodes(struct Node* p){
//p가 NULL 아닌 경우 = 마지막 노드가 아닌 경우
while(p!=NULL){
//노드에 저장된 값을 출력하고, 다음 노드로 이동
p = p->next;
}
}
void freeNodes(struct Node* p){
struct Node *temp;//다음 노드의 주소를 임시 저장할 temp 변수
while(p!=NULL){//연결 리스트의 끝 까지 반복
temp = p;//지울 노드를 임시변수에 저장
p = p->next;//다음 노드로 이동
free(temp
);//임시변수에 저장된 이전 노드를 해제 }
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0c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