C++实现静态顺序表的增删查改

顺序表:用一段地址连续的存储单元依s次存储数据元素的线性结构,是线性表的一种。

SeqList.h

//SeqList.h  

#pragma once  

#include   
#include   

#define MAX_SIZE 5  
typedef int DataType;  

//定义顺序表结构体  
typedef struct SeqList  
{  
    DataType array[MAX_SIZE];  //数据块数组  
    size_t size;               //当前有效数据个数  
}SeqList;  

//有关顺序表函数的声明  
void InitSeqList(SeqList* pSeq);  
void PushBack(SeqList* pSeq,DataType x);  
void PopBack(SeqList* pSeq);  
void PushFront(SeqList* pSeq,DataType x);  
void PopFront(SeqList* pSeq);  
void PrintSeqList(SeqList* pSeq);  
void Insert(SeqList* pSeq, size_t pos, DataType x);  
int Find(SeqList* pSeq, DataType x);  
void Erase(SeqList* pSeq, size_t pos);  
void Remove(SeqList* pSeq, DataType x);  
void RemoveAll(SeqList* pSeq, DataType x);  

//初始化  
void InitSeqList(SeqList* pSeq)  
{  
    assert(pSeq);  
    memset(pSeq->array, 0, sizeof(DataType)*MAX_SIZE);  
    pSeq->size = 0;  
}  

//尾插  
void PushBack(SeqList* pSeq, DataType x)  
{  
    assert(pSeq);  
    if (pSeq->size >= MAX_SIZE)  
    {  
        cout << "The SeqList is Full!" << endl;  
        return;  
    }  
    pSeq->array[pSeq->size++] = x;  
}  

//尾删  
void PopBack(SeqList* pSeq)  
{  
    assert(pSeq);  
    if (pSeq->size <= 0)  
    {  
        cout << "The SeqList is Empty!" << endl;  
        return;  
    }  
    pSeq->array[--pSeq->size] = 0;  
}  

//头插  
void PushFront(SeqList* pSeq, DataType x)  
{  
    assert(pSeq);  
    DataType begin = pSeq->size - 1;  
    if (pSeq->size >= MAX_SIZE)  
    {  
        cout << "The SeqList is Full!" << endl;  
        return;  
    }  
    for (;begin >= 0;--begin)  
    {  
        pSeq->array[begin + 1] = pSeq->array[begin];  
    }  
    pSeq->array[0] = x;  
    ++pSeq->size;  
}  

//头删  
void PopFront(SeqList* pSeq)  
{  
    assert(pSeq);  
    DataType begin = 0;  
    if (pSeq->size <= 0)  
    {  
        cout << "The SeqList is Empty!" << endl;  
        return;  
    }  
    for (;begin <= pSeq->size;++begin)  
    {  
        pSeq->array[begin] = pSeq->array[begin + 1];  
    }  
    pSeq->array[pSeq->size] = 0;  
    --pSeq->size;  
}  

//打印  
void PrintSeqList(SeqList* pSeq)  
{  
    assert(pSeq);  
    DataType i = 0;  
    for (;i < pSeq->size;++i)  
    {  
        cout << pSeq->array[i]<<" ";  
    }  
    cout << endl;  
}  

//修改某个位置上的数据  
void Insert(SeqList* pSeq, size_t pos, DataType x)  
{  
    assert(pSeq);  
    DataType begin = pSeq->size;  
    if (pos >= pSeq->size)  
    {  
        cout << "The pos is wrong!" << endl;  
        return;  
    }  
    if (pSeq->size >= MAX_SIZE)  
    {  
        cout << "The SeqList is Full!" << endl;  
        return;  
    }  
    for (;begin >= pos;--begin)  
    {  
        pSeq->array[begin] = pSeq->array[begin - 1];  
    }  
    pSeq->array[pos - 1] = x;  
    ++pSeq->size;  
}  

