//current - 리스트를 처음부터 끝까지 차례대로 검색할때 현재 위치.
//previous - 바로 전 위치.
//Next - 다음 위치.
#include < stdio.h >
#include < stdlib.h >
typedef struct _NODE{
char ch;
struct _NODE *next;
}NODE;
NODE *head;
void init(void);
void build(void);
void print(void);
void reverse(void);
int main(void){
init();
build();
print();
reverse();
print();
return 0;
}
void init(void){
head = (NODE*)malloc(sizeof(NODE));
head->next = NULL;
}
void build(void){
NODE *tmp1, *tmp2, *tmp3, *tmp4;
tmp1 = (NODE*)malloc(sizeof(NODE));
tmp2 = (NODE*)malloc(sizeof(NODE));
tmp3 = (NODE*)malloc(sizeof(NODE));
tmp4 = (NODE*)malloc(sizeof(NODE));
head->next = tmp1;
tmp1->next = tmp2;
tmp2->next = tmp3;
tmp3->next = tmp4;
tmp4->next = NULL;
tmp1->ch = 'A';
tmp2->ch = 'B';
tmp3->ch = 'C';
tmp4->ch = 'D';
}
void print(void){
NODE *index;
index = head->next;
while(index !=NULL){
printf("%c ", index->ch);
index = index ->next;
}
printf("\n");
}
void reverse(void){
NODE *index, *tmp, *tmp2;
index = head->next->next;
tmp = head->next;
tmp2 = head;
while(index->next != NULL){ //리버스 작업
tmp->next = tmp2;
tmp2 = tmp;
tmp = index;
index = index->next;
}
//마지막 작업
tmp->next = tmp2;
index->next = tmp;
head->next->next = NULL;
head->next = index;
}