#pragma once
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
typedef int DateType;
typedef struct SeqList
{
DateType* _array;
size_t _size;
size_t _capacity;
}SeqList;
void InitSeqList(SeqList* pSeq)
{
assert(pSeq);
pSeq->_capacity = 3; //方便测试,给小点
pSeq->_array = (DateType*)malloc\
(sizeof(DateType)*pSeq->_capacity);
memset(pSeq->_array, 0, sizeof(DateType)*3);
pSeq->_size = 0;
}
void CheckCapacity(SeqList* pSeq) //检测容量是否足够,不够开辟
{
if (pSeq->_size >= pSeq->_capacity)
{
DateType* tmp;
pSeq->_capacity *= 2;
tmp = (DateType*)malloc(sizeof(DateType)*pSeq->_capacity);
memcpy(tmp, pSeq->_array, sizeof(DateType)*pSeq->_size);
free(pSeq->_array);
pSeq->_array = tmp;
}
}
void PushBack(SeqList*pSeq, DateType x) //从尾部插入数据
{
assert(pSeq);
CheckCapacity(pSeq);
pSeq->_array[pSeq->_size++] = x;
}
void PopBack(SeqList* pSeq) //从尾部删除数据
{
assert(pSeq);
CheckCapacity(pSeq);
if (pSeq->_size == 0)
{
printf("SeqList is empty\n");
return;
}
//pSeq->_array[pSeq->_size--] = 0;
pSeq->_size--;
}
void PushFront(SeqList* pSeq, DateType x) //从头部插入数据
{
int i = pSeq->_size;
assert(pSeq);
CheckCapacity(pSeq);
for (; i > 0; i--)
{
pSeq->_array[i] = pSeq->_array[i - 1];
}
pSeq->_array[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-1; i++)
{
pSeq->_array[i] = pSeq->_array[i + 1];
}
pSeq->_size--;
}
void Insert(SeqList* pSeq, size_t pos, DateType x) //任意位置插入,pos是位置,即下标
{
int i = pSeq->_size;
assert(pSeq);
CheckCapacity(pSeq);
assert(pos <= pSeq->_size);
for (; i >= (int)pos; i--)//一定要注意有符号无符号数,或者把上面参数的类型改成int
{
pSeq->_array[i] = pSeq->_array[i - 1];
}
pSeq->_array[pos-1] = x;
pSeq->_size++;
}
void Erase(SeqList* pSeq, size_t pos) //任意位置删除,把这个位置数删掉
{
int i = pos - 1;
assert(pSeq);
assert(pos <= pSeq->_size);
for (; i < pSeq->_size - 1; i++)
{
pSeq->_array[i] = pSeq->_array[i + 1];
}
pSeq->_size--;
}
void Remove(SeqList* pSeq, DateType x) // 删第一个x
{
int i = 0;
assert(pSeq);
for (; i < pSeq->_size; i++)
{
if (pSeq->_array[i] == x)
{
int begin = i;
for (; begin < pSeq->_size - 1; begin++)
{
pSeq->_array[begin] = pSeq->_array[begin + 1];
}
pSeq->_size--;
return;
}
}
}
void Removeall(SeqList* pSeq, DateType x)
{
int count = 0;
size_t firstIndex = 0, secondIndex = 0;
assert(pSeq);
while (secondIndex < pSeq->_size)
{
if (pSeq->_array[secondIndex] == x)
{
pSeq->_size--;//在这里减减不对
//secondIndex++;
count++;
}
else
{
pSeq->_array[firstIndex] = pSeq->_array[secondIndex];
firstIndex++;
//secondIndex++;
}
secondIndex++;
}
pSeq->_size -= count;
}
int Find(SeqList* pSeq, DateType x)
{
int i = 0;
assert(pSeq);
if (pSeq->_size == 0)
{
printf("SeqList is empty\n");
return;
}
for (; i < pSeq->_size; i++)
{
if (pSeq->_array[i] == x)
{
return 1;
}
}
return -1;
}
void Modify(SeqList* pSeq, size_t pos, DateType x) //修改某位置的数
{
int i = 0;
assert(pSeq);
assert(pos <= pSeq->_size);
/*if (pSeq->_size == 0)
{
printf("SeqList is empty\n");
return;
}
for (; i < pos; i++)
{
if (i == (pos-1))
{
pSeq->_array[pos-1] = x;
}
}*/
//或者:
pSeq->_array[pos - 1] = x;
}
//源文件
#include <stdio.h>
#include "SeqList.h"
void Test1() //测试尾部的操作
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PrintSeqList(&s);
PopBack(&s);
PopBack(&s);
PrintSeqList(&s);
}
void Test2() //测试头部的操作
{
SeqList s;
InitSeqList(&s);
PushFront(&s, 1);
PushFront(&s, 2);
PushFront(&s, 3);
PushFront(&s, 4);
PrintSeqList(&s);
PopFront(&s);
PopFront(&s);
PrintSeqList(&s);
}
void Test3() //任意位置插入
{
SeqList s;
InitSeqList(&s);
Insert(&s, 0, 0);
Insert(&s, 0, 1);
Insert(&s, 0, 2);
Insert(&s, 0, 3);
PrintSeqList(&s);
}
void Test4() //删除某一个指定的数
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 2);
PushBack(&s, 2);
PushBack(&s, 4);
PrintSeqList(&s);
//Remove(&s, 2);
Removeall(&s, 2);
PrintSeqList(&s);
}
void Test5() //找一个数,找到返回1,找不到返回-1
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PrintSeqList(&s);
int ret = Find(&s, 6);
printf("%d\n", ret);
}
void Test6()
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 1);
PushBack(&s, 1);
PushBack(&s, 1);
PrintSeqList(&s);
Modify(&s, 3, 4);
PrintSeqList(&s);
}
void Test7()
{
SeqList s;
InitSeqList(&s);
PushBack(&s, 1);
PushBack(&s, 2);
PushBack(&s, 3);
PushBack(&s, 4);
PrintSeqList(&s);
Erase(&s, 2);
PrintSeqList(&s);
}
int main()
{
//Test1();
//Test2();
//Test3();
//Test4();
//Test5();
//Test6();
//Test7();
return 0;
}
转载于:https://blog.51cto.com/10706198/1712611