C List

 

 

#include <stdio.h>

#include <malloc.h>

#include <string.h>

 

#define __TEST_VRSER__

 

struct SUer{

    int sNumber;

    char sName[20];

    float fMath;

};

 

typedef SUer DATA;

 

struct SNode{

    DATA data;

    SNode *pNext;

};

 

SNode *g_pHead = NULL;

 

void AddHead(DATA _data){

    SNode *p = (SNode*)malloc(sizeof(SNode));

    p->data = _data;

    p->pNext = g_pHead;

 

    g_pHead = p;

}

 

void AddTalie(DATA _data){

    SNode *pNew = (SNode*)malloc(sizeof(SNode));

    pNew->data = _data;

    pNew->pNext = NULL;

    if (!g_pHead) {

        g_pHead = pNew;

    }

    SNode *p = g_pHead;

    while(p->pNext != NULL)

        p = p->pNext;

    p->pNext = pNew;

}

 

void Print(){

    SNode *p = g_pHead;

    while (p) {

        printf("%d\t%s\t%0.2f\n",p->data.sNumber,p->data.sName,p->data.fMath);

#ifdef __TEST_VRSER__

        printf("p=0x%p\n",p);

#endif

        p = p->pNext;

    }

    printf("\n");

}

 

void Modinfy(){

    int number;

    printf("請輸入你要查詢的學號:");

    scanf("%d",&number);

    SNode *p = g_pHead;

    while(p->pNext){

        if (p->data.sNumber == number) {

            break;

        }

        p = p->pNext;

    }

    if (!p) {

        puts("你輸入的學號不存在:");

        return;

    }

    printf("%d\t\%s\t\%0.1f\n\n",p->data.sNumber,p->data.sName,p->data.fMath);

    printf("請輸入姓名和成績");

    scanf("%s",p->data.sName,sizeof(p->data.sName));

    scanf("%f",&p->data.fMath);

    Print();

}

 

void Input(const int &_dt){

    int m;

    printf("請輸入學號: \n");

    scanf("%d",&m);

    DATA gData;

    gData.sNumber = m;

    printf("請輸入名字: \n");

    scanf("%s",gData.sName,sizeof(gData.sName));

    printf("請輸入成績: \n");

    scanf("%f",&gData.fMath);

    if (1 == _dt){

        AddHead(gData);

    }else if(2 == _dt){

        AddTalie(gData);

    }

}

 

int Find(const int &_data){

    SNode *p = g_pHead;

    int i = 0;

    while (p) {

        if (p->data.sNumber == _data) {

            return i;

        }

        i++;

        p = p->pNext;

    }

 

    return -1;

}

 

 

 

int Delete(const int &nNumber){

    SNode *p = g_pHead;

    SNode *p1 = NULL;

    if (!p)

        return 0;

    if (p->data.sNumber == nNumber) {

        g_pHead = p->pNext;

        free(p);

        return 1;

    }

    while (p) {

        if (p->data.sNumber == nNumber) {

            p1->pNext = p->pNext;

            free(p);

            return 1;

        }

        p1 = p;

        p = p->pNext;

    }

    return NULL;

}

 

int Judge(SNode *q,SNode *m,const int &nIndx){

    switch (nIndx) {

        case 1:

            return q->data.sNumber < m->data.sNumber;

        case 2:

            return strcmp(q->data.sName, m->data.sName) <0;

        case 3:

            return q->data.fMath < m->data.fMath ? q->data.fMath : m->data.fMath;

            break;

    }

 

    return 0;

}

 

void Sort(const int &Indx){

    SNode *p = g_pHead;

    if (!g_pHead) {

        printf("你當前的數據爲空");

        return;

    }

    while (p->pNext) {

        SNode *q = p->pNext;

        SNode *m = p;

        while (q) {

            //if (q->data.fMath < m->data.fMath) {

            if(Judge(q, m, Indx)){

                m = q;

                q = q->pNext;

            }

        }

        if (m != q) {

            DATA t = p->data;

            p->data = m->data;

            m->data = t;

        }

        p = p->pNext;

    }

}

 

void PrintS(SNode *ps[]){

    puts("學號\t姓名\t成績");

    int i = 0;

    while (ps[i]) {

        //SNode *p = ps[i];

        //printf("打印外排鏈表:%d\t%s\t%0.2f\n",p->data.sNumber,

             //  p->data.sName,p->data.fMath);

        printf("打印外排鏈表:%d\t%s\t%0.2f\n",ps[i]->data.sNumber,

               ps[i]->data.sName,ps[i]->data.fMath);

        i++;

    }

}

 

//表外排序

void SortWai(const int &Indx){

    if (!g_pHead) {

        return;

    }

    SNode *p = g_pHead;

   // SNode *ps[100] ={0};

    int sum = 0;

    int i = 0;

    while (p) { //求鏈表個數

        p = p->pNext;

        ++sum;

    }

    SNode* *ps = (SNode**)malloc(sizeof(SNode*)*(sum+1));//sum+1 malloc 動態申請數組的時候對申請一個作爲空節點判斷結束的時候用,也可以在插入的時候添加一個GetSize()函數去記錄插入的個數;

    sum = 0;

    p = g_pHead;

    while (p) {

        ps[sum] = p;

        p = p->pNext;

        ++sum;

    }

    ps[sum] = NULL;

    while (i < sum - 1) {

        int j = i + 1;

        int m = i;

        while (j < sum) {

            if (ps[j]->data.sNumber < ps[m]->data.sNumber) //新手注意這個裏的ps[xx]是一個二級指針 也就是SNode -> pa[xxx].xx

                m = j;

            ++j;

        }

        if (m != i) {

            SNode *t = ps[i];

            ps[i] = ps[m];

            ps[m] = t;

        }

        ++i;

    }

   // ps[i] = NULL;

    PrintS(ps);

    free(ps);

}

void PT(){

    printf("****************************** \n");

    printf("******    c測試   ******* \n");

    printf("****************************** \n");

}

 

 

int main(int argc, const char * argv[]){

 

#ifdef __TEST_VRSER__

    puts("當前版本是測試版");

#else

    PT();

#endif

 

    Input(1);

    Input(2);

    Input(1);

   // Input(2);

    Print();

    

    SortWai(1);

 

 

 

    return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章