上次已经写了静态的顺序表,今天在补充一个动态的顺序表。大体思路还是一样的。请看代码。这次我就不详细解释了。
#include”pSeqList.h”
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int DataType;
#define INIT_UNM 3
typedef struct SeqList
{
DataType* date;
DataType sz;
int capacity;
}SeqList, *pSeqList;
void initSeqList(pSeqList* pseq);
void PushBack(pSeqList pseq, DataType d);
void PopBack(pSeqList pseq);
void FrontBack(pSeqList pseq, DataType d);
void PopFront(pSeqList pseq);
void PrintfSeqList(pSeqList pseq);
int Find(pSeqList pseq, DataType d);
void Insert(pSeqList pseq, int pos, DataType d);
void Erase(pSeqList pseq, int pos);
void Remove(pSeqList pseq, DataType d);
void TestRemoveAll(pSeqList pseq, DataType d);
int Size(pSeqList pseq);
int Empty(pSeqList pseq);
void BubbleSort(pSeqList pseq);
int BinarySearch(pSeqList pseq, DataType d);
int BinarySearch_R(pSeqList pseq, int left, int right, DataType d);
void selectsort(pSeqList pseq);
void SelectSort_op(pSeqList pseq);
#endif // __SEQLIST_H__
#include”pSeqListTest.h”
#include"pSeqList.h";
void Testbushback()
{
SeqList seq;
initSeqList(&seq);
CheckCapacity(&seq);
for (int i = 1; i <= 5; i++)
{
PushBack(&seq, i);
}
PrintfSeqList(&seq);
PopBack(&seq);
PrintfSeqList(&seq);
}
void TestFrontback()
{
SeqList seq;
initSeqList(&seq);
FrontBack(&seq, 1);
FrontBack(&seq, 2);
FrontBack(&seq, 3);
FrontBack(&seq, 4);
PrintfSeqList(&seq);
PopFront(&seq);
PrintfSeqList(&seq);
}
void TestFind()
{
int pos = 0;
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintfSeqList(&seq);
pos = Find(&seq, 4);
if (-1 == pos)
printf("不存在\n");
else
printf("%d\n", pos);
}
void TestInsert()
{
int pos = 0;
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
pos = Find(&seq, 3);
if (pos != -1)
{
Insert(&seq, pos, 5);
}
PrintfSeqList(&seq);
}
void TestErase()
{
int pos = 0;
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintfSeqList(&seq);
pos = Find(&seq, 3);
if (pos != -1)
{
Erase(&seq, 3);
}
PrintfSeqList(&seq);
}
void TestRemove()
{
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 1);
PrintfSeqList(&seq);
Remove(&seq, 1);
PrintfSeqList(&seq);
}
void TestRemoveAll()
{
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 1);
PrintfSeqList(&seq);
RemoveAll(&seq, 1);
PrintfSeqList(&seq);
}
void TestBubbleSort()
{
SeqList seq;
initSeqList(&seq);
PushBack(&seq, 5);
PushBack(&seq, 4);
PushBack(&seq, 3);
PushBack(&seq, 2);
PushBack(&seq, 1);
PushBack(&seq, 0);
PrintfSeqList(&seq);
BubbleSort(&seq);
PrintfSeqList(&seq);
}
void TestBinarySearch()
{
int i = 0;
int pos = 0;
SeqList seq;
initSeqList(&seq);
for (i = 0; i < 10; i++)
{
PushBack(&seq, i + 1);
}
pos = BinarySearch_R(&seq, 0, seq.sz - 1, 7);
if (pos == -1)
{
printf("找不到\n");
}
else
{
printf("找到了,下标是%d\n", pos);
}
}
void TestSelectSort()
{
int i = 0;
int pos = 0;
SeqList seq;
initSeqList(&seq);
for (i = 10; i > 0; i--)
{
PushBack(&seq, i);
}
PrintfSeqList(&seq);
selectsort(&seq);
PrintfSeqList(&seq);
}
void TestSelectSearch()
{
int i = 0;
int pos = 0;
SeqList seq;
initSeqList(&seq);
for (i = 10; i > 0; i--)
{
PushBack(&seq, i);
}
PrintfSeqList(&seq);
SelectSort_op(&seq);
PrintfSeqList(&seq);
}
int main()
{
Testbushback();
printf("\n");
TestFrontback();
printf("\n");
TestFind();
printf("\n");
TestInsert();
printf("\n");
TestErase();
printf("\n");
TestRemove();
printf("\n");
TestRemoveAll();
printf("\n");
TestBubbleSort();
printf("\n");
TestBinarySearch();
printf("\n");
TestSelectSort();
printf("\n");
TestSelectSearch();
system("pause");
}
#include”pSeqList.c”
#include"pSeqList.h";
void initSeqList(pSeqList pseq)
{
pseq->sz = 0;
DataType* tmp = NULL;
tmp = (DataType*)malloc(INIT_UNM * sizeof(pseq->date));
if (tmp == NULL)
{
perror("malloc error:");
exit(EXIT_FAILURE);
}
pseq->date = tmp;
pseq->capacity = INIT_UNM;
}
void CheckCapacity(pSeqList pseq)
{
if (pseq->sz == pseq->capacity)
{
DataType* tmp = (pSeqList*)realloc(pseq->date,
(pseq->capacity + INIT_UNM)*sizeof(pseq->date));
if (pseq->date== NULL)
{
perror("realloc error:");
exit(EXIT_FAILURE);
}
pseq->date = tmp;
pseq->capacity = pseq->capacity + INIT_UNM;
}
}
void PushBack(pSeqList pSeq, DataType d)
{
assert(pSeq);
CheckCapacity(pSeq);
pSeq->date[pSeq->sz] = d;
pSeq->sz++;
}
void FrontBack(pSeqList pSeq, DataType d)
{
assert(pSeq);
CheckCapacity(pSeq);
for (int i = pSeq->sz - 1; i >= 0; i--)
{
pSeq->date[i + 1] = pSeq->date[i];
}
pSeq->date[0] = d;
pSeq->sz++;
}
void PopFront(pSeqList pseq)
{
assert(pseq);
if (pseq->sz == 0)
{
printf("顺序表为空,无法删除");
return;
}
for (int i = 0; i < pseq->sz - 1; i++)
{
pseq->date[i] = pseq->date[i + 1];
}
pseq->sz--;
}
void PopBack(pSeqList pseq)
{
assert(pseq);
if (pseq->sz == 0)
{
printf("顺序表为空,无法删除\n");
return;
}
return pseq->sz--;
}
void PrintfSeqList(pSeqList pSeq)
{
int i = 0;
if (pSeq->sz == 0)
{
printf("顺序表为空");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
printf("%d ", pSeq->date[i]);
}
printf("\n");
}
int Find(pSeqList pseq, DataType d)
{
int i = 0;
assert(pseq);
for (i = 0; i < pseq->sz; i++)
{
if (pseq->date[i] == d)
{
return i;
}
}
return -1;
}
void Insert(pSeqList pseq, int pos, DataType d)
{
int i = 0;
assert(pseq != NULL && pos >= 0 && pos<pseq->sz);
for (i = pseq->sz - 1; i >= pos; i--)
{
pseq->date[i + 1] = pseq->date[i];
}
pseq->date[pos] = d;
pseq->sz++;
}
void Erase(pSeqList pseq, int pos)
{
int i = 0;
assert(pseq != NULL && pos >= 0 && pos < pseq->sz);
if (pseq->sz == 0)
{
printf("顺序表为空,无法删除");
return;
}
for (i = pos; i < pseq->sz - 1; i++)
{
pseq->date[i] = pseq->date[i + 1];
}
pseq->sz--;
}
void Remove(pSeqList pseq, DataType d)
{
#if 0
int i = 0;
assert(pseq);
for (i = 0; i < pseq->sz; i++)
{
if (pseq->date[i] == d)
{
break;
}
}
if (i == pseq->sz)
{
printf("要删除的元素不存在\n");
return;
}
for (; i < pseq->sz - 1; i++)
{
pseq->date[i] = pseq->date[i + 1];
}
pseq->sz--;
#endif
int i = 0;
int pos = 0;
assert(pseq);
pos = Find(pseq, d);
if (pos != -1)
{
Erase(pseq, pos);
}
}
void RemoveAll(pSeqList pseq, DataType d)
{
#if 0
int i = 0;
assert(pseq);
for (i = 0; i < pseq->sz; i++)
{
if (pseq->date[i] == d)
{
int j = 0;
for (j = i; j < pseq->sz - 1; j++)
{
pseq->date[j] = pseq->date[j + 1];
}
pseq->sz--;
i--;
}
}
#endif
#if 0
int count = 0;
int i = 0;
DataType *tmp = NULL;
assert(pseq);
tmp = malloc(pseq->sz*sizeof(DataType));
if (tmp == NULL)
{
perror(" use malloc");
return;
}
for (i = 0; i < pseq->sz; i++)
{
if (pseq->date[i] != d)
{
tmp[count++] = pseq->date[i];
}
}
memcpy(pseq->date, tmp, count*sizeof(DataType));
pseq->sz = count;
free(tmp);
tmp = NULL;
#endif
assert(pseq);
int i = 0;
int count = 0;
for (i = 0; i < pseq->sz; i++)
{
if (pseq->date[i] != d)
{
pseq->date[count++] = pseq->date[i];
}
}
pseq->sz = count;
}
int Size(pSeqList pseq)
{
return pseq->sz;
}
int Empty(pSeqList pseq)
{
return pseq->sz == 0;
}
void Sawp(DataType* px, DataType*py)
{
DataType tmp = *px;
*px = *py;
*py = tmp;
}
void BubbleSort(pSeqList pseq)
{
int i = 0;
int j = 0;
int flag = 0;
assert(pseq);
for (i = 0; i < pseq->sz - 1; i++)
{
flag = 0;
for (j = 0; j < pseq->sz - 1 - i; j++)
{
if (pseq->date[j] > pseq->date[j + 1])
{
Sawp(pseq->date + j, pseq->date + j + 1);
flag = 1;
}
}
if (flag == 0)
return;
}
}
int BinarySearch(pSeqList pseq, DataType d)
{
int left = 0;
int right = pseq->sz - 1;
assert(pseq);
while (left <= right)
{
int mid = left + (right - left) / 2;
if (pseq->date[mid] > d)
{
right = mid - 1;
}
else if (pseq->date[mid] < d)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
int BinarySearch_R(pSeqList pseq, int left, int right, DataType d)
{
int mid = 0;
if (left > right)
return -1;
mid = left + (right - left) / 2;
if (pseq->date[mid] < d)
{
return BinarySearch_R(pseq, mid + 1, right, d);
}
else if (pseq->date[mid]>d)
{
return BinarySearch_R(pseq, left, mid - 1, d);
}
else
{
return mid;
}
}
void selectsort(pSeqList pseq)
{
assert(pseq);
int i = 0;
int j = 0;
for (i = 0; i < pseq->sz - 1; i++)
{
int MaxPos = 0;
for (j = 0; j<pseq->sz - i; j++)
{
if (pseq->date[j]>pseq->date[MaxPos])
{
MaxPos = j;
}
}
if (MaxPos != pseq->sz - 1 - i)
{
int tmp = pseq->date[MaxPos];
pseq->date[MaxPos] = pseq->date[pseq->sz - 1 - i];
pseq->date[pseq->sz - 1 - i] = tmp;
}
}
}
void SelectSort_op(pSeqList pseq)
{
int i = 0;
int start = 0;
int end = pseq->sz - 1;
while (start < end)
{
int MaxPos = start;
int MinPos = end;
for (i = 1; i < end; i++)
{
if (pseq->date[i]>pseq->date[MaxPos])
{
MaxPos = i;
}
if (pseq->date[i] < pseq->date[MinPos])
{
MinPos = i;
}
}
if (MinPos != start)
{
Sawp(pseq->date + MinPos, pseq->date + start);
}
if (MaxPos == start)
{
MaxPos = MinPos;
}
if (MaxPos != end)
{
Sawp(pseq->date + MaxPos, pseq->date + end);
}
start++;
end--;
}
}
如有错误,还请指出。