【C语言】静态顺序表的实现(包括头插、头删、尾插、尾删、查找、删除指定位置)

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;
#include<assert.h>

#define MAXSIZE 100
typedef int DataType;


typedef struct SeqList
{
    DataType _array[MAXSIZE];
    size_t _size;
}SeqList;


void InitSeqList(SeqList* pSeq)
{
    assert(pSeq);
    memset(pSeq->_array,0, sizeof(DataType)*MAXSIZE);    
    pSeq->_size = 0;
}


void PushBack(SeqList* pSeq,DataType x)
{
    assert(pSeq->_size< MAXSIZE);
    pSeq->_array[pSeq->_size++] = x;
}


void PopBack(SeqList* pSeq)
{
    assert(pSeq->_size>0);
    --pSeq->_size;
}


void PushFront(SeqList* pSeq,DataType x)
{
    assert(pSeq->_size < MAXSIZE);
    for (int i = pSeq->_size-1; i >= 0; i--)
    {
        pSeq->_array[i+1] = pSeq->_array[i];
    }
    ++pSeq->_size;
    pSeq->_array[0] = x;
}


void PopFront(SeqList* pSeq)
{
    assert(pSeq->_size > 0);
    for (int i = 1; i < pSeq->_size; i++)
    {
        pSeq->_array[i - 1] = pSeq->_array[i];
    }
    --pSeq->_size;
}

void Insert(SeqList* pSeq,int pos, DataType x)
{
    assert(pos >= 0);
    assert(pSeq->_size < MAXSIZE);
    for (int i = pSeq->_size - 1; i >= pos; i--)
    {
        pSeq->_array[i+1] = pSeq->_array[i];
    }
    ++pSeq->_size;
    pSeq->_array[pos] = x;
}


int Find(SeqList*pSeq,DataType x)
{
    int i = 0;
    for (i = 0; i < pSeq->_size; i++)
    {
        if (pSeq->_array[i] == x)
        {
            return i;
        }
    }
    return -1;
}


void Erase(SeqList* pSeq, int pos)
{
    assert(pos > 0);
    for (int i = pos; i < pSeq->_size; i++)
    {
        pSeq->_array[i] = pSeq->_array[i + 1];
    }
    --pSeq->_size;
}


void PrintSeqList(SeqList* pSeq)
{
    if (pSeq->_size == 0)
    {
        printf("当前顺序表为空!");
        return;
    }
    for (int i = 0; i < pSeq->_size; i++)
    {
        printf("%d  ", pSeq->_array[i]);
    }
    printf("\n");
}


void Test()
{
    SeqList List;
    InitSeqList(&List);
    PushBack(&List,1);
    PushBack(&List, 2);
    PushBack(&List, 3);
    PushBack(&List, 4);
    PushBack(&List, 5);
    PushBack(&List, 6);
    PrintSeqList(&List);

    PopBack(&List);
    PopBack(&List);
    PopBack(&List);
    PrintSeqList(&List);

    PushFront(&List,0);
    PushFront(&List, -1); 
    PushFront(&List, -2);
    PrintSeqList(&List);

    PopFront(&List);
    PopFront(&List);
    PopFront(&List);
    PrintSeqList(&List);

    int ret = Find(&List, 2);
    printf("%d\n", ret);

    Erase(&List, 1);
    PrintSeqList(&List);

    Insert(&List, 1, 2);
    PrintSeqList(&List);

}


int main()
{
    Test();
    system("pause");
    return 0;
}


本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1769310

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值