Code

연결리스트(Linked List)

BIGFROG 2019. 7. 17. 21:35
#### linkedListFunc.h ####


#define TRUE 1
#define FALSE 0

typedef struct Student *StudentPointer;

struct Student
{
 char name[50];
 int sno; // 학번
 float gpa; //학점
 char phone[20];
 char province[30];


 StudentPointer next;
 StudentPointer prev;

};



typedef struct _LinkedList
{
 StudentPointer head;
 StudentPointer tail;
 StudentPointer cur;
 int num_of_Node;
} LinkedList;

typedef LinkedList List;

StudentPointer List_Init(List * ptr_list);
void IN(List * ptr_list); 
void PP(List * ptr_list);
void DE(List * ptr_list);
void PH(List * ptr_list);
void CT(List * ptr_list);
void PV(List * ptr_list);

int List_First(List * ptr_list);
int List_Next(List * ptr_list);
int List_Prev(List * ptr_list);

int List_Number(List * ptr_list);

void Sort_By_Name(List * ptr_list);
void Print_Node(List * ptr_list);

 

#### linkedListFunc.c ####


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedListFunc.h"
#pragma warning(disable:4996)


StudentPointer List_Init(List * ptr_list) { //초기화 : 연결리스트를 생성하고 파일을 읽어온다.

 ptr_list->head = NULL;
 ptr_list->num_of_Node = 0;

 StudentPointer newNode;
 
 FILE *fp;

 fp = fopen("mydata.txt", "r");
 if (fp == NULL) {
  perror("error : can't read the data file..\n");
  return FALSE;
 }
 else {
  
  while(!feof(fp)){
   //1. newNode 생성 - 메모리 할당,초기화
   newNode = (StudentPointer)malloc(sizeof(struct Student));
   
   fscanf(fp, "%s %d %f %s %s", newNode->name, &newNode->sno, &newNode->gpa, newNode->phone, newNode->province);
   
   
   newNode->next = ptr_list->head;
   
   if (ptr_list->head != NULL)
    ptr_list->head->prev = newNode;
  
   newNode->prev = NULL;
   ptr_list->head = newNode;
   

   (ptr_list->num_of_Node)++;
   
  }
  ptr_list->cur = ptr_list->head;


  
 
  return TRUE;
 }

 
}

void IN(List * ptr_list) { //새 학생의 삽입
 StudentPointer newNode = (StudentPointer)malloc(sizeof(struct Student));
 
 //printf("이름을 입력하세요.\n");
 scanf("%s",newNode->name);

 //printf("학번을 입력하세요.\n");
 scanf("%d", &newNode->sno);

 //printf("학점을 입력하세요.\n");
 scanf("%f", &newNode->gpa);
 
 //printf("번호를 입력하세요.\n");
 scanf("%s", newNode->phone);
 
 //printf("주소를 입력하세요.\n");
 scanf("%s",newNode->province);
 
 newNode->next = ptr_list->head;
 if (ptr_list->head != NULL)
  ptr_list->head->prev = newNode;

 newNode->prev = NULL;
 ptr_list->head = newNode;
 Sort_By_Name(ptr_list);

 (ptr_list->num_of_Node)++;
}


void PP(List * ptr_list) { //일부의 출력
 //이름 두 개 입력
 char name1[50];
 char name2[50];
 int flag = 0;

 //printf("이름1 입력 : ");
 scanf("%s", name1);
 //printf("이름2 입력 : ");
 scanf("%s", name2);


 if (List_First(ptr_list))
 {
  while (List_Next(ptr_list)) {
   if (strcmp(ptr_list->cur->name, name1) == 0 || strcmp(ptr_list->cur->name, name2) == 0) {
    flag = 1;
    break;
   }
  }
 }
 if (!flag)
  printf("Can't find the name..\n");

 

 if (strcmp(name1, name2) > 0) {
  if (List_First(ptr_list)) {
   while (strcmp(ptr_list->cur->name, name2) != 0) {

    List_Next(ptr_list);

   }
  }

  while (strcmp(ptr_list->cur->name, name1) != 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   ptr_list->cur = ptr_list->cur->next;
  }
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
 }


 else if (strcmp(name1, name2) < 0) {
  if (List_First(ptr_list)) {
   while (strcmp(ptr_list->cur->name, name1) != 0) {

    List_Next(ptr_list);

   }
  }

  while (strcmp(ptr_list->cur->name, name2) != 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   ptr_list->cur = ptr_list->cur->next;
  }
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
 }
 else
  printf("동명이인입니다.\n");

}

