-
문제05-2 더미 노드 기반 양방향 연결 리스트Data Structure/윤성우의 열혈 자료구조 2019. 9. 20. 19:35This 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
#include <stdio.h> #include <stdlib.h> #pragma warning(disable:4996) typedef struct _node { int data; struct _node * next; struct _node * prev; }Node; typedef struct _link { Node * head; Node * tail; Node * cur; int numOfdata; }List; void ListInit(List *plist) { plist->head = (Node*)malloc(sizeof(Node)); plist->tail = (Node*)malloc(sizeof(Node)); plist->head->next = plist->tail; plist->head->prev = NULL; plist->tail->prev = plist->head; plist->tail->next = NULL; plist->numOfdata = 0; } void LInsert(List *plist, int data) { Node * newnode = (Node*)malloc(sizeof(Node)); newnode->data = data; plist->tail->prev->next = newnode; newnode->prev = plist->tail->prev; newnode->next = plist->tail; plist->tail->prev = newnode; (plist->numOfdata)++; } int LFirst(List *plist, int* data) { if (plist->head->next == plist->tail) return 0; plist->cur = plist->head->next; *data = plist->cur->data; return 1; } int LNext(List *plist, int *data) { if (plist->cur->next == plist->tail) return 0; plist->cur = plist->cur->next; *data = plist->cur->data; return 1; } int LRemove(List *plist) { Node * rnode = plist->cur; int rdata = rnode->data; plist->cur->prev->next = plist->cur->next; plist->cur->next->prev = plist->cur->prev; plist->cur = plist->cur->prev; free(rnode); (plist->numOfdata)--; return rdata; } int main() { List list; int data; ListInit(&list); //저장 LInsert(&list, 1); LInsert(&list, 2); LInsert(&list, 3); LInsert(&list, 4); //조회 if (LFirst(&list, &data)) { printf("%d ", data); while (LNext(&list, &data)) printf("%d ", data); printf("\n"); } //2의 배수 삭제 if (LFirst(&list, &data)) { if (data % 2 == 0) LRemove(&list); while (LNext(&list, &data)) { if (data % 2 == 0) LRemove(&list); } } //조회 if (LFirst(&list, &data)) { printf("%d ", data); while (LNext(&list, &data)) printf("%d ", data); printf("\n"); } return 0; } 'Data Structure > 윤성우의 열혈 자료구조' 카테고리의 다른 글
문제04-4 정렬의 기준으로 활용되는 함수의 정의 (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