//查找  
int Find(SeqList* pSeq, DataType x)  
{  
    assert(pSeq);  
    int i = 0;  
    for (;i < pSeq->size;++i)  
    {  
        if (pSeq->array[i] == x)  
        {  
            return i;  
        }  
    }  
    return -1;    //表示没有找到x  
}  

//删除某个位置上的数据  
void Erase(SeqList* pSeq, size_t pos)  
{  
    assert(pSeq);  
    //DataType begin = pSeq->size -1;  
    if (pos >= pSeq->size)  
    {  
        cout << "The pos is wrong!" << endl;  
        return;  
    }  
    if (pSeq->size <= 0)  
    {  
        cout << "The SeqList is Empty!" << endl;  
        return;  
    }  
    for (;pos < pSeq->size;++pos)  
    {  
        pSeq->array[pos] = pSeq->array[pos + 1];  
    }  
    --pSeq->size;  
}  

//删除顺序表中第一个值为X的数据  
void Remove(SeqList* pSeq, DataType x)  
{  
    assert(pSeq);  
    int pos = 0;  
    pos = Find(pSeq, x);  
    if (pos != -1)  
    {  
        Erase(pSeq, pos);  
    }  
}  

//删除顺序表中所有值为X的数据  
void RemoveAll(SeqList* pSeq, DataType x)  
{  
    assert(pSeq);  
    int pos = 0;  
    pos = Find(pSeq, x);  
    while (pos != -1)  
    {  
        Erase(pSeq, pos);  
        pos = Find(pSeq, x);  
    }  
}  

Test.cpp


  //Test.cpp 
#define _CRT_SECURE_NO_WARNINGS 1  

#include   
using namespace std;  

#include "SeqList.h"  

//测试尾插  
void Test1()  
{  
    SeqList seq;  
    InitSeqList(&seq);  
    PushBack(&seq, 1);  
    PushBack(&seq, 2);  
    PushBack(&seq, 3);  
    PushBack(&seq, 4);  
    PushBack(&seq, 5);  
    PushBack(&seq, 6);  

    PrintSeqList(&seq);  
}  

//测试头插头删  
void Test2()  
{  
    SeqList seq;  
    InitSeqList(&seq);  
    PushBack(&seq, 1);  
    PushBack(&seq, 2);  
    PushBack(&seq, 3);  
    PushBack(&seq, 4);  
    PushFront(&seq, 0);  
    PushFront(&seq, -1);  

    PrintSeqList(&seq);  

    PopBack(&seq);  
    PopBack(&seq);  
    PopBack(&seq);  
    PopBack(&seq);  
    PopBack(&seq);  
    PopBack(&seq);  

    PrintSeqList(&seq);  
}  

//测试修改某个位置上的数据  
void Test3()  
{  
    SeqList seq;  
    InitSeqList(&seq);  
    PushBack(&seq, 1);  
    PushBack(&seq, 2);  
    PushBack(&seq, 4);  
    PushBack(&seq, 5);  

    Insert(&seq, 3, 3);  
    PrintSeqList(&seq);  
}  

//测试查找和删除  
void Test4()  
{  
    SeqList seq;  
    InitSeqList(&seq);  
    PushBack(&seq, 1);  
    PushBack(&seq, 2);  
    PushBack(&seq, 3);  
    PushBack(&seq, 4);  
    PrintSeqList(&seq);  

    int ret = Find(&seq, 2);  
    cout << "pos:" << ret << endl;  
    Erase(&seq, 3);  
    PrintSeqList(&seq);  
}  

//测试删除顺序表中第一个值为x的数据  
void Test5()  
{  
    SeqList seq;  
    InitSeqList(&seq);  
    PushBack(&seq, 1);  
    PushBack(&seq, 2);  
    PushBack(&seq, 3);  
    PushBack(&seq, 4);  
    PrintSeqList(&seq);  

    Remove(&seq, 2);  
    PrintSeqList(&seq);  
}  

int main()  
{  
    //Test1();  
    //Test2();  
    //Test3();  
    //Test4();  
    Test5();  
    system("pause");  
    return 0;  
}  

你可能感兴趣的:(C++)