참고 : C로 배우는 알고리즘 / 이재규
typedef struct _dnode
{
char quiz[MAX_SIZE]; // 내용을 입력
char answer[MAX_SIZE];
int is;
int count;
struct _dnode *prev;
struct _dnode *next;
} dnode;
dnode *head, *tail; // 머리, 꼬리
dnode *temp; // 임시적인 노드
void init_dlist(void) // 이중 링크드 리스트 헤드, 꼬리 부분 초기화
{
head = (dnode*)malloc(sizeof(dnode));
tail = (dnode*)malloc(sizeof(dnode));
head->next=tail;
head->prev=head;
tail->next=tail;
tail->prev=head;
}
dnode *insert_dnode_ptr(char *quiz, char *answer, dnode *t) // 특정 t노드 앞에 내용을 새로 추가함
{
dnode *i;
if ( t == head)
return NULL;
i = (dnode*)malloc(sizeof(dnode));
strcpy(i->quiz,quiz);
strcpy(i->answer,answer);
t->prev->next=i;
i->prev = t->prev;
t->prev = i;
i->next = t;
return i;
}
int delete_dnode_ptr(dnode *p) // 특정 p노드를 리스트에서 제거함
{
if(p==head || p==tail) return 0;
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
return 1;
}
int count_dlist(dnode *p) // 헤드와 꼬리 사이의 노드 개수 세서 리턴함
{
int count=0;
while( p != tail )
{
p=p->next;
count++;
}
return count-1;
}
void delete_all(void) // 기존의 모든 내용 지우기
{
dnode *p, *s;
p=head->next;
while(p != tail)
{
s=p;
p = p->next;
free(s);
}
head->next=tail;
tail->prev=head;
}