링크드리스트

언어2016. 1. 10. 22:29
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define END 0
#define TRUE 1
#define MAX 100
typedef struct linkedType
{
 char name[MAX];
 int age;
 int weight;
 struct linkedType* next;
}linked, *linkedPointer;
typedef struct nodeType
{
 linkedPointer head;
 linkedPointer tail;
 int dataCount;
}nodeType, *nodeTypePointer;
// <<<<<<< function >>>>>>>>>
nodeTypePointer createData();
void dataInput(nodeTypePointer);
void dataPrintf(nodeTypePointer);
int main(void)
{
 char choice;
 nodeTypePointer information = NULL;
 information = createData();
 while (TRUE)
 {
  printf("-------------\n");
  printf("데이터 삽입[1]\n");
  printf("데이터 출력[2]\n");
  printf("끝내기     [3]\n");
  printf("-------------\n");
  printf("입력>> ");
  scanf_s("%c", &choice);
  
  if ((choice -'0') == 1)
  {
   fflush(stdin);
   dataInput(information);
  }
  else if ((choice - '0') == 2)
  {
   dataPrintf(information);
  }
  else if ((choice - '0') == 3)
  {
   return 0;
  }
  else
  {   
   printf("잘못 입력하셨습니다.\n");
  }
  fflush(stdin);
 }
 return 0;
}
nodeTypePointer createData()
{
 nodeTypePointer H = NULL;
 H = (nodeTypePointer)malloc(sizeof(nodeType));
 if (H == NULL)
 {
  printf("동적할당 실패!!\n");
  return END;
 }
 else
 //H != NULL
 {
  memset(H, 0, sizeof(nodeType));
  return H;
 }
}
void dataInput(nodeTypePointer H)
{
 linkedPointer newData = NULL;
 newData = (linkedPointer)malloc(sizeof(linked));
 if (newData == NULL)
 {
  printf("동적할당 실패!!\n");
  return;//END
 }
 else//newData != NULL
 {
  H->dataCount++;
  char name[10];
  memset(newData, 0, sizeof(linked));
  printf(" <<<< %d번째 데이터 입력 >>>> \n", H->dataCount);
  printf("이름 입력: ");
  gets_s(newData->name, MAX);
  printf("나이 입력: ");
  scanf_s("%d", &newData->age);
  printf("몸무게 입력: ");
  scanf_s("%d", &newData->weight);
  if (H->head == NULL)
  {//데이터 최초 삽입
   H->head = newData;
   H->tail = newData;
  }
  else
  {//H->head != NULL
   H->tail->next = newData;
   H->tail = newData;
  }
  
 }
}
void dataPrintf(nodeTypePointer H)
{
 int index;
 linkedPointer node = H->head;
 for (index = 0; index < H->dataCount; index++)
 {
  printf("이름   [%s]\n", node->name);
  printf("나이   [%d]\n", node->age);
  printf("몸무게 [%d]\n", node->weight);
  node = node->next;
 }
}

 

 

'언어' 카테고리의 다른 글

버블정렬  (0) 2016.01.12
삽입정렬  (0) 2016.01.12
c언어 행렬 곱 and 더하기  (0) 2016.01.10
이거 좀 신기하다 공부해볼것  (0) 2015.12.06
짝수만 합하자  (0) 2015.10.10