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

[C언어 자료구조] 5.4 스택 소스 코드

언제나휴일 2016. 11. 26. 13:14
반응형

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

}

 


 

//EHStack.h

#pragma once

#include "LinkedList.h"

 

typedef LinkedList EHStack;

 

EHStack *New_EHStack();

void Delete_EHStack(EHStack *ehs);

void EHStack_Push(EHStack *ehs, Element data);

Element EHStack_Pop(EHStack *ehs);

int EHStack_IsEmpty(EHStack *ehs);

 


//EHStack.c

#include "EHStack.h"

#include <malloc.h>

#include <memory.h>

EHStack *New_EHStack()

{

    return New_LinkedList();    

}

void Delete_EHStack(EHStack *ehs)

{

    Delete_LinkedList(ehs);     

}

void EHStack_Push(EHStack *ehs, Element data)

{

    LinkedList_PushBack(ehs,data);

}

Element EHStack_Pop(EHStack *ehs)

{

    Element element = 0;

    if( ! EHStack_IsEmpty(ehs))

    {

        Link last = LinkedList_End(ehs);

        last = last->prev;

        element = last->data;

        LinkedList_Erase(ehs,last);

    }

    return element;

}

int EHStack_IsEmpty(EHStack *ehs)

{

    return ehs->usage == 0;

}

 


//Program.c

#include "EHStack.h"

#include "Book.h"

 

int main()

{

    EHStack *ehs = 0;

    Book *book = 0;

    ehs = New_EHStack();

    EHStack_Push(ehs,New_Book("C언어","홍길동",10));             

    EHStack_Push(ehs,New_Book("C++언어","강감찬",20));

    EHStack_Push(ehs,New_Book("자료구조","김구",5));

   

    book = (Book *)EHStack_Pop(ehs);

    if(book)

    {

        Book_View(book);

        Delete_Book(book);

    }

   

    EHStack_Push(ehs,New_Book("알고리즘","이순신",9));

    EHStack_Push(ehs,New_Book("디자인패턴","정약용",13));

   

    while( ! EHStack_IsEmpty(ehs))

    {

        book = (Book *)EHStack_Pop(ehs);

        if(book)

        {

            Book_View(book);

            Delete_Book(book);

        }

    }

    Delete_EHStack(ehs);

    return 0;

}

 

반응형