2017年5月21日 星期日

example three for Data Structure Course(Category Queues)

data.dat
24 7 31 23 26 14 19 8 9 6 43 16 22 0 39 46 22 38 41 23 19 18 14 3 41
說明
從data.dat讀入25個數字,並將這些數字加以分類(0~9、10~19、20~29、超過29以上)、計數。
/*the filename: main
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
LIST *q0_9, *q10_19, *q20_29, *q_o_29;

void creat_four_quque(){
/* create four queue */
    q0_9 = (LIST *) malloc(sizeof(LIST));
    creatList(q0_9);
    q10_19 = (LIST *) malloc(sizeof(LIST));
    creatList(q10_19);
    q20_29 = (LIST *) malloc(sizeof(LIST));
    creatList(q20_29);
    q_o_29 = (LIST *) malloc(sizeof(LIST));
    creatList(q_o_29);
}

int main(){

    FILE *f1;
    int data;
    int status;

    creat_four_quque();
    f1 = fopen("data.dat", "r");
    status = fscanf(f1, "%d", &data);
    while (status != EOF){
        if (data <= 9) {
            insertLastList(q0_9, data);
        }
        else if (data <= 19){
            insertLastList(q10_19, data);
        }
        else if (data <= 29){
            insertLastList(q20_29, data);
        }
        else {
             insertLastList(q_o_29, data);
        }
        status = fscanf(f1, "%d", &data);
    }

    printList(q0_9);
    printList(q10_19);
    printList(q20_29);
    printList(q_o_29);
}
/*the filename: list
 *the filename extension: .h
 */

typedef struct node {
    int data;
    struct node *link;
} NODE;

typedef struct list {
    int count;
    struct node *head;
    struct node *rear;
} LIST;


void creatList(LIST *listHead);
void insertFirstList(LIST *listHead, int data);
void insertLastList(LIST *listHead, int data);
void printList(LIST *listHead);
void dumpNode(NODE *input);
void deleteFirstList(LIST *listHead);


NODE *getFirstList(LIST *listHead);


/*the filename: list_implement_bak
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "list.h"

void creatList(LIST *listHead) {

    listHead->count = 0;
    listHead->head = NULL;
    listHead->rear = NULL;
}

void insertLastList(LIST *listHead, int data){
    NODE *pNew;
    pNew = (NODE *) malloc(sizeof(NODE));
    pNew->data = data;
    pNew->link = NULL;
    if (listHead->head == NULL){ // case 1: empty list
        listHead->head = pNew;
        listHead->rear = pNew;
    }
    else { // case 2: begining list
        listHead->rear->link = pNew;
        listHead->rear = pNew;
    }
    listHead->count++;

}



void insertFirstList(LIST *listHead, int data){

    NODE *pNew;
    pNew = (NODE *) malloc(sizeof(NODE));
    pNew->data = data;
    pNew->link = NULL;
    if (listHead->head == NULL){ // case 1: empty list
        listHead->head = pNew;
        listHead->rear = pNew;
    }
    else { // case 2: begining list
        pNew->link = listHead->head;
        listHead->head = pNew;
    }
    listHead->count++;
}

void deleteFirstList(LIST *listHead){
    NODE *pLoc;  //為了記錄欲刪除的節點所設置
    if (listHead->count == 0){
        printf("Do nothing \n");
    }else if (listHead->count == 1){
        pLoc = listHead->head;
        listHead->head = NULL;
        listHead->rear = NULL;
        listHead->count--;
        free(pLoc);
    } else {
        pLoc = listHead->head;
        listHead->head = pLoc->link;
        listHead->count--;
        free(pLoc);
    }
}

NODE *getFirstList(LIST *listHead){
    NODE *pLoc;
    if (listHead->count == 0){
        printf("Do nothing \n");
    }else {
        pLoc = listHead->head;
        return pLoc;
    }
}

void dumpNode(NODE *input){
    if (input != NULL){
        printf("the data =%d\n", input->data);
    }
}


void printList(LIST *listHead){

    NODE *curr;
    if (listHead->head != NULL){
        printf("the total count = %d\n", listHead->count);
        for (curr = listHead->head; curr != NULL; curr = curr->link){
            printf("%d\n", curr->data);
        }
    }
    else{
        printf("the list is empty\n");
    }
}
說明
事實上,本程式只使用到creatList()、insertLastList()、printList()三個子函式,故可以簡化成以下:
/*the filename: main
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
LIST *q0_9, *q10_19, *q20_29, *q_o_29;

void creat_four_queue(){
/* create four queue */
    q0_9 = (LIST *) malloc(sizeof(LIST));
    creatList(q0_9);
    q10_19 = (LIST *) malloc(sizeof(LIST));
    creatList(q10_19);
    q20_29 = (LIST *) malloc(sizeof(LIST));
    creatList(q20_29);
    q_o_29 = (LIST *) malloc(sizeof(LIST));
    creatList(q_o_29);
}

int main(){

    FILE *f1;
    int data;

    creat_four_queue();
    f1 = fopen("data.dat", "r");
    while (fscanf(f1, "%d", &data) != EOF){
        if (data <= 9) {
            insertLastList(q0_9, data);
        }
        else if (data <= 19){
            insertLastList(q10_19, data);
        }
        else if (data <= 29){
            insertLastList(q20_29, data);
        }
        else {
             insertLastList(q_o_29, data);
        }
    }
    printList(q0_9);
    printList(q10_19);
    printList(q20_29);
    printList(q_o_29);
    return 0;
}

/*the filename: list
 *the filename extension: .h
 */

typedef struct node {
    int data;
    struct node *link;
} NODE;

typedef struct list {
    int count;
    struct node *head;
    struct node *rear;
} LIST;

void creatList(LIST *listHead);
void insertLastList(LIST *listHead, int data);
void printList(LIST *listHead);

/*the filename: list_implement_bak
 *the filename extension: .c
 */
#include <stdio.h>
#include <stdlib.h>
#include "list.h"

void creatList(LIST *listHead)
{
    listHead->count = 0;
    listHead->head = NULL;
    listHead->rear = NULL;
}

void insertLastList(LIST *listHead, int data)
{
    NODE *pNew;
    pNew = (NODE *) malloc(sizeof(NODE));
    pNew->data = data;
    pNew->link = NULL;
    if (listHead->head == NULL)  // case 1: empty list
    {
        listHead->head = pNew;
        listHead->rear = pNew;
    }
    else   // case 2: begining list
    {
        listHead->rear->link = pNew;
        listHead->rear = pNew;
    }
    listHead->count++;

}

void printList(LIST *listHead)
{
    NODE *a;
    if (listHead->head != NULL)
    {
        printf("the total count = %d\n", listHead->count);
        for (a = listHead->head; a != NULL; a = a->link)
        {
            printf("%d\n", a->data);
        }
    }
    else
    {
        printf("the list is empty\n");
    }
}



圖片說明:


沒有留言:

張貼留言