//common.h #pragma once //헤더 파일을 한 번만 포함해서 컴파일 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <malloc.h> #include <memory.h> #include <time.h> #pragma warning(disable:4996) //4996컴파일 경고 메시지 출력 해제
//Book.h #pragma once #include "common.h" #define MAX_TIT_LEN 200 #define MAX_AUT_LEN 20 typedef struct _Book Book; struct _Book { char title[MAX_TIT_LEN+1]; char author[MAX_AUT_LEN+1]; int num; }; Book *New_Book(const char *title,const char *author,int num); void Delete_Book(Book *book); void Book_View(Book *book); int Book_CompareTitle(Book *book,const char *title); int Book_CompareAuthor(Book *book,const char *author); int Book_CompareNum(Book *book,int num);
//Book.c #include "Book.h" void Book_Book(Book *book,const char *title,const char *author,int num); Book *New_Book(const char *title,const char *author,int num) { Book *book = 0; book = (Book *)malloc(sizeof(Book)); Book_Book(book,title,author,num); return book; } void Book_Book(Book *book,const char *title,const char *author,int num) { memset(book,0,sizeof(Book)); strncpy(book->title,title,MAX_TIT_LEN); strncpy(book->author,author,MAX_AUT_LEN); book->num = num; } void Delete_Book(Book *book) { free(book); } void Book_View(Book *book) { printf("<%010d>:<%s>\n",book->num,book->title); printf("\t 저자:%s\n",book->author); } int Book_CompareTitle(Book *book,const char *title) { return strcmp(book->title,title); } int Book_CompareAuthor(Book *book,const char *author) { return strcmp(book->author,author); } int Book_CompareNum(Book *book,int num) { return book->num-num; }
//LinkedList.h #pragma once typedef void * Element; typedef struct _Node Node; typedef Node *Link; struct _Node { Link next; Link prev; Element data; }; typedef struct _LinkedList LinkedList; struct _LinkedList { Link head; Link tail; int usage; }; LinkedList *New_LinkedList(); void Delete_LinkedList(LinkedList *linkedlist); void LinkedList_PushBack(LinkedList *linkedlist,Element data); void LinkedList_Insert(LinkedList *linkedlist,Link pos,Element data); Link LinkedList_Begin(LinkedList *linkedlist); Link LinkedList_End(LinkedList *linkedlist); void LinkedList_Erase(LinkedList *linkedlist,Link pos);
//LinkedList.c #include "LinkedList.h" #include <malloc.h> #include <memory.h> Link New_Node(Element data) { Link link = (Link)malloc(sizeof(Node)); link->data = data; link->next = link->prev = 0; return link; } void HangNode(Link link,Link pos) { link->prev = pos->prev; link->next = pos; pos->prev->next = link; pos->prev = link; } void DisconnectNode(Link pos) { pos->prev->next = pos->next; pos->next->prev = pos->prev; } LinkedList *New_LinkedList() { LinkedList *linkedlist = 0; linkedlist = (LinkedList *)malloc(sizeof(LinkedList)); linkedlist->head = New_Node(0); linkedlist->tail = New_Node(0); linkedlist->head->next = linkedlist->tail; linkedlist->tail->prev = linkedlist->head; linkedlist->usage = 0; return linkedlist; } void Delete_LinkedList(LinkedList *linkedlist) { Link seek = linkedlist->head; while( seek != linkedlist->tail) { seek = seek->next; free(seek->prev); } free(linkedlist->tail); free(linkedlist); } void LinkedList_PushBack(LinkedList *linkedlist,Element data) { LinkedList_Insert(linkedlist,linkedlist->tail,data); } void LinkedList_Insert(LinkedList *linkedlist,Link pos,Element data) { Link link = New_Node(data); HangNode(link,pos); linkedlist->usage++; } Link LinkedList_Begin(LinkedList *linkedlist) { return linkedlist->head->next; } Link LinkedList_End(LinkedList *linkedlist) { return linkedlist->tail; } void LinkedList_Erase(LinkedList *linkedlist,Link pos) { DisconnectNode(pos); linkedlist->usage--; }
//EHQueue.h #pragma once #include "LinkedList.h" typedef LinkedList EHQueue; EHQueue *New_EHQueue(); void Delete_EHQueue(EHQueue *ehq); void EHQueue_Put(EHQueue *ehq, Element data); Element EHQueue_Get(EHQueue *ehq); int EHQueue_IsEmpty(EHQueue *ehq);
//EHQueue.c #include "EHQueue.h" EHQueue *New_EHQueue() { return New_LinkedList(); } void Delete_EHQueue(EHQueue *ehq) { Delete_LinkedList(ehq); } void EHQueue_Put(EHQueue *ehq, Element data) { LinkedList_PushBack(ehq,data); } Element EHQueue_Get(EHQueue *ehq) { Element element = 0; if( ! EHQueue_IsEmpty(ehq)) { Link first = LinkedList_Begin(ehq); element = first->data; LinkedList_Erase(ehq,first); } return element; } int EHQueue_IsEmpty(EHQueue *ehq) { return ehq->usage == 0; }
//Program.c #include "EHQueue.h" #include "Book.h" int main() { EHQueue *ehq = 0; Book *book = 0; ehq = New_EHQueue(); EHQueue_Put(ehq,New_Book("C언어","홍길동",10)); EHQueue_Put(ehq,New_Book("C++언어","강감찬",20)); EHQueue_Put(ehq,New_Book("자료구조","김구",5)); book = (Book *)EHQueue_Get(ehq); if(book) { Book_View(book); Delete_Book(book); } EHQueue_Put(ehq,New_Book("알고리즘","이순신",9)); EHQueue_Put(ehq,New_Book("디자인패턴","정약용",13)); while( ! EHQueue_IsEmpty(ehq)) { book = (Book *)EHQueue_Get(ehq); if(book) { Book_View(book); Delete_Book(book); } } Delete_EHQueue(ehq); return 0; }
▷ 실행 결과
<0000000010>:<C언어> 저자:홍길동 <0000000020>:<C++언어> 저자:강감찬 <0000000005>:<자료구조> 저자:김구 <0000000009>:<알고리즘> 저자:이순신 <0000000013>:<디자인패턴> 저자:정약용