3.1. 예제1 ¶
#include <stdio.h>
#include <malloc.h>
typedef struct _node{
int value;
struct _node * next;
} node;
void freeAll(node*);
int main(void) {
node* head;
head = (node*)malloc(sizeof(node));
node* temp = head;
for (int i = 0; i < 50; i++) {
temp->next = (node*)malloc(sizeof node);
temp->value = i;
temp = temp->next;
}
temp->next = NULL;
temp->value = 50;
freeAll(head);
return 0;
}
void freeAll(node* hd) {
node* t;
while (hd != NULL) {
t = hd->next;
printf("%d ", hd->value);
free(hd);
hd = t;
}
}
5.3. 박인서 ¶
- 연결 리스트
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node * next;
}node;
void freeall(node * hd)
{
node * t=hd->next;
while(hd!=NULL)
{
t=hd->next;
printf("%d\n",hd->value);
free(hd);
hd=t;
}
}
int main()
{
node * head;
node * temp;
int i;
head = (node *)malloc(sizeof(node));
head->value=0;
head->next=(node *)malloc(sizeof(node));
temp=head;
for(i=0;i<50;i++)
{
temp->next=(node *)malloc(sizeof(node));
temp->value=i;
temp=temp->next;
}
temp->next=NULL;
temp->value=50;
freeall(head);
return 0;
}










