'linked list'에 해당되는 글 1건

  1. 2006.10.18 single linked list 역순으로 정렬

//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;
}

Posted by 창신다이

BLOG main image
오랫동안 꿈을 그리는 사람은 마침내 그 꿈을 닮아 간다. -앙드레 말로- by 창신다이

공지사항

카테고리

분류 전체보기 (248)
공장이야기 (115)
Education (30)
회사이야기 (19)
일상 (73)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :