动态顺序表

c语言实现动态顺序表:

#define _CRT_SECURE_NO_WARNIN

#include<stdio.h>

#include<assert.h>

#include<malloc.h>

typedef int Datetype;

typedef struct Seqlist

{

Datetype* _pdate;

size_t _size;

size_t _capacity;

}Seq,*pSeq;

void InitSeqlist(pSeq pseq)

{

assert(pseq);

pseq->_capacity = 10;

pseq->_pdate = (Datetype*)malloc(sizeof(Datetype)*(pseq->_capacity));

    pseq->_size = 0;

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pseq->_capacity; i++)

{

*p = 0;

p++;

}

}

void PrintSeqlist(pSeq pseq)

{

assert(pseq);

int i = (int)pseq->_capacity;//>>>>>>>>>>>注意无符号的   ――   运算

Datetype* p = pseq->_pdate;

while (i--)

{

printf("%d ",*p);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%d?%c?

p++;

}

printf("\n");

}

int find(pSeq pseq, Datetype x)

{

assert(pseq);

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pseq->_size; i++)

{

if (*p == x)

{

return i;

}

p++;

}

return -1;

}

void AddCapacity(pSeq pseq)

{

assert(pseq);

Datetype* padd;

    padd = (Datetype*)malloc(sizeof(Datetype)*(2 * pseq->_capacity));

free(pseq->_pdate);

pseq->_pdate = padd;

}

void PushBack(pSeq pseq, Datetype x)

{

assert(pseq);

Datetype* p = pseq->_pdate;

if (pseq->_size == pseq->_capacity)

{

AddCapacity(pseq);

}

size_t i = 0;

for (i = 0; i < pseq->_size; i++)//size_t   注意    -1 之后的值!!!

{

p++;

}

*p = x;

pseq->_size++;

}

void PopBack(pSeq pseq)

{

assert(pseq);

if (pseq->_size == 0)

{

printf("此表暂无数据!\n");

return 0;

}

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pseq->_size; i++)

{

p++;

}//指向最后一个数据的   后面  一个

*(--p) = 0;

pseq->_size--;

}

void PushFront(pSeq pseq, Datetype x)

{

assert(pseq);

if (pseq->_size == pseq->_capacity)

{

AddCapacity(pseq);

}

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pseq->_size; i++)

{

p++;

}

i = pseq->_size;

while (i--)

{

*p = *(p - 1);

p--;

}

*p = x;

pseq->_size++;

}

void PopFront(pSeq pseq)

{

assert(pseq);

if (pseq->_size == 0)

{

printf("此表暂无数据!\n");

return 0;

}

int i = 0;

Datetype* p = pseq->_pdate;

i = (int)pseq->_size - 1;

while (i--)

{

*p = *(p + 1);

p++;

}

*p = 0;

pseq->_size--;

}

void Erase(pSeq pseq, size_t pos)

{

assert(pseq);

if (pseq->_size == 0)

{

printf("此表暂无数据!\n");

return 0;

}

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pos-1; i++)

{

p++;//.........相当于    p[循环次数]     ..........

}

i = pseq->_size - pos;

while (i--)

{

*p = *(p + 1);

p++;

}

*p = 0;

pseq->_size--;

}

void Remove(pSeq pseq, Datetype x)

{

assert(pseq);

size_t i = 0,j = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pseq->_size; i++)

{

if (*p == x)

{

j = i;

for (; j < pseq->_size; j++)

{

*p = *(p + 1);

p++;

}

pseq->_size--;

return 0;

}

p++;

}

}

void RemoveAll(pSeq pseq, Datetype x)

{

assert(pseq);

size_t i = 0, j = 0;

Datetype* pos = pseq->_pdate;

for (i = 0; i < pseq->_size; i++)

{

if (*pos == x)

{

Datetype* p = pos;

j = i;

for (; j < pseq->_size; j++)

{

*p = *(p + 1);

p++;

}

*p = 0;

pseq->_size--;

pos--;//*****这是保证停留在原来的地方

}

pos++;

}

}

void Modify(pSeq pseq, size_t pos, Datetype x)

{

assert(pseq);

assert(pos <= pseq->_capacity);

size_t i = 0;

Datetype* p = pseq->_pdate;

for (i = 0; i < pos - 1; i++)

{

p++;

}

*p = x;

}

int main()

{

Seq seq;

InitSeqlist(&seq);

PrintSeqlist(&seq);

PushBack(&seq, 1);

PushBack(&seq, 2);

PushBack(&seq, 2);

PushBack(&seq, 3);

PushBack(&seq, 4);

PushBack(&seq, 5);

PrintSeqlist(&seq);

Modify(&seq, 2, 3);

//RemoveAll(&seq, 2);

//Erase(&seq, 4);

//Remove(&seq, 2);

//PopBack(&seq);

//PushFront(&seq, 6);

//PopFront(&seq);

//printf("%d\n", find(&seq, 5));

PrintSeqlist(&seq);

system("pause");

return 0;

}


你可能感兴趣的:(动态顺序表)