언어 자료구조 알고리즘/디딤돌 자료구조 (C언어)

[C언어 자료구조] 2.4 동적 배열 소스 코드

언제나휴일 2016. 11. 26. 12:55
반응형

[C언어 자료구조] 2.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;

}


//Array.h

#pragma once

typedef void * Element;

typedef struct _Array Array;

struct _Array

{

    Element *base;

    int capacity;

    int usage;

};

typedef Element *Iterator;

 

Array *New_Array();

void Delete_Array(Array *arr);

void Array_SetSize(Array *arr,int capacity,Element data);

void Array_PushBack(Array *arr,Element data);

void Array_Insert(Array *arr,Iterator pos,Element data);

void Array_SetAt(Array *arr,int index,Element data);

Element Array_GetAt(Array *arr,int index);

Iterator Array_Begin(Array *arr);

Iterator Array_End(Array *arr);

void Array_Erase(Array *arr,Iterator pos);

 

//Array.c

#include "Array.h"

#include <malloc.h>

#include <memory.h>

Array *New_Array()

{

    Array *arr = 0;

    arr = (Array *)malloc(sizeof(Array));

    arr->base = 0;

    arr->capacity = arr->usage = 0;

    return arr;

}

void Delete_Array(Array *arr)

{

    if(arr->base)

    {

        free(arr->base);

    }

    free(arr);

}

void Array_SetSize(Array *arr,int capacity,Element data)

{      

    arr->capacity = capacity;

    arr->base = (Element *)realloc(arr->base,sizeof(Element)*arr->capacity);

    for(    ;arr->usage<arr->capacity; arr->usage++)

    {

        arr->base[arr->usage] = data;

    }

}

void Array_PushBack(Array *arr,Element data)

{

    Iterator at = Array_End(arr);

    Array_Insert(arr,at,data);

}

void Array_Insert(Array *arr,Iterator pos,Element data)

{

    int index = pos - arr->base;

    int mcount = arr->usage - index;

    if(arr->capacity == arr->usage)

    {

        if(arr->capacity)

        {

            arr->capacity*=2;

        }

        else

        {

            arr->capacity = 1;

        }

        arr->base = (Element *)realloc(arr->base,sizeof(Element)*arr->capacity);

    }

    memcpy(arr->base+index+1,arr->base+index,sizeof(Element)*mcount);

    arr->base[index] = data;

    arr->usage++;

}

void Array_SetAt(Array *arr,int index,Element data)

{

    if((index>=0)&&(index<arr->usage))

    {

        arr->base[index] = data;

    }

}

Element Array_GetAt(Array *arr,int index)

{

    if((index>=0)&&(index<arr->usage))

    {

        return arr->base[index];

    }

    return 0;

}

Iterator Array_Begin(Array *arr)

{

    return arr->base;

}

Iterator Array_End(Array *arr)

{

    return arr->base+arr->usage;

}

void Array_Erase(Array *arr,Iterator pos)

{

    int index = pos - arr->base;

    int mcount = arr->usage - index -1;

    memcpy(pos,pos+1,sizeof(Element)*mcount);

    arr->usage--;

}


//Program.c

#include "Array.h"

#include "Book.h"

void View_Array(Array *arr)

{

    Book *book = 0;

    Iterator seek = Array_Begin(arr);

    Iterator end = Array_End(arr);

   

    printf("-----보유 도서 목록-----\n");

    for(   ;seek!=end;seek++)

    {

        book = (Book *)(*seek);

        if(book)

        {

            Book_View(book);

        }

    }  

}

void SimulEnd(Array *arr)

{

    Book *book = 0;

    Iterator seek = Array_Begin(arr);

    Iterator end = Array_End(arr);

   

    for(   ;seek!=end;seek++)

    {

        book = (Book *)(*seek);

        if(book)

        {

            Delete_Book(book);

        }

    }

    Delete_Array(arr);

}

void Simul_Find(Array *arr,const char *title)