void DE(List * ptr_list) { //한 학생의 삭제
 char name[50];
 //printf("삭제할 이름 입력: ");
 scanf("%s", name);
 if (List_First(ptr_list)) {
  while (strcmp(ptr_list->cur->name, name) != 0) {
   List_Next(ptr_list);
   if (ptr_list->cur == NULL)
    printf("없는 이름입니다.\n");
  }
 }

 printf("삭제할 데이터는 %s입니다.\n\n", ptr_list->cur->name);
 StudentPointer rm_pos = ptr_list->cur;

 //맨 첫 데이터 삭제할경우
 if (ptr_list->cur->prev == NULL) {
  ptr_list->cur->next->prev = NULL;
  ptr_list->cur = ptr_list->cur->next;
  ptr_list->head = ptr_list->cur;
 }
 //맨 뒤 데이터 삭제할경우
 else if (ptr_list->cur->next == NULL) {
  ptr_list->cur->prev->next = NULL;
  ptr_list->cur = ptr_list->cur->prev;
 }
 
 
 else {
  ptr_list->cur->prev->next = ptr_list->cur->next;
  ptr_list->cur->next->prev = ptr_list->cur->prev;
  ptr_list->cur = ptr_list->cur->prev;
 }

 free(rm_pos);
 (ptr_list->num_of_Node)--;

}

void PH(List * ptr_list) { // 전화번호에 의한 출력
 char TwoDigit_phone[20];
 int flag = 0;
 
 //printf("전화번호의 연속되는 두 숫자 입력: ");
 scanf("%s", TwoDigit_phone);
 
 if (List_First(ptr_list))
 {
  if (strstr(ptr_list->cur->phone, TwoDigit_phone)) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   flag = 1;
  }

  while (List_Next(ptr_list)){
   if (strstr(ptr_list->cur->phone, TwoDigit_phone)) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  } 
  if (!flag) 
   printf("Can't find the digits..\n");
 }

}

void CT(List * ptr_list) { // 학점 이상
 float gpa;
 int flag = 0;

 //printf("학점 입력: ");
 scanf("%f", &gpa);

 if (List_First(ptr_list)) {

  while (List_Next(ptr_list)) {
   if (ptr_list->cur->gpa >= gpa) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  }
  if (!flag)
   printf("No one had the grade..\n");
 }

}

void PV(List * ptr_list) { // 카운트 - 지역명을 입력받아 출력
 char local[20];
 int flag = 0;

 //printf("지역명을 입력하세요: ");
 scanf("%s", local);

 if (List_First(ptr_list))
 {
  if (strcmp(ptr_list->cur->province, local) == 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   flag = 1;
  }

  while (List_Next(ptr_list)) {
   if (strcmp(ptr_list->cur->province, local) == 0) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  }
  if (!flag)
   printf("Can't find the city..\n");
 }
}
int List_First(List * ptr_list) { // 첫번째 데이터 탐색
 if (ptr_list->head == NULL)
  return FALSE;
 
 ptr_list->cur = ptr_list->head;

 
 return TRUE;
}
int List_Next(List * ptr_list) { // 다음 데이터 탐색
 if (ptr_list->cur->next == NULL)
  return FALSE;
 
 ptr_list->cur = ptr_list->cur->next;
 
 return TRUE;
}
int List_Prev(List * ptr_list) { // 이전 데이터 탐색
 if (ptr_list->cur->prev == NULL)
  return FALSE;

 ptr_list->cur = ptr_list->cur->prev;
 
 return TRUE;
}

int List_Number(List * ptr_list) { // 리스트의 노드 개수
 return ptr_list->num_of_Node;
}

