数据结构实验一:(1)(顺序表)线性表的各种操作SqList

#include<iostream>
#include<malloc.h>
#include<cstdio>
#define MAXSIZE 10
using namespace std;
//typedef struct SqList *L;
typedef struct{
        char data[MAXSIZE];
        int length;
    }SqList;

void InitList(SqList *&L){
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}


bool InsertList(SqList *&L,int i,char e){
    int j;
    if(i<1||i>L->length+1)
        return false;
    i--;
    for(j=L->length;j>i;j--)
        L->data[j]=L->data[j-1];
    L->data[i]=e;
    L->length++;
    return true;
}

bool DeleteList(SqList *&L,int i,char e){
    int j;
    if(i<1||i>L->length)
        return false;
    i--;
    e=L->data[i];
    for(j=i;j<L->length-1;j++)
        L->data[j]=L->data[j+1];
    L->length--;
    return true;
}

int DispList(SqList * L){
    int i;
    for(i=0;i<L->length;i++)
        printf("%c ",L->data[i]);
    printf("\n");
}

int LengthList(SqList * L){
    return (L->length);
}

bool ListEmpty(SqList * L){
    return (L->length==0);
}

void DestroyList(SqList *& L){
    free(L);
}


int main(){

    SqList *L;
    char e;
    int i;
    InitList(L);
    InsertList(L,1,'a');
    InsertList(L,2,'b');
    InsertList(L,3,'c');
    InsertList(L,4,'d');
    InsertList(L,5,'e');
    DispList(L);
    printf("%d",LengthList(L));
    printf("\n");
    if(ListEmpty(L)=='true')
        printf("yes");
    else
        printf("no");
    printf("\n");
    printf("%c",L->data[2]);
    printf("\n");
    for(i=0;i<L->length;i++)
        if(L->data[i]=='a')
            printf("%d",i+1);
    printf("\n");
    InsertList(L,4,'f');
    DispList(L);
    DeleteList(L,3,e);
    DispList(L);
    DestroyList(L);

    return 0;
}

总结:

1.线性表顺序存储类型可以描述如下:

typedef struct

{

ElemType data[MaxSize];

int length;

}SqList;
假设ElemType 是int 类,使用如下自定义类型语句:‘

typedef int ElemType;

2。线性表的九种基本运算:

void InitList(SqList *&L)

void DestroyList(SqList *&L)

bool ListEmpty(SqList *L)
int ListLength(SqList *L)

void DispList(SqList *L)

bool GetElem(SqList *L,int i,ElemType &e)

int LocateElem(SqList*L,ElemType e)

bool ListInsert(SqLsit *L,int i,ElemType e)

bool ListDelete(SqList *&L,int i,ElemType &e)

总共返回得到的元素和删除元素需要返回元素e的地址

3.注意

void initlist(sqlist *&L);意思是说,参数L是sqlist类型,且参数是以别名传参出现的。所谓别名的意思说就是,在这个函数中,函数可以修改L的修,且只能在c++中使用。

猜你喜欢

转载自blog.csdn.net/wssyaoqifei/article/details/83215409