顺序表(静态)

**seqlit.h**
#pragma once
#define _SEQ_LIST_
#ifdef _SEQ_LIST_
#include<stdio.h>
#include<assert.h>
#include<string.h>

#define MAX_LEN 100
typedef int DataType;

typedef struct SeqList
{
    DataType arr[MAX_LEN];
    size_t size;

}SeqList;
typedef enum TAG
{
    TURE,  //真
    FALSE,  //假
}TAG;
typedef struct Findret
{
    TAG Isfind;  //是否找到下标
    size_t index;  //找到数据的下标
}Findret;

void InitSeqList(SeqList* pSeq); //初始化链表
void PrintSeqList(SeqList* pSeq);//输出链表
void PushBack(SeqList* pSeq, DataType x);//在链表后面添加数据
void PopBack(SeqList* pSeq); //删除链表最后一个元素
void PushFront(SeqList* pSeq, DataType x);//在链表开头插入一个元素,其他元素依次向后移
void PopFront(SeqList* pSeq); //删除第一个元素
void Insert(SeqList* pSeq, size_t index, DataType x);//在ndex位置插入x
void Modified(SeqList* pSeq, size_t index, DataType x);//将index位置的元素修改为x
void removed(SeqList* pSeq, size_t index); //移除index位置的元素
Findret  Find(SeqList* pSeq, DataType x, size_t index);//查找数据x,并返回下标
TAG Erase(SeqList* pSeq, DataType x, TAG all); //all等于TURE表示删除所有x
                                                                     //all等于FALSE表示删除第一个x

#endif //_SEQ_LIST_



**seqlist.c**

#include"seqlist.h"


void InitSeqList(SeqList* pSeq)
{
    assert(pSeq);
    memset(pSeq->arr, 0, MAX_LEN * sizeof(DataType));
    pSeq->size = 0;
}
void PrintSeqList(SeqList* pSeq)
{
    int i = 0;
    assert(pSeq);
    for (; i < pSeq->size; i++)
    {
        printf("%d ", pSeq->arr[i]);
    }

    printf("\n");
}
void PushBack(SeqList* pSeq, DataType x)
{
    assert(pSeq);
    if ((pSeq->size) > MAX_LEN -1) //判断链表是否已满
    {
        printf("SeqList is full\n");
        return;
    }
    pSeq->arr[(pSeq->size)++] = x;
}

void PopBack(SeqList* pSeq)
{
    assert(pSeq);
    if (pSeq->size == 0)
    {
        printf("SeqList is empty\n");
        return;
    }
    pSeq->arr[--(pSeq->size)] = 0; //或者 --(pSeq->size);
}
void PushFront(SeqList* pSeq, DataType x)
{
    int i = 0;
    assert(pSeq);
    if ((pSeq->size) > MAX_LEN - 1) //判断链表是否已满
    {
        printf("SeqList is full\n");
        return;
    }
    for (i = pSeq->size; i > 0; i--)
    {
        pSeq->arr[i] = pSeq->arr[i - 1];
    }
    pSeq->arr[0] = x;
    ++(pSeq->size);
}
void PopFront(SeqList* pSeq)
{
    int i = 0;
    assert(pSeq);
    if (pSeq->size == 0)
    {
        printf("SeqList is empty\n");
        return;
    }
    for (; i < pSeq->size; i++)
    {
        pSeq->arr[i] = pSeq->arr[i + 1];

    }
    (pSeq->size)--;
}
void Insert(SeqList* pSeq, size_t index, DataType x)
{
    int i = 0;
    assert(pSeq);
    assert(index <=pSeq->size);
    if ((pSeq->size) > MAX_LEN - 1) //判断链表是否已满
    {
        printf("SeqList is full\n");
        return;
    }
    for (i = pSeq->size; i > index; i--)
    {
        pSeq->arr[i] = pSeq->arr[i - 1];
    }
    pSeq->arr[index] = x;
    (pSeq->size)++;
}
void Modified(SeqList* pSeq, size_t index, DataType x)
{
    assert(pSeq);
    assert(index <= pSeq->size);
    pSeq->arr[index] = x;
}
void removed(SeqList* pSeq, size_t index)
{
    int i = 0;
    assert(pSeq);
    assert(index <= pSeq->size);
    for (i = index; i < pSeq->size; i++)
    {
        pSeq->arr[i] = pSeq->arr[i + 1];

    }
    (pSeq->size)--;
}

Findret Find(SeqList* pSeq, DataType x, size_t index)
{
    Findret ret;
    assert(pSeq);
    for (; index < pSeq->size; index ++)
    {
        if (pSeq->arr[index] == x)  //找到x
        {
            ret.Isfind = TURE;
            ret.index = index;
            return ret;
        }
    }
    ret.Isfind = FALSE;
    return ret;
}
TAG Erase(SeqList* pSeq, DataType x, TAG all)
{
    TAG success = FALSE;
    Findret ret;
    assert(pSeq);
    ret = Find(pSeq, x, 0);
    while (ret.Isfind == TURE)
    {
        success = TURE;
        removed(pSeq, ret.index);
        if (all == FALSE)
        {
            break;
        }
        ret = Find(pSeq, x, ret.index);
    }
    return success;
}



**main.c**
#include"seqlist.h"

void test()
{
    SeqList s;
    InitSeqList(&s);
    PushBack(&s, 1);
    PushBack(&s, 2);
    PushBack(&s, 3);
    PushBack(&s, 4);
    PopBack(&s);
    PushFront(&s, 10);
    PopFront(&s);
    Insert(&s, 2, 10);
    Modified(&s, 3, 11);
    removed(&s, 1);
    PrintSeqList(&s);
}

void test2()
{
        SeqList s;
        InitSeqList(&s);
        Insert(&s, 0, 0);
        Insert(&s, 1, 1);
        Insert(&s, 2, 2);
        Insert(&s, 3, 2);
        Insert(&s, 4, 3);
        Insert(&s, 5, 2);
        PrintSeqList(&s);

        Erase(&s, 2, FALSE);
        PrintSeqList(&s);

        Erase(&s, 2, TURE);
        PrintSeqList(&s);
}

int main()
{
    test();
    test2();
    getchar();
    return 0;
}

你可能感兴趣的:(静态,顺序表,查,删,增)