아틴
Atin
아틴
전체 방문자
오늘
어제
  • 분류 전체보기 (460)
    • Devlopment (246)
      • 정리 글 (20)
      • MicroServices (0)
      • Reactive, Concurrenc.. (12)
      • Java (44)
      • Spring (20)
      • C,C++,Ruby,Python (52)
      • Mobile (39)
      • Web (35)
      • Tip & Info (14)
      • Unit Test (7)
    • Infra (44)
      • OS (21)
      • RDBMS (13)
      • NoSQL&Cache (5)
      • AWS (4)
    • Computer Science (11)
    • Etc (156)

블로그 메뉴

  • Home
  • Guestbook

공지사항

인기 글

태그

  • Java
  • 여행
  • Android
  • jsp
  • Ruby on Rails
  • mysql
  • Python
  • 던젼 앤 드래곤즈
  • 전라도
  • 해킨토시
  • C
  • 아이폰
  • TRPG
  • 안드로이드
  • Dungeons & Dragons
  • Linux
  • CSS
  • javascript
  • 자바
  • 정읍

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
아틴

Atin

Devlopment/C,C++,Ruby,Python

[VC++] 더블 링크드 리스트

2010. 10. 27. 13:54
반응형

/*
 * Author  : Chang-Hwan Han
 * Date   : 2010.
 * Description : double linked list
 */
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct node
{
  char* szName;
  struct node* prevNode;
  struct node* nextNode;
}Node;

Node* __insertNode(Node *pNode);
Node* __deleteNode(Node *pNode);
void __printNode(Node *pNode);
void __freeNode(Node *pNode);
void __freeNodeAll(Node *pNode);

void main()
{
  Node *head = NULL;
  char chInput;

  while(1)
  {
    printf("1. insert\n");
    printf("2. delete\n");
    printf("3. print all\n");
    printf("4. Exit\n");
    printf("input menu : ");
    scanf("%c", &chInput);
    if(chInput=='1')  head = __insertNode(head);
    else if(chInput=='2') head = __deleteNode(head);
    else if(chInput=='3') __printNode(head);
    else if(chInput=='4') break;
    else printf("input error\n");
    fflush(stdin);
  }
  __freeNodeAll(head);
}

Node* __insertNode(Node *pNode)
{
  Node *newNode, *curNode;
  char szInputName[50];
 
  printf("input node name : ");
  fflush(stdin);
  gets(szInputName);
  newNode = (Node*)malloc(sizeof(Node));
  newNode->szName = (char*)malloc((strlen(szInputName)+1) * sizeof(char));
  strcpy(newNode->szName, szInputName);
  newNode->nextNode = NULL;
  newNode->prevNode = NULL;

  if(pNode == NULL)
  {
    pNode = newNode;
    pNode->nextNode = NULL;
    pNode->prevNode = NULL;
  }
  else
  {
    curNode = pNode;
    while(curNode->nextNode != NULL)
      curNode = curNode->nextNode;
    curNode->nextNode = newNode;
    curNode->nextNode->prevNode = curNode;
 }
 return pNode;
}

Node* __deleteNode(Node *pNode)
{
  Node *curNode;
  char szInputName[50];
 
  printf("delete node name : ");
  fflush(stdin);
  gets(szInputName);
 
  curNode = pNode;
  while(curNode != NULL)
  {
    if(strcmp(curNode->szName, szInputName) == 0)
    {
      if(curNode->prevNode == NULL && curNode->nextNode == NULL)
      {
        pNode = NULL;
      }
      else if(curNode->prevNode == NULL)
      {
        pNode = pNode->nextNode;
        curNode->nextNode->prevNode = NULL;
      }
      else if(curNode->nextNode == NULL)
      {
        curNode->prevNode->nextNode = NULL;
      }
      else
      {
        curNode->nextNode->prevNode = curNode->prevNode;
        curNode->prevNode->nextNode = curNode->nextNode;   
      }
      __freeNode(curNode);
      break;
    }
    curNode = curNode->nextNode;
  }
  if(curNode == NULL) printf("node not found\n");
  return pNode;
}

void __printNode(Node *pNode)
{
  while(pNode != NULL)
  {
    printf("%s -> ", pNode->szName);
    pNode = pNode->nextNode;
  }
  printf("\n");
}

void __freeNode(Node *pNode)
{
  free(pNode->szName);
  free(pNode);
}

void __freeNodeAll(Node *pNode)
{
  Node *node;
  while(pNode != NULL)
  {
    node =pNode;
    pNode = pNode->nextNode;
    __freeNode(node);
  }
}


반응형

'Devlopment > C,C++,Ruby,Python' 카테고리의 다른 글

[VC++] Google C++ Style Guide  (0) 2011.10.14
[VC++] HEAP CORRUPTION DETECTED 에러 해결  (0) 2011.02.23
[VC++] 마우스 휠 클릭 처리  (0) 2010.11.25
[VC++] 이중 포인터에 이차원 배열 형태의 문자열 할당하기.  (0) 2010.10.26
[VC++] 구조체 패킹과 패딩 비트  (1) 2010.08.25
[VC++] 'symbol' 외부 기호(참조 위치: 'function' 함수)에서 확인하지 못했습니다.  (0) 2010.07.22
[VC++] vcvars32.bat 및 nmake.exe파일 위치  (0) 2010.06.18
[VC++] Visual Studio 6에서 include 경로 추가  (0) 2010.06.17
[VC++] CString -> char 변환  (0) 2010.04.21
[VC++] Debug모드와 Release모드  (0) 2010.04.15
    'Devlopment/C,C++,Ruby,Python' 카테고리의 다른 글
    • [VC++] HEAP CORRUPTION DETECTED 에러 해결
    • [VC++] 마우스 휠 클릭 처리
    • [VC++] 이중 포인터에 이차원 배열 형태의 문자열 할당하기.
    • [VC++] 구조체 패킹과 패딩 비트
    아틴
    아틴

    티스토리툴바