[C언어 자료구조] 3.4 연결리스트 소스 코드
//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 now,Link pos)
{
now->prev = pos->prev;
now->next = pos;
pos->prev->next = now;
pos->prev = now;
}
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);
free(pos);
linkedlist->usage--;
}
//Program.c
#include "LinkedList.h"
#include "Book.h"
void View_LinkedList(LinkedList *linkedlist)
{
Book *book = 0;
Link seek = LinkedList_Begin(linkedlist);
Link end = LinkedList_End(linkedlist);
printf("-----보유 도서 목록-----\n");
for( ;seek!=end;seek = seek->next)
{
book = (Book *)(seek->data);
if(book)
{
Book_View(book);
}
}
}
void SimulEnd(LinkedList *linkedlist)
{
Book *book = 0;
Link seek = LinkedList_Begin(linkedlist);
Link end = LinkedList_End(linkedlist);
for( ;seek!=end;seek=seek->next)
{
book = (Book *)(seek->data);
if(book)
{
Delete_Book(book);
}
}
Delete_LinkedList(linkedlist);
}
void Simul_Find(LinkedList *linkedlist,const char *title)
{
Book *book = 0;
Link seek = LinkedList_Begin(linkedlist);
Link end = LinkedList_End(linkedlist);
for( ;seek!=end;seek=seek->next)
{
book = (Book *)(seek->data);
if(book && (Book_CompareTitle(book,title)==0))
{
printf("검색 결과:");
Book_View(book);
return;
}
}
printf("<%s> 책을 찾을 수 없습니다.\n",title);
}
void Simul_Remove(LinkedList *linkedlist,const char *title)
{
Book *book = 0;
Link seek = LinkedList_Begin(linkedlist);
Link end = LinkedList_End(linkedlist);
for( ;seek!=end;seek=seek->next)
{
book = (Book *)(seek->data);
if(book && (Book_CompareTitle(book,title)==0))
{
LinkedList_Erase(linkedlist,seek);
Delete_Book(book);
printf("삭제 성공!\n");
return;
}
}
printf("<%s> 책을 찾을 수 없습니다.\n",title);
}
void Simul_Seq()
{
LinkedList *linkedlist = New_LinkedList();
LinkedList_PushBack(linkedlist,New_Book("C언어","홍길동",10));
LinkedList_PushBack(linkedlist,New_Book("C++언어","강감찬",20));
LinkedList_PushBack(linkedlist,New_Book("자료구조","김구",5));
LinkedList_PushBack(linkedlist,New_Book("알고리즘","이순신",9));
LinkedList_PushBack(linkedlist,New_Book("디자인패턴","정약용",13));
View_LinkedList(linkedlist);
Simul_Find(linkedlist,"C언어");
Simul_Find(linkedlist,"이데아이론");
Simul_Remove(linkedlist,"자료구조");
View_LinkedList(linkedlist);
SimulEnd(linkedlist);
}
void Simul_OrderAdd(LinkedList *linkedlist,Book *book)
{
Book *stored_book = 0;
Link seek = LinkedList_Begin(linkedlist);
Link end = LinkedList_End(linkedlist);
for( ;seek!=end;seek=seek->next)
{
stored_book = (Book *)(seek->next);
if(stored_book && (Book_CompareAuthor(stored_book,book->author)>=0))
{
break;
}
}
LinkedList_Insert(linkedlist,seek,book);
}
void Simul_Order()
{
LinkedList *linkedlist = New_LinkedList();
Simul_OrderAdd(linkedlist,New_Book("C언어","홍길동",10));
Simul_OrderAdd(linkedlist,New_Book("C++언어","강감찬",20));
Simul_OrderAdd(linkedlist,New_Book("자료구조","김구",5));
Simul_OrderAdd(linkedlist,New_Book("알고리즘","이순신",9));
Simul_OrderAdd(linkedlist,New_Book("디자인패턴","정약용",13));
View_LinkedList(linkedlist);
Simul_Find(linkedlist,"C언어");
Simul_Find(linkedlist,"이데아이론");
Simul_Remove(linkedlist,"자료구조");
View_LinkedList(linkedlist);
SimulEnd(linkedlist);
}
int main()
{
Simul_Seq();
Simul_Order();
return 0;
}
'언어 자료구조 알고리즘 > 디딤돌 자료구조 (C언어)' 카테고리의 다른 글
[C언어 자료구조] 4.4 큐 소스 코드 (0) | 2016.11.26 |
---|---|
[C언어 자료구조] 4.3 큐 테스트 (0) | 2016.11.26 |
[C언어 자료구조] 4.2 큐 구현 (0) | 2016.11.26 |
[C언어 자료구조] 4.1 큐 설계 (0) | 2016.11.26 |
[C언어 자료구조] 4. 큐(Queue) (0) | 2016.11.26 |
[C언어 자료구조] 3.3 연결리스트 테스트 (0) | 2016.11.26 |
[C언어 자료구조] 3.2 연결리스트 구현 (0) | 2016.11.26 |
[C언어 자료구조] 3.1 연결리스트 설계 (0) | 2016.11.26 |
[C언어 자료구조] 3. 연결리스트 (0) | 2016.11.26 |
[C언어 자료구조] 2.4 동적 배열 소스 코드 (0) | 2016.11.26 |