void Sort_By_Name(List * ptr_list) { // 정렬 함수
 /*
 strcmp의 리턴값
 음수 - string1 < string2
 0    - string1 = string2
 양수 - string1 > string2
 */
 
 ptr_list->cur = ptr_list->head;


 while (ptr_list->cur->next != NULL) {
  if (strcmp(ptr_list->cur->name, ptr_list->cur->next->name) > 0) {
   //비교한 노드를 통째로 변경
   
   if (ptr_list->cur->next->next != NULL && ptr_list->cur->prev != NULL) { // 중간 노드를 변경할 때

    ptr_list->cur->next = ptr_list->cur->next->next;
    ptr_list->cur->next->prev->prev = ptr_list->cur->prev; 
    ptr_list->cur->next->prev->next = ptr_list->cur;
    ptr_list->cur->prev->next = ptr_list->cur->next->prev;
    ptr_list->cur->prev = ptr_list->cur->prev->next;
    ptr_list->cur->next->prev = ptr_list->cur;
   
   }
   
   else if(ptr_list->cur->next->next == NULL) //예외1 . 리스트에서 마지막 노드를 변경할 때
   {
    ptr_list->cur->next->next = ptr_list->cur;
    ptr_list->cur->prev->next = ptr_list->cur->next;
    ptr_list->cur->next->prev = ptr_list->cur->prev;
    ptr_list->cur->prev = ptr_list->cur->next;
    ptr_list->cur->next= NULL;
    break;
   }

   else if (ptr_list->cur->prev == NULL) //예외2 . 리스트에서 첫 노드를 변경할 때
   {

    ptr_list->cur->next = ptr_list->cur->next->next;
    ptr_list->cur->next->prev->prev = NULL;
    ptr_list->cur->prev = ptr_list->cur->next->prev;

    ptr_list->cur->next->prev->next = ptr_list->cur;
    
    ptr_list->cur->next->prev = ptr_list->cur;
    ptr_list->cur = ptr_list->cur->prev;
    ptr_list->head = ptr_list->cur;
   
   
   }
   
  

  }
  else if (strcmp(ptr_list->cur->name, ptr_list->cur->next->name) <= 0) {
   
   if (ptr_list->cur->next != NULL)
    ptr_list->cur = ptr_list->cur->next;
  

   }
   
  
  else {
    perror("error : from Sort_by_name");
    printf("\n");

   } 
  } 


 

}


void Print_Node(List * ptr_list) {


 ptr_list->cur = ptr_list->head;
 printf("Name==============SNO===========GPA================Phone========Region\n");
 printf("\n");
 while (ptr_list->cur->next != NULL) {
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
  ptr_list->cur = ptr_list->cur->next;
 }
 printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa,ptr_list->cur->phone,ptr_list->cur->province);
 
}

 

 

#### main.c ####


#include <stdio.h>
#include <string.h>
#include "linkedListFunc.h"
#pragma warning(disable:4996)

int main() {

 List list;
 int count = 1;
 char command[20] = "";
 int numOfNode;

 List_Init(&list);
 
 numOfNode = List_Number(&list); //리스트에 저장된 노드 개수
 while (count < numOfNode-1) {
  Sort_By_Name(&list);
  count++;
 }
 
 Print_Node(&list);
 
 while (strcmp(command,"EX") != 0) {

  printf("Type command> ");
  scanf("%s", command);

  if (strcmp(command, "IN") == 0) {
   IN(&list);
   Print_Node(&list);
  }
  else if (strcmp(command, "PP") == 0)
   PP(&list);
  else if (strcmp(command, "DE") == 0) {
   DE(&list);
   Print_Node(&list);
  }
  else if (strcmp(command, "CT") == 0)
   CT(&list);
  else if (strcmp(command, "PH") == 0)
   PH(&list);
  else if (strcmp(command, "PV") == 0)
   PV(&list);
  else if (strcmp(command, "EX") == 0)
   break;
  else
   printf("잘못된 명령입니다.\n");
  
 }
 
 printf("프로그램을 종료합니다!\n");
 
 return 0;
}

'Code' 카테고리의 다른 글

[Absolute C++] Ch6-3  (0) 2019.09.19
[Absolute C++] Ch10-2  (0) 2019.09.19
[Absolute C++] Ch14-9  (0) 2019.09.19
이진 탐색 트리(Binary Search Tree)  (0) 2019.07.17
일반트리  (0) 2019.07.02