之前用C语言实现了静态顺序表,那么现在我来介绍一下动态动态顺序表是如何实现的。
1、动态顺序表将静态顺序表的数组数据类型改为指针类型;
2、能够实现增容过程,当顺序表中容量不足时,能够自动开辟空间;(malloc使用后,一定要free)
3、 同时还实现了
void Checkcapacity(pSeqList ps);//检查是否需要增容
void DestroySeqList(pSeqList ps);//销毁
具体实现代码如下:
SeqListD.h
#ifndef __SEQLISTD_H__
#define __SEQLISTD_H__
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#define DEFAULT 2
#define INC 2
typedef int DataType;
typedef struct SeqList
{
int sz;
DataType *data;
int capacity;
}SeqList, *pSeqList;
void InitSeqList(pSeqList ps);//初始化
void Checkcapacity(pSeqList ps);//检查是否需要增容
void DestroySeqList(pSeqList ps);//销毁
void PushBack(pSeqList ps, DataType d);//尾部添加
void PopBack(pSeqList ps);//尾部删除
void Display(const pSeqList ps);//打印
void PushFront(pSeqList ps, DataType d);//头部添加
void PopFront(pSeqList ps);//头部删除
int Find(pSeqList ps, DataType d);//查找
void Insert(pSeqList ps, DataType d, int pos);//在顺序表某位置插入某个元素
void Remove(pSeqList ps, DataType d);//删除某一个特定元素
void RemoveAll(pSeqList ps, DataType d);//删除顺序表中出现的所有的特定元素
void Reverse(pSeqList ps);//逆序
void Sort(pSeqList ps);//排序(升序)
int BinarySearch(pSeqList ps, DataType d);//二分查找
#endif//__SEQLIST_H_
SeqListD.c
#include "SeqListD.h"
//初始化
void InitSeqList(pSeqList ps)
{
ps->sz = 0;
ps->data = malloc(DEFAULT*sizeof(DataType));
if (ps->data == NULL)
{
perror("use mallic");
exit(EXIT_FAILURE);
}
memset(ps->data, 0, sizeof(ps->data));
ps->capacity = DEFAULT;
}
void Checkcapacity(pSeqList ps)
{
if (ps->sz == ps->capacity)
{
DataType*ptr = realloc(ps->data, sizeof(DataType)*(ps->capacity + INC));
if (ptr == NULL)
{
perror("use realloc");
exit(EXIT_FAILURE);
}
else
{
ps->data = ptr;
}
ps->capacity += INC;
}
}
//尾部添加
void PushBack(pSeqList ps, DataType d)
{
assert(ps != NULL);
Checkcapacity(ps);
ps->data[ps->sz] = d;
ps->sz++;
}
//尾部删除
void PopBack(pSeqList ps)
{
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
ps->sz--;
}
//打印
void Display(const pSeqList ps)
{
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
printf("%d ", ps->data[i]);
}
printf("\n");
}
//头部添加
void PushFront(pSeqList ps, DataType d)
{
assert(ps != NULL);
Checkcapacity(ps);
memmove(ps->data + 1, ps->data, sizeof(DataType)*(ps->sz));
ps->data[0] = d;
ps->sz++;
}
//头部删除
void PopFront(pSeqList ps)
{
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
for (i = 0; i < ps->sz - 1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
//查找
int Find(pSeqList ps, DataType d)
{
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
if (ps->data[i] == d)
{
return i;
}
}
return -1;
}
//
void Insert(pSeqList ps, DataType d, int pos)
{
int i = 0;
assert(ps != NULL);
Checkcapacity(ps);
memmove(ps->data + pos + 1, ps->data + pos, (ps->sz - pos)*sizeof(DataType));
ps->data[pos] = d;
ps->sz++;
}
void Remove(pSeqList ps, DataType d)
{
assert(ps != NULL);
int pos = Find(ps, d);
if (pos != -1)
{
memmove(ps->data + pos, ps->data + pos + 1, (ps->sz - pos - 1)*sizeof(DataType));
ps->sz--;
}
}
void RemoveAll(pSeqList ps, DataType d)
{
int pos = 0;
assert(ps != NULL);
while ((pos = Find(ps, d)) != -1)
{
Remove(ps, d);
}
}
void Reverse(pSeqList ps)
{
int left = 0;
int right = ps->sz - 1;
DataType tmp = 0;
if (ps->sz == 0)
{
return;
}
while (left < right)
{
tmp = ps->data[left];
ps->data[left] = ps->data[right];
ps->data[right] = tmp;
left++;
right--;
}
}
void Sort(pSeqList ps)
{
int i = 0;
int j = 0;
assert(ps);
if (ps->sz == 0)
{
return;
}
for (i = 0; i < ps->sz - 1; i++)
{
for (j = 0; j < ps->sz - 1 - i; j++)
{
if (ps->data[j]>ps->data[j + 1])
{
DataType tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
}
}
}
}
int BinarySearch(pSeqList ps, DataType d)
{
int left = 0;
int right = ps->sz;
int mid = ((left + right ) >> 1);
assert(ps);
while (left <= right)
{
if (ps->data[mid] < d)
{
left = mid + 1;
}
else if (ps->data[mid] > d)
{
right = mid - 1;
}
else
return mid;
}
return -1;
}
void DestroySeqList(pSeqList ps)
{
free(ps->data);
ps->data = NULL;
ps->sz = 0;
ps->capacity = 0;
}
test.c
#include"SeqListD.h"
void test1()
{
SeqList my_list;
InitSeqList(&my_list);
PushBack(&my_list, 1);
PushBack(&my_list, 2);
PushBack(&my_list, 3);
PushBack(&my_list, 4);
Display(&my_list);
PopBack(&my_list);
Display(&my_list);
DestroySeqList(&my_list);
}
void test2()
{
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 2);
PushFront(&my_list, 4);
PushFront(&my_list, 5);
Display(&my_list);
RemoveAll(&my_list, 2);
Display(&my_list);
DestroySeqList(&my_list);
}
void test3()
{
SeqList my_list;
InitSeqList(&my_list);
PushBack(&my_list, 1);
PushBack(&my_list, 5);
PushBack(&my_list, 4);
PushBack(&my_list, 2);
PushBack(&my_list, 3);
Display(&my_list);
//Insert(&my_list, 2, 4);
Sort(&my_list);
Display(&my_list);
}
void test4()
{
SeqList my_list;
int ret = 0;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 4);
PushFront(&my_list, 5);
Display(&my_list);
//Remove(&my_list,2);
//Reverse(&my_list);
ret = BinarySearch(&my_list, 3);
printf("%d\n", ret);
}
int main()
{
test4();
system("pause");
return 0;
}