数据结构笔记(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值