1. 程式人生 > >資料結構 C語言 線性表 順序表 實現2

資料結構 C語言 線性表 順序表 實現2

話不多說,先上main函式流程圖


main流程圖方便看程式進行狀態。原本是想將所有的基礎資料結構寫完以後再傳的,可我等不及了,不寫點東西就感覺啥都沒做一樣。

將程式碼集中在一個檔案了,方便傳送和閱讀一些。

#include <stdio.h>
#include <string.h>
#define MAXSIZE 100              //定義線性表的最大長度

//定義線性表——順序儲存結構體—————————————————————————
typedef struct DATA
{
    char key[15];  			    //結點的關鍵字
    char name[20];
    int age;
} DATA;  						//定義結點型別,可定義為簡單型別,也可定義為結構

typedef struct SeqListType      //定義順序表結構
{
    DATA ListData[MAXSIZE+1];   //儲存順序表的陣列
    int ListLen;                //順序表已存結點的數量
} SeqListType;

//函式宣告———————————————————————————————————
void  SeqListInit(SeqListType *SL);                    //初始化順序表
int   SeqListLength(SeqListType *SL);                  //返回順序表的元素數量
int   SeqListAdd(SeqListType *SL,DATA data);           //向順序表中新增元素
int   SeqListInsert(SeqListType *SL,int n,DATA data);  //向順序表中插入元素
int   SeqListDelete(SeqListType *SL,int n);            //刪除順序表中的據元素
DATA *SeqListFindByNum(SeqListType *SL,int n);         //根據序號返回元素
int   SeqListFindByCont(SeqListType *SL,char *key);    //按關鍵字查詢
int   SeqListAll(SeqListType *SL);                     //遍歷順序表中的內容

//函式定義———————————————————————————————————
void SeqListInit(SeqListType *SL)   //初始化順序表
{
    SL->ListLen=0;    			    //初始化時,設定順序表長度為0
}
int SeqListLength(SeqListType *SL)  //返回順序表的元素數量
{
    return (SL->ListLen);
}
int SeqListAdd(SeqListType *SL,DATA data)  //增加元素到順序表尾部
{
    if(SL->ListLen>=MAXSIZE)  			   //順序表已滿
    {
        printf("順序表已滿,不能再新增結點了!\n");
        return 0;
    }
    SL->ListData[++SL->ListLen]=data;
    return 1;
}
int SeqListInsert(SeqListType *SL,int n,DATA data)
{
    int i;
    if(SL->ListLen>=MAXSIZE)   //順序表結點數量已超過最大數量
    {
        printf("順序表已滿,不能插入結點!\n");
        return 0;                //返回0表示插入不成功
    }
    if(n<1 || n>SL->ListLen-1)   //插入結點序號不正確
    {
        printf("插入元素序號錯誤,不能插入元素!\n");
        return 0;                //返回0,表示插入不成功
    }
    for(i=SL->ListLen; i>=n; i--) //將順序表中的資料向後移動
        SL->ListData[i+1]=SL->ListData[i];
    SL->ListData[n]=data;        //插入結點
    SL->ListLen++;               //順序表結點數量增加1
    return 1;                    //返回成功插入
}
int SeqListDelete(SeqListType *SL,int n)  //刪除順序表中的資料元素
{
    int i;
    if(n<1 || n>SL->ListLen+1)   //刪除元素序號不正確
    {
        printf("刪除結點序號錯誤,不能刪除結點!\n");
        return 0;                //返回0,表示刪除不成功
    }
    for(i=n; i<SL->ListLen; i++) //將順序表中的資料向前移動
        SL->ListData[i]=SL->ListData[i+1];
    SL->ListLen--;               //順序表元素數量減1
    return 1;                    //返回成功刪除
}
DATA *SeqListFindByNum(SeqListType *SL,int n)  //根據序號返回資料元素
{
    if(n<1 || n>SL->ListLen+1)   //元素序號不正確
    {
        printf("結點序號錯誤,不能返回結點!\n");
        return NULL;             //返回0,表示不成功
    }
    return &(SL->ListData[n]);
}
int SeqListFindByCont(SeqListType *SL,char *key)  //按關鍵字查詢結點
{
    int i;
    for(i=1; i<=SL->ListLen; i++)
        if(strcmp(SL->ListData[i].key,key)==0)   //如果找到所需結點
            return i;           //返回結點序號
    return 0;  				    //遍歷後仍沒有找到,則返回0
}



int SeqListAll(SeqListType *SL) //遍歷順序表中的結點
{
    int i;
    for(i=1; i<=SL->ListLen; i++)
        printf("(%s,%s,%d)\n",SL->ListData[i].key,SL->ListData[i].name,SL->ListData[i].age);
}

//主函式————————————————————————————————————
int main()
{
    int i;
    SeqListType SL;         //定義順序表變數
    DATA data,*data1;       //定義結點儲存資料型別變數和指標變數
    char key[15];           //儲存關鍵字

    SeqListInit(&SL);       //初始化順序表

    do                      //迴圈新增結點資料
    {
        printf("輸入新增的結點(學號 姓名 年齡):");
        fflush(stdin);              //清空輸入緩衝區
        scanf("%s%s%d",&data.key,&data.name,&data.age);
        if(data.age)                                    //若年齡不為0
        {
            if(!SeqListAdd(&SL,data))                   //若新增結點失敗
                break;                                  //退出死迴圈
        }
        else                //若年齡為0
            break;          //退出死迴圈
    }
    while(1);
    printf("\n順序表中的結點順序為:\n");
    SeqListAll(&SL);                     //顯示所有結點資料

    fflush(stdin);                       //清空輸入緩衝區
    printf("\n要取出結點的序號:");
    scanf("%d",&i);                      //輸入結點序號
    data1=SeqListFindByNum(&SL,i);       //按序號查詢結點
    if(data1)                            //若返回的結點指標不為NULL
        printf("第%d個結點為:(%s,%s,%d)\n",i,data1->key,data1->name,data1->age);

    fflush(stdin);                     //清空輸入緩衝區
    printf("\n要查詢結點的關鍵字:");
    scanf("%s",key);  				   //輸入關鍵字
    i=SeqListFindByCont(&SL,key);      //按關鍵字查詢 ,返回結點序號
    data1=SeqListFindByNum(&SL,i);     //按序號查詢,返回結點指標
    if(data1)                          //若結點指標不為NULL
        printf("第%d個結點為:(%s,%s,%d)\n",i,data1->key,data1->name,data1->age);
    getch();
    return 0;
}