-
문제04-4 정렬의 기준으로 활용되는 함수의 정의Data Structure/윤성우의 열혈 자료구조 2019. 9. 20. 18:39
다음의 정렬기준을 연결 리스트에 등록하여 그 결과를 확인하자
- x 좌표의 값을 기준으로 오름차순 정렬
- x 좌표의 값이 같은 경우 y좌표를 대상으로 오름차순 정렬
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters// DLinkedList.c #include <stdio.h> #include <stdlib.h> #include "DLinkedList.h" void ListInit(List * plist) { plist->head = (Node*)malloc(sizeof(Node)); plist->head->next = NULL; plist->comp = NULL; plist->numOfData = 0; } void FInsert(List * plist, LData data) { Node * newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = plist->head->next; plist->head->next = newNode; (plist->numOfData)++; } void SInsert(List * plist, LData data) { Node * newNode = (Node*)malloc(sizeof(Node)); Node * pred = plist->head; newNode->data = data; while(pred->next != NULL && plist->comp(data, pred->next->data) != 0) { pred = pred->next; } newNode->next = pred->next; pred->next = newNode; (plist->numOfData)++; } void LInsert(List * plist, LData data) { if(plist->comp == NULL) FInsert(plist, data); else SInsert(plist, data); } int LFirst(List * plist, LData * pdata) { if(plist->head->next == NULL) return FALSE; plist->before = plist->head; plist->cur = plist->head->next; *pdata = plist->cur->data; return TRUE; } int LNext(List * plist, LData * pdata) { if(plist->cur->next == NULL) return FALSE; plist->before = plist->cur; plist->cur = plist->cur->next; *pdata = plist->cur->data; return TRUE; } LData LRemove(List * plist) { Node * rpos = plist->cur; LData rdata = rpos->data; plist->before->next = plist->cur->next; plist->cur = plist->before; free(rpos); (plist->numOfData)--; return rdata; } int LCount(List * plist) { return plist->numOfData; } void SetSortRule(List * plist, int (*comp)(LData d1, LData d2)) { plist->comp = comp; } // DLinkedList.h #ifndef __D_LINKED_LIST_H__ #define __D_LINKED_LIST_H__ #include "Point.h" #define TRUE 1 #define FALSE 0 typedef Point * LData; typedef struct _node { LData data; struct _node * next; } Node; typedef struct _linkedList { Node * head; Node * cur; Node * before; int numOfData; int (*comp)(LData d1, LData d2); } LinkedList; typedef LinkedList List; void ListInit(List * plist); void LInsert(List * plist, LData data); int LFirst(List * plist, LData * pdata); int LNext(List * plist, LData * pdata); LData LRemove(List * plist); int LCount(List * plist); void SetSortRule(List * plist, int (*comp)(LData d1, LData d2)); #endif //Point.c #include <stdio.h> #include "Point.h" void SetPointPos(Point * ppos, int xpos, int ypos) { ppos->xpos = xpos; ppos->ypos = ypos; } void ShowPointPos(Point * ppos) { printf("[%d, %d] \n", ppos->xpos, ppos->ypos); } int PointComp(Point * pos1, Point * pos2) { if(pos1->xpos == pos2->xpos && pos1->ypos == pos2->ypos) return 0; else if(pos1->xpos == pos2->xpos) return 1; else if(pos1->ypos == pos2->ypos) return 2; else return -1; } //Point.h #ifndef __POINT_H__ #define __POINT_H__ typedef struct _point { int xpos; int ypos; } Point; // Point 변수의 xpos, ypos 값 설정 void SetPointPos(Point * ppos, int xpos, int ypos); // Point 변수의 xpos, ypos 정보 출력 void ShowPointPos(Point * ppos); // 두 Point 변수의 비교 int PointComp(Point * pos1, Point * pos2); #endif //PointListSortMain.c #include <stdio.h> #include <stdlib.h> #include "DLinkedList.h" #include "Point.h" int WhoIsPrecede(Point * d1, Point * d2) { if(d1->xpos < d2->xpos) { return 0; } else if(d1->xpos == d2->xpos) { if(d1->ypos < d2->ypos) return 0; else return 1; } else return 1; } int main(void) { List list; Point comp; Point * pPoint; ListInit(&list); SetSortRule(&list, WhoIsPrecede); // 정렬기준을 등록! pPoint = (Point*)malloc(sizeof(Point)); SetPointPos(pPoint, 3, 2); LInsert(&list, pPoint); pPoint = (Point*)malloc(sizeof(Point)); SetPointPos(pPoint, 3, 1); LInsert(&list, pPoint); pPoint = (Point*)malloc(sizeof(Point)); SetPointPos(pPoint, 2, 2); LInsert(&list, pPoint); pPoint = (Point*)malloc(sizeof(Point)); SetPointPos(pPoint, 2, 1); LInsert(&list, pPoint); printf("현재 데이터의 수: %d \n", LCount(&list)); if(LFirst(&list, &pPoint)) { ShowPointPos(pPoint); while(LNext(&list, &pPoint)) ShowPointPos(pPoint); } printf("\n"); // xpos가 2인 모든 데이터 삭제 //////// comp.xpos = 2; comp.ypos = 0; if(LFirst(&list, &pPoint)) { if(PointComp(pPoint, &comp) == 1) { pPoint = LRemove(&list); free(pPoint); } while(LNext(&list, &pPoint)) { if(PointComp(pPoint, &comp) == 1) { pPoint = LRemove(&list); free(pPoint); } } } // 삭제 후 저장된 데이터 출력 //////// printf("현재 데이터의 수: %d \n", LCount(&list)); if(LFirst(&list, &pPoint)) { ShowPointPos(pPoint); while(LNext(&list, &pPoint)) ShowPointPos(pPoint); } printf("\n"); return 0; } 'Data Structure > 윤성우의 열혈 자료구조' 카테고리의 다른 글
문제05-2 더미 노드 기반 양방향 연결 리스트 (31) 2019.09.20 문제 04-2 더미 노드 (31) 2019.09.20 문제 04-1 연결 리스트 관련 코드에 익숙해지기 (0) 2019.09.05 문제 03 -1 리스트 라이브러리의 활용 (0) 2019.09.04 문제 11-1 이진 탐색 트리의 조건 (0) 2019.08.16