头文件 SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<stdio.h>
#include<string.h>
#include<assert.h>
#define MAX_SIZE 10
typedef int DataType;
typedef unsigned int size_t;
typedef struct SeqList
{
DataType array[MAX_SIZE];
size_t size;
}SeqList, *PSeqList;
// 初始化顺序表
void InitSeqList(PSeqList seqList);
// 顺序表尾插
// 设计函数原型
// 参数检测
// 边界条件考虑
// 逻辑操作
// 尾插
void PushBack(PSeqList pSeqList, DataType data);
// 尾删
void PopBack(PSeqList pSeqList);
// 打印顺序表
void PrintSeqList(PSeqList pSeqList);
// 头插
void PushFront(PSeqList pSeqList, DataType data);
// 头删
void PopFront(PSeqList pSeqList);
// 任意位置插入
void Insert(PSeqList pSeqList, size_t pos, DataType data);
// 在顺序表中查找元素data
int Find(PSeqList pSeqList, DataType data);
// 删除顺序表中pos位置上的元素
void Erase(PSeqList pSeqList, size_t pos);
//移除顺序表中的元素data
void Remove(PSeqList pSeqList, DataType data);
// 移除顺序表中所有元素data
void RemoveAll(PSeqList pSeqList, DataType data);
#endif
实现函数 SeqList.c
#include "SeqList.h"
void InitSeqList(PSeqList seqList)// 初始化顺序表
{
memset(seqList->array, 0, MAX_SIZE*sizeof(DataType));
seqList->size = 0;
}
// 打印顺序表
void PrintSeqList(PSeqList pSeqList)
{
size_t i = 0;
if (pSeqList->size == 0)
{
printf("线性表为空,打印结束\n");
return;
}
for (i = 0; i < pSeqList->size; i++)
{
printf("%d ", pSeqList->array[i]);
}
printf("\n");
}
void PushBack(PSeqList pSeqList, DataType data)//从尾部插入
{
assert(pSeqList);//判断指针是否为空
if ((pSeqList->size) >= MAX_SIZE)
{
printf("线性表已满,无法继续插入\n");
return;
}
pSeqList->array[pSeqList->size] = data;
pSeqList->size++;
}
void PopBack(PSeqList pSeqList)//从尾部删除
{
assert(pSeqList);
if (pSeqList->size == 0)
{
printf("线性表为空\n");
return;
}
pSeqList->size--;
printf("删除成功\n");
}
void PushFront(PSeqList pSeqList, DataType data)//从首部插入
{
size_t i = pSeqList->size;
assert(pSeqList);//判断指针是否为空
if ((pSeqList->size) >= MAX_SIZE)
{
printf("线性表已满,无法继续插入\n");
return;
}
while (i)
{
pSeqList->array[ i ] = pSeqList->array[i-1];
i--;
}
pSeqList->array[0] = data;
printf("插入完成\n");
pSeqList->size++;
}
void PopFront(PSeqList pSeqList)//删除最前面一个
{
size_t i = 0;
assert(pSeqList);//判断指针是否为空
if (pSeqList->size == 0)
{
printf("线性表为空\n");
return;
}
for (i = 0; i < pSeqList->size-1; i++)
{
pSeqList->array[i] = pSeqList->array[i + 1];
}
pSeqList->size--;
printf("删除完成\n");
}
int Find(PSeqList pSeqList, DataType data)//在顺序表内查找某元素
{
size_t i = 0;
assert(pSeqList);//判断指针是否为空
if (pSeqList->size == 0)
{
printf("线性表为空\n");
return -1;
}
for (i = 0; i < pSeqList->size - 1; i++)
{
if (pSeqList->array[i] == data)
{
return i;
}
}
return -1;
}
void Insert(PSeqList pSeqList, size_t pos, DataType data)//在任意位置插入元素
{
size_t i = pSeqList->size;
assert(pSeqList);//判断指针是否为空
if ((pSeqList->size) >= MAX_SIZE)
{
printf("线性表已满,无法继续插入\n");
return;
}
if (pos > pSeqList->size)
{
printf("插入位置不被允许\n");
return;
}
for (i = pSeqList->size; i >pos; i--)
{
pSeqList->array[i] = pSeqList->array[i - 1];
}
pSeqList->array[pos] = data;
pSeqList->size++;
printf("添加完成\n");
}
// 删除顺序表中pos位置上的元素
void Erase(PSeqList pSeqList, size_t pos)
{
size_t i = pos;
assert(pSeqList);//判断指针是否为空
if (pSeqList->size == 0)
{
printf("线性表为空\n");
return;
}
if (pos > pSeqList->size)
{
printf("删除位置有误\n");
return;
}
for (i = pos; i < pSeqList->size-1; i++)
{
pSeqList->array[i] = pSeqList->array[i + 1];
}
pSeqList->size--;
printf("删除完成\n");
}
void RemoveAll(PSeqList pSeqList, DataType data)
{
size_t i = 0;
assert(pSeqList);//判断指针是否为空
if (pSeqList->size == 0)
{
printf("线性表为空\n");
return;
}
for (i = 0; i < pSeqList->size; i++)
{
if (pSeqList->array[i] == data)
{
size_t j = i;
while (j< pSeqList->size-1)
{
pSeqList->array[j] = pSeqList->array[j + 1];
j++;
}
pSeqList->size--;
printf("已删除一个元素\n");
i--;
}
}
printf("删除完成\n");
}
void Remove(PSeqList pSeqList, DataType data)
{
size_t i = 0;
assert(pSeqList);//判断指针是否为空
i = Find(pSeqList, data);
if (i >= 0)
{
while (i < pSeqList->size - 1)
{
pSeqList->array[i] = pSeqList->array[i + 1];
i++;
}
pSeqList->size--;
printf("删除完成\n");
return;
}
else
{
printf("没有找到该元素");
return;
}
}
测试程序 test.c (可分三次测试)
#include "SeqList.h"
static void test1(PSeqList seqList)
{
InitSeqList(seqList);
PushBack(seqList, 1);
PushBack(seqList, 2);
PushBack(seqList, 3);
PushBack(seqList, 4);
PrintSeqList(seqList);
PopBack(seqList);
PrintSeqList(seqList);
}
static void test2(PSeqList seqList)
//测试PushFront ,PrintSeqList,PopFront等函数
{
int ret = 0;
InitSeqList(seqList);
PushFront(seqList, 1);
PushFront(seqList, 2);
PushFront(seqList, 3);
PushFront(seqList, 4);
PrintSeqList(seqList);
PopFront(seqList);
PopFront(seqList);
PopFront(seqList);
PopFront(seqList);
PopFront(seqList);
PrintSeqList(seqList);
}
static void test3(PSeqList seqList)//测试Insert,Erase,Remove,RemoveALL函数
{
int ret = 0;
InitSeqList(seqList);
PushFront(seqList, 1);//在首部加入 1
PushFront(seqList, 2);
PushFront(seqList, 3);
PushFront(seqList, 4);
Insert(seqList, 3, 5);//在下标3的位置插入 5
PrintSeqList(seqList);
Erase(seqList, 3);//删除下标3位置上的元素
PrintSeqList(seqList);
Insert(seqList, 3, 2);
Insert(seqList, 3, 2);
PrintSeqList(seqList);
Remove(seqList, 2);//删除第一个 2
PrintSeqList(seqList);
RemoveAll(seqList, 2);//删除所有的 2
PrintSeqList(seqList);
}
static void test4(PSeqList seqList)//测试Find函数
{
int ret = 0;
InitSeqList(seqList);
PushFront(seqList, 1);
PushFront(seqList, 2);
PushFront(seqList, 3);
PushFront(seqList, 4);
ret = Find(seqList, 2);
printf("%d", ret);
}
int main()
{
SeqList seqList;
/*test1(&seqList);*/
/*test2(&seqList);*/
test3(&seqList);
/*test4(&seqList);*/
system("pause");
return 0;
}
时间: 2024-10-11 10:27:51