數據結構筆記(1)-順序表

程序都是自己手打,測試的。有問題可以留言。

1.順序表(what)
順序表是數據結構中鏈表的線性表的一種,是直接用數組做順序存儲數據,所以叫做順序表。

2.爲什麼(why)要有順序表
至於爲什麼,在後面記錄完鏈表的鏈式存儲後,通過順序表和鏈表的比較來說明。

3.怎麼使用順序表
二話不多說,直接上代碼,細節看代碼中的註釋。

3.1 C語言版本
包括main的測試程序和測試結果。

注:因爲c中沒有bool型,我在裏面定義了1未true,0爲false,在輸出值時,爲了區別是本來表中的0,還是表示false的0,判斷找到數的方法不同於教材。
另外,我使用的開發環境時mac的xcode。

list.h

//
//  list.h
//  DataStructure
//
//  Created by xxm on 16/4/11.
//  Copyright © 2016年 xxm. All rights reserved.
//

#ifndef list_h
#define list_h

#include <stdio.h>

#define true 1
#define false 0
#define maxsize 1024
//#define datatype int
typedef int datatype;
typedef int boolen;
//#define boolen int

typedef struct {
    datatype elem[maxsize];
    int length;
}sequencelist;

void InitSequenceList(sequencelist *L);
void ClearSequenceList(sequencelist *L);
int SequenceListLength(sequencelist *L);
boolen SequenceListInsert(sequencelist *L,int i,datatype item);
boolen SequenceListDelete(sequencelist *L,int i);
int SequenceListGetPrior(sequencelist *L,datatype item,datatype *p);
int SequenceListGetNext(sequencelist *L,datatype item,datatype *p);
int SequenceListGetNode(sequencelist *L,int i,datatype *p);
int SequenceLocal(sequencelist *L,datatype item,int *i);

#endif /* list_h */

list.c

//
//  list.c
//  DataStructure
//
//  Created by xxm on 16/4/11.
//  Copyright © 2016年 xxm. All rights reserved.
//

#include "list.h"

//創建順序表
void InitSequenceList(sequencelist *L)
{
    L->length = 0;
}

//清除順序表
void ClearSequenceList(sequencelist *L)
{
    L->length = 0;
}

//獲取順序表的數據長度
int SequenceListLength(sequencelist *L)
{
    return L->length;
}

//順序表中第k個位置插入值item,返回成功或失敗
boolen SequenceListInsert(sequencelist *L,int i,datatype item)
{
    if (i < 1 || i > L->length)
        return false;
    for(int j = L->length; j >= i; j--)
        L->elem[j] = L->elem[j-1];
    L->elem[i-1] = item;
    L->length++;
    return true;
}

//順序表中第i個位置刪除值item,返回成功或失敗
boolen SequenceListDelete(sequencelist *L,int i)
{
    if (i < 1 || i > L->length)
        return false;
    for (int j = i; j <= L->length; j++)
        L->elem[j-1] = L->elem[j];
    L->length--;
    return true;
}

//獲取順序表L中第一個item值的直接前驅,賦值給p
int SequenceListGetPrior(sequencelist *L,datatype item,datatype *p)
{
    int i,j;
    j = L->length;

    if(j == 0)
        return false;//空列表,直接返回false

    for (i = 1; i < j; i++) {//因爲第一項沒有直接前驅,所以i從1開始
        if (item == L->elem[i]) {
            *p = L->elem[i-1];
            return *p;//找到
            /*
             如果此時p=0,如何區分這個0和錯誤時的false?
             所以在聲明時要加入參數datatype p,只有當
             SequenceListGetPrior()函數返回的值
             和datatype p中的p值相同時候才證明是找到了所要的值
             */
        }
    }
    return false;//未找到
}

//獲取順序表L中第一個item值的直接後驅,賦值給p
int SequenceListGetNext(sequencelist *L,datatype item,datatype *p)
{
    int i,j;
    j = L->length;

    if(j == 0)
        return false;//空列表,直接返回false

    for (i = 0; i < j - 1 ; i++) {//因爲最後項沒有直接後驅,所以i到最後第二項結束
        if (item == L->elem[i]) {
            *p = L->elem[i+1];
            return *p;//找到
            /*
             如果此時p=0,如何區分這個0和錯誤時的false?
             所以在聲明時要加入參數datatype p,只有當
             SequenceListGetNext()函數返回的值
             和datatype p中的p值相同時候才證明是找到了所要的值
             */
        }
    }
    return false;//未找到
}

//獲取順序表L第i個數的值,賦值給p
int SequenceListGetNode(sequencelist *L,int i,datatype *p)
{
    int j;
    j = L->length;

    if(j == 0)
        return false;//空列表,直接返回false

    if(i < 1 || i > j )
        return false;//越界,直接返回false

    *p = L->elem[i-1];//表L是從1開始計數,數組從0開始計數,所以要-1
    return *p;//找到
    /*
    如果此時i=0,如何區分這個0和錯誤時的false?
    所以在聲明時要加入參數int i,只有當
    SequenceListGetNode()函數返回的值
    和int i中的i值相同時候才證明是找到了所要的值
    */

}

