QUEUE

언어/c언어2016. 2. 22. 13:03

#include <stdio.h>
#include <stdlib.h>
typedef int element_type_int;
typedef struct queue{
    element_type_int data;
    struct queue* linked;
}queue, *queue_pointer;
typedef struct node{
    queue_pointer head;
    queue_pointer tail;
    int dataCnt;
}node, *node_pointer;
//--------------- FUNCTION COMPLEX -------------------
void Queue_Setting(node_pointer NP);
void Queue_Data_Input(node_pointer NP);
void Queue_Data_Remove(node_pointer NP);
void Queue_Data_Printf(node_pointer NP);
int main(void)
{
    node start;
    Queue_Setting(&start);
    Queue_Data_Input(&start);
    Queue_Data_Input(&start);
    Queue_Data_Input(&start);
    Queue_Data_Input(&start);
    Queue_Data_Printf(&start);
    return 0;
}
//---------------------------------------------------
void Queue_Setting(node_pointer NP){
    NP->head = (queue_pointer)malloc(sizeof(queue));
    if ( NP->head == NULL ){
        printf("malloc fail!!\n");
        return;//END
    }
    else{ // NP->head != NULL
    //------data setting-------------------
        NP->head->data = 0;
        NP->head->linked = NULL;
        NP->tail = NP->head;
        NP->dataCnt = 0;
    //------------------------------------
    }
}
void Queue_Data_Input(node_pointer NP){
    queue_pointer newData_Node = NULL;
    newData_Node = (queue_pointer)malloc(sizeof(queue));
    if (newData_Node == NULL){
        printf("malloc fail!!|\n");
        return;//END
    }
    else{ //newData_Node != NULL
        //----------- data setting ---------------
        newData_Node->data = 0;
        newData_Node->linked = NULL;
        printf("newData input >>>    ");
        scanf("%d", &newData_Node->data);
        //--------------------------------------
        if (NP->dataCnt == 0){
            NP->head->linked = newData_Node;
            NP->tail = newData_Node;
            NP->dataCnt++; // data  +1
        }
        else{ // NP->dataCnt >=1
            NP->tail->linked = newData_Node;
            NP->tail = newData_Node;
            NP->dataCnt++; // data  +1
        }
    }
}
void Queue_Data_Remove(node_pointer NP){
    queue_pointer remove_node = NULL;
    if(NP->dataCnt == 0){
        printf("data is empty !! \n");
        return;//END
    }
    else{//NP->dataCnt >=1
        if (NP->dataCnt == 1){
            remove_node = NP->head->linked;
            NP->head->linked = NULL;
            NP->tail = NULL;
            NP->dataCnt--; // dataCnt -1
            free(remove_node);
        }
        else{// NP->dataCnt >1
            remove_node = NP->head->linked;
            NP->head->linked = NP->head->linked->linked;
            NP->dataCnt--; // dataCnt -1
            free(remove_node);
        }
}    }
void Queue_Data_Printf(node_pointer NP){
    queue_pointer index_node = NULL;
    int index;
    if(NP->dataCnt == 0){
        printf("data is empty !! \n");
        return;//END
    }
    else{//NP->data
        index_node = NP->head->linked;
        for(index = 0; index< NP->dataCnt; index++){
            printf("%d ",index_node->data);
            index_node = index_node->linked;
        }
    }printf("\n");
}

'언어 > c언어' 카테고리의 다른 글

c언어 행렬 구현  (0) 2016.02.25
double linked  (0) 2016.02.23
STACK  (0) 2016.02.21
queue (수정할것 아직 완성되지 않음)  (0) 2016.02.17
Bubble__Sort (ver 0.1) 2016. 2. 17 kimjun hyeon  (0) 2016.02.17