动态顺序表的实现:
头文件 SeqList.h
#ifndef __SeqList__
#define __SeqList__
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
typedef int DataType;
typedef struct SeqList
{
DataType* array;
size_t size;
size_t capacity; //容量
}SeqList;
void InitSeqList(SeqList* seq);
void PrintSeqList(SeqList* seq);
void CheckCapacity(SeqList* seq);
void PushBack(SeqList* seq, DataType x);
void PopBack(SeqList* seq, DataType x);
void PushFront(SeqList* seq, DataType x);
void PopFront(SeqList* seq, DataType x);
void Insert(SeqList* seq, size_t pos, DataType x);
void Find(SeqList* seq, DataType x);
void Erase(SeqList* seq, size_t pos);
void Remove(SeqList* seq, DataType x);
void Removeall(SeqList* seq, DataType X);
#endif
主要功能函数的实现 SeqList.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include "SeqList.h" //顺序表的初始化 void InitSeqList(SeqList* seq) { assert(seq); seq->capacity = 3; //容量小 方便测试 seq->array = (DataType*)malloc(sizeof(DataType)*seq->capacity); assert(seq->array); memset(seq->array, 0, sizeof(DataType)* 3); seq->size = 0; } //打印顺序表 void PrintSeqList(SeqList* seq) { assert(seq); for (int i = 0; i < seq->size; i++) { printf("%d ", seq->array[i]); } printf("\n"); } //判断容量是否足够 并且开辟空间 void CheckCapacity(SeqList* seq) { if (seq->size >= seq->capacity) { DataType* tmp; seq->capacity *= 2; tmp = (DataType*)malloc(sizeof(DataType)*seq->capacity); assert(tmp); memcpy(tmp, seq->array, sizeof(DataType)*seq->size); free(seq->array); seq->array = tmp; } } //从尾部插入元素 void PushBack(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); seq->array[seq->size] = x; seq->size++; } //从尾部删除元素 void PopBack(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); if (seq->size == 0) { printf("顺序表为空!"); return; } seq->size--; } //从头部插入元素 void PushFront(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); for (int i = seq->size - 1; i >= 0; i--) { seq->array[i + 1] = seq->array[i]; } seq->array[0] = x; seq->size++; } //从头部删除元素 void PopFront(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); if (seq->size == 0) { printf("该顺序表为空!"); return; } for (int i = 0; i <= seq->size - 1; i++) { seq->array[i] = seq->array[i + 1]; } seq->size--; } //指定位置插入一个元素x void Insert(SeqList* seq, size_t pos, DataType x) { assert(seq); CheckCapacity(seq); if (pos > seq->size) { printf("输入的位置不合法"); return; } else { for (int i = seq->size; i > pos - 1; i--) { seq->array[i] = seq->array[i - 1]; } seq->array[pos - 1] = x; } seq->size++; } //在顺序表查找x元素 void Find(SeqList* seq, DataType x) { assert(seq); for (int i = seq->size - 1; i >= 0; i--) { if (seq->array[i] == x) { printf("找到该元素!"); return; } } printf("没有找到该元素!"); } //删除顺序表pos位置上的元素 void Erase(SeqList* seq, size_t pos) { assert(seq); assert(pos <= seq->size); CheckCapacity(seq); for (int i = pos - 1; i < seq->size; i++) { seq->array[i] = seq->array[i + 1]; } seq->size--; } //在顺序表中删除x元素 void Remove(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); for (int i = 0; i < seq->size; i++) { if (seq->array[i] == x) { for (int pos = i; pos < seq->size; pos++) { seq->array[pos] = seq->array[pos + 1]; } seq->size--; return; } } printf("该顺序表中没有这个元素!\n"); } //在顺序表中删除所有为x的元素 void Removeall(SeqList* seq, DataType x) { assert(seq); CheckCapacity(seq); int count = 0; for (int i = 0; i < seq->size; i++) { if (seq->array[i] == x) { count++; } else { if (count == 0) { } else { seq->array[i - count] = seq->array[i]; } } } seq->size -= count; }主函数的实现 Test.cpp
#define _CRT_SECURE_NO_WARNINGS 1 #include "SeqList.h" void Test1() { SeqList data; InitSeqList(&data); PushBack(&data, 1); PushBack(&data, 2); PushBack(&data, 3); PushBack(&data, 4); PushBack(&data, 5); PushBack(&data, 6); PushBack(&data, 7); PushBack(&data, 8); Insert(&data, 2, 2); //Find(&data,2); //Erase(&data, 3); //Remove(&data, 2); //Removeall(&data, 2); PrintSeqList(&data); } void Test() { SeqList data; InitSeqList(&data); PushBack(&data, 1); PushBack(&data, 2); PushBack(&data, 3); PushBack(&data, 4); PrintSeqList(&data); } int main() { // Test(); Test1(); system("pause"); return 0; }