//獲取順序表L中第一個item值的位置,賦值給i,也作爲函數返回值
int SequenceLocal(sequencelist *L,datatype item,int *i)
{
    int j,k;
    j = L->length;

    if(j == 0)
        return false;//空列表,直接返回false

    for (k = 0; k < j ; k++) {//因爲最後項沒有直接後驅,所以i到最後第二項結束
        if (item == L->elem[k]) {
            *i = k + 1;//表L是從1開始計數,數組從0開始計數,所以要+1
            return *i;//找到下標位置i
            /*
             如果此時i=0,如何區分這個0和錯誤時的false?
             所以在聲明時要加入參數int i,只有當
             SequenceListGetNode()函數返回的值
             和int i中的i值相同時候才證明是找到了所要的值
             */
        }
    }
    return false;//未找到
}

main.c
測試了上面寫的內容,並有結果輸出。

//
//  main.c
//  DataStructure
//
//  Created by xxm on 16/4/11.
//  Copyright © 2016年 xxm. All rights reserved.
//

#include <stdio.h>
#include "list.h"
#include <stdlib.h>
//#include <malloc.h>

//順序表測試
void printSequenList(sequencelist *L)
{
    if (L->length == 0) {
        printf("空表\n");
    }
    for (int i = 0; i < L->length; i++) {
        printf("L[%d] = %d \n",i,L->elem[i]);
    }
}

void sequenceListTest(void)
{
    datatype value[15]={
        12,345,67,434,8,
        2,9,34,45,2,
        345,0,88,13,76};
    int length;
    sequencelist *sequenList;
    datatype num;
    int local;

    int temp;

    sequenList = (sequencelist *)malloc(sizeof(sequenList));
    //初始化順序表
    InitSequenceList(sequenList);
    printSequenList(sequenList);

    //SequenceListInsert(sequenList,0,125);

    //初始化值
    for (int i = 0; i < 15; i++) {
        //SequenceListInsert(sequenList,i+1,value[i]);
        sequenList->elem[i] = value[i];
        sequenList->length++;
    }

    printSequenList(sequenList);

    //求表長
    length = SequenceListLength(sequenList);
    printf("length = %d\n",length);

    //表的插入
    if(SequenceListInsert(sequenList,3,125) == true)
    {
        printf("Insert success\n");
        printSequenList(sequenList);
    }
    else if(SequenceListInsert(sequenList,3,125) == false)
        printf("Insert false\n");

    //表的刪除
    if(SequenceListDelete(sequenList,3) == true)
    {
        printf("Delete success\n");
        printSequenList(sequenList);
    }
    else if(SequenceListDelete(sequenList,3) == false)
        printf("Delete false\n");

    //求表中某數的直接前驅
    temp = SequenceListGetPrior(sequenList,2,&num);
    if (temp == num)
    {
        printSequenList(sequenList);
        printf("GetPrior=%d success \n",num);
    }
    else if(temp == false)
        printf("GetPrior false\n");

    //求表中某數的直接後驅
    temp = SequenceListGetNext(sequenList,2,&num);
    if (temp == num)
    {
        //printSequenList(sequenList);
        printf("GetNext=%d success \n",num);
    }
    else if(temp == false)
        printf("GetNext false\n");

    //求表中某位置的值
    temp = SequenceListGetNode(sequenList,6,&num);
    if (temp == num)
    {
        //printSequenList(sequenList);
        printf("GetNode=%d success \n",num);
    }
    else if(temp == false)
        printf("GetNode false\n");

    //求表中某數的位置
    temp = SequenceLocal(sequenList,345,&local);
    if (temp == local)
    {
        //printSequenList(sequenList);
        printf("Local=%d success \n",num);
    }
    else if(temp == false)
        printf("GetNext false\n");

    //刪除表
    ClearSequenceList(sequenList);
    printSequenList(sequenList);
    free(sequenList);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    sequenceListTest();
    return 0;
}

測試程序輸出的結果

空表
L[0] = 12 
L[1] = 345 
L[2] = 67 
L[3] = 434 
L[4] = 8 
L[5] = 2 
L[6] = 9 
L[7] = 34 
L[8] = 45 
L[9] = 2 
L[10] = 345 
L[11] = 0 
L[12] = 88 
L[13] = 13 
L[14] = 76 
length = 15
Insert success
L[0] = 12 
L[1] = 345 
L[2] = 125 
L[3] = 67 
L[4] = 434 
L[5] = 8 
L[6] = 2 
L[7] = 9 
L[8] = 34 
L[9] = 45 
L[10] = 2 
L[11] = 345 
L[12] = 0 
L[13] = 88 
L[14] = 13 
L[15] = 76 
Delete success
L[0] = 12 
L[1] = 345 
L[2] = 67 
L[3] = 434 
L[4] = 8 
L[5] = 2 
L[6] = 9 
L[7] = 34 
L[8] = 45 
L[9] = 2 
L[10] = 345 
L[11] = 0 
L[12] = 88 
L[13] = 13 
L[14] = 76 
L[0] = 12 
L[1] = 345 
L[2] = 67 
L[3] = 434 
L[4] = 8 
L[5] = 2 
L[6] = 9 
L[7] = 34 
L[8] = 45 
L[9] = 2 
L[10] = 345 
L[11] = 0 
L[12] = 88 
L[13] = 13 
L[14] = 76 
GetPrior=8 success 
GetNext=9 success 
GetNode=2 success 
Local=2 success 
空表
Program ended with exit code: 0
發佈了54 篇原創文章 · 獲贊 53 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章