#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct DNode
{
char data[10];
struct DNode* llink;
struct DNode* rlink;
}Dnode;
typedef struct
{
Dnode* head;
}dnode_h;
dnode_h* createDLList_h(void);
void addLastNode(dnode_h*, char*);
void deleteLastNode(dnode_h*);
void addmiddleNode(dnode_h*, char*);
void printList(dnode_h*);
dnode_h* createDLList_h(void)
{
dnode_h* L;
L=(dnode_h*)malloc(sizeof(dnode_h));
L->head=NULL;
return L;
}
void addLastNode(dnode_h* L, char *x)
{
Dnode* newNode;
Dnode* p;
newNode=(Dnode*)malloc(sizeof(Dnode));
strcpy(newNode->data, x);
if(L->head==NULL){
L->head=newNode;
newNode->rlink=NULL;
return;
}
p=L->head;
while(p->rlink!=NULL) p=p->rlink;
//p는 마지막 노드
newNode->rlink=NULL;
p->rlink=newNode;
newNode->llink=p;
}
void addmiddleNode(dnode_h*, char*)
{
Dnode* newNode;
Dnode* p;
}
void deleteLastNode(dnode_h* L)
{
Dnode* pre;
Dnode* cur;
if(L->head==NULL) return;
if(L->head->rlink==NULL){
free(L->head);
L->head=NULL;
return;
}
else {
pre=L->head;
cur=L->head->rlink;
while(cur->rlink!=NULL){
pre=cur;
cur=cur->rlink;
}
free(cur);
pre->rlink=NULL;
}
}
void printList(dnode_h* L)
{
Dnode* p;
printf("L=(");
p=L->head;
while(p!=NULL){
printf("%s", p->data);
p=p->rlink;
if(p!=NULL) printf(", ");
}
printf(") \n");
}
int main()
{
dnode_h* L;
L=createDLList_h();
printf("(1) 공백 리스트 생성하기 \n");
printList(L); getchar();
printf("(2) 리스트에 3개의 노드 추가하기 \n");
addLastNode(L, "월");
addLastNode(L, "수");
addLastNode(L, "금");
printList(L); getchar();
printf("(3) 리스트 마지막 노드에 1개의 노드 추가하기 \n");
addLastNode(L, "일");
printList(L); getchar();
printf("(4) 리스트 중간 노드에 1개의 노드 추가하기 \n");
addLastNode(L, "토");
printList(L); getchar();
printf("(5) 마지막 노드 삭제하기 \n");
deleteLastNode(L);
printList(L); getchar();
getchar();
return 0;
}