#include <stdlib.h>
#include <stdio.h>
#define SIZE 6

typedef struct
{
    int front;
    int rear;
    int data[SIZE];
}DequeType;

void init(DequeType *D)
{
    D->front = 0;
    D->rear = 0;
} 

int isfull(DequeType *D)
{
    if(D->rear== (D->front+1)%SIZE)
        return 1;
    else
        return 0;
}

int isEmpty(DequeType *D)
{
    if(D->front == D->rear)
        return 1;
    else
        return 0;
}

int add_rear(DequeType *D, int e_r)
{
    if(isfull(D))
        return -1;
    else
    {      
        D->rear = (D->rear - 1 + SIZE) % SIZE;                 
        D->data[D->rear] = e_r;
        return 0;
    }                   
}

int delete_rear(DequeType *D)
{
    if(isEmpty(D))
        return -1;
    else
    {
        printf("delete rear : %d\n", D->data[D->rear++]);
        return 0;
    } 
}

int get_rear(DequeType *D)
{
    if(isEmpty(D))
        return -1;
    else
    {
        printf("get rear : %d\n", D->data[D->rear]);
        return 0;
    }
}

int add_front(DequeType *D,int e_f)
{
    if(isfull(D))
        return -1;  
    else
    {
        D->front = (D->front + 1) % SIZE;
        D->data[D->front] = e_f;
        return 0;
    }
}

int delete_front(DequeType *D)
{
    if(isEmpty(D))
        return -1;
    else
    {
        printf("delete front : %d\n", D->data[D->front--]);
        return 0;  
    }
}

int get_front(DequeType *D)
{
    if(isEmpty(D))
        return -1;
    else
    {
        printf("get front : %d\n", D->data[D->front]);
        return 0;
    }
}

void print(DequeType *D)
{
    for(int i = 1; i <= D->front; i++)
        printf("%d \n", D->data[i]);
    for(int j = D->rear; j <= SIZE -1 ; j++)
        printf("%d \n", D->data[j]);
}



int main()
{
    DequeType D;
    init(&D);

    add_front(&D, 3);    
    add_front(&D, 1);
    add_front(&D, 5);
    
    add_rear(&D, 2);
    add_rear(&D, 8);

    delete_front(&D);
    delete_rear(&D);

    get_front(&D);
    get_rear(&D);

    print(&D);
    return 0;
}

댓글남기기