{

    Book *book = 0;

    Iterator seek = Array_Begin(arr);

    Iterator end = Array_End(arr);

   

    for(   ;seek!=end;seek++)

    {

        book = (Book *)(*seek);

        if(book && (Book_CompareTitle(book,title)==0))

        {

            printf("검색 결과:");

            Book_View(book);

            return;

        }

    }

    printf("<%s> 책을 찾을 수 없습니다.\n",title);

}

void Simul_Remove(Array *arr,const char *title)

{

    Book *book = 0;

    Iterator seek = Array_Begin(arr);

    Iterator end = Array_End(arr);

   

    for(   ;seek!=end;seek++)

    {

        book = (Book *)(*seek);

        if(book && (Book_CompareTitle(book,title)==0))

        {

            Array_Erase(arr,seek);

            Delete_Book(book);

            printf("삭제 성공!\n");

            return;

        }

    }

    printf("<%s> 책을 찾을 수 없습니다.\n",title);

}

void Simul_Seq()

{

    Array *arr = New_Array();

    Array_PushBack(arr,New_Book("C언어","홍길동",10));

    Array_PushBack(arr,New_Book("C++언어","강감찬",20));

    Array_PushBack(arr,New_Book("자료구조","김구",5));

    Array_PushBack(arr,New_Book("알고리즘","이순신",9));

    Array_PushBack(arr,New_Book("디자인패턴","정약용",13));

    View_Array(arr);

    Simul_Find(arr,"C언어");

    Simul_Find(arr,"이데아이론");

    Simul_Remove(arr,"자료구조");

    View_Array(arr);

    SimulEnd(arr);     

}

 

void Simul_OrderAdd(Array *arr,Book *book)

{      

    Book *stored_book = 0;

    Iterator seek = Array_Begin(arr);

    Iterator end = Array_End(arr);

   

    for(   ;seek!=end;seek++)

    {

        stored_book = (Book *)(*seek);

        if(stored_book && (Book_CompareAuthor(stored_book,book->author)>=0))

        {

            break;

        }

    }  

    Array_Insert(arr,seek,book);

}

void Simul_Order()

{

    Array *arr = New_Array();

   

    Simul_OrderAdd(arr,New_Book("C언어","홍길동",10));

    Simul_OrderAdd(arr,New_Book("C++언어","강감찬",20));

    Simul_OrderAdd(arr,New_Book("자료구조","김구",5));

    Simul_OrderAdd(arr,New_Book("알고리즘","이순신",9));

    Simul_OrderAdd(arr,New_Book("디자인패턴","정약용",13));

    View_Array(arr);

    Simul_Find(arr,"C언어");

    Simul_Find(arr,"이데아이론");

    Simul_Remove(arr,"자료구조");

    View_Array(arr);

    SimulEnd(arr);

}

 

void Simul_FindNum(Array *arr,int bnum)

{

    Book *book = 0;

    book = (Book *)Array_GetAt(arr,bnum-1);

    if(book)

    {

        printf("검색 결과:");

        Book_View(book);               

    }

    else

    {

        printf("<%d>번 책을 찾을 수 없습니다.\n",bnum);

    }

}

void Simul_RemoveNum(Array *arr,int bnum)

{      

    Book *book = 0;

    book = (Book *)Array_GetAt(arr,bnum-1);

    if(book)

    {          

        Delete_Book(book);                     

        Array_SetAt(arr,bnum-1,0);

    }

    else

    {

        printf("<%d>번 책을 찾을 수 없습니다.\n",bnum);

    }

}

void Simul_Index()

{

    Array *arr = New_Array();

    Array_SetSize(arr,100,0);   

    Array_SetAt(arr,9,New_Book("C언어","홍길동",10));

    Array_SetAt(arr,19,New_Book("C++언어","강감찬",20));

    Array_SetAt(arr,4,New_Book("자료구조","김구",5));

    Array_SetAt(arr,8,New_Book("알고리즘","이순신",9));

    Array_SetAt(arr,12,New_Book("디자인패턴","정약용",13));

    View_Array(arr);

    Simul_FindNum(arr,20);

    Simul_FindNum(arr,21);     

    Simul_RemoveNum(arr,9);

    View_Array(arr);

    SimulEnd(arr);

}

int main()

{

    Simul_Seq();

    Simul_Order();

    Simul_Index();

    return 0;

}

반응형