#include<stdio.h>
#ifndef __SEQ_LIST__
#define __SEQ_LIST__
#define MIX_SIZE 5
typedef int DataType;
typedef struct SeqList
{
DataType seqList[MIX_SIZE];
size_t size;
}SeqList;
void InitSeqList(SeqList* seq);
void PrintSeqList(SeqList* seq);
void PushBack(SeqList* seq, DataType x);
void PopBack(SeqList* seq);
void PushFront(SeqList* seq, DataType x);
void PopFront(SeqList* seq);
void Insert(SeqList* seq, size_t pos, DataType x);
返回-1表示未找到数据
int Find(SeqList* seq, DataType x);
void Erase(SeqList* seq, size_t pos);
void Remove(SeqList* seq, DataType x);
void RemoveAll(SeqList* seq, DataType x);
void Modify(SeqList* seq, size_t pos, DataType x);
#endif
#include<stdio.h>//函数文件
#include<assert.h>
#include<string.h>
#include"SeqList.h"
void InitSeqList(SeqList* seq)//初始化 默认为零
{
assert(seq);
memset(seq, 0, MIX_SIZE);
seq->size = 0;
}
void PrintSeqList(SeqList* seq)//遍历顺序表
{
assert(seq);
size_t index = 0;
if (seq->size == 0)
{
printf("顺序表为空!\n");
return;
}
for (; index < seq->size ; ++index)
{
printf("%d\n",seq->seqList[index]);
}
}
void PushBack(SeqList* seq, DataType x)//尾插
{
assert(seq);
if (seq->size > MIX_SIZE)
seq->size = 0;
if (seq->size == MIX_SIZE)
{
printf("顺序表已满!\n");
return;
}
seq->seqList[(seq->size)++] = x;
}
void PopBack(SeqList* seq)//尾删
{
assert(seq);
if (seq->size ==0)
{
printf("顺序表已空!\n");
return;
}
--(seq->size);
}
void PushFront(SeqList* seq, DataType x)//头插
{
int end;
assert(seq);
if (seq->size > MIX_SIZE)
seq->size = 0;
if (seq->size == MIX_SIZE)
{
printf("顺序表已满!\n");
return;
}
end = (seq->size)++;
for (; end > 0; --end)
{
seq->seqList[end] = seq->seqList[end - 1];
}
seq->seqList[0] = x;
}
void PopFront(SeqList* seq)//头删
{
size_t start = 0;
assert(seq);
if (seq->size == 0)
{
printf("顺序表已空!\n");
return;
}
for (; start <seq->size; ++start)
{
seq->seqList[start] = seq->seqList[start+1];
}
--seq->size;
}
void Insert(SeqList* seq, size_t pos, DataType x)
{
int end;
assert(seq);
if (seq->size > MIX_SIZE)
seq->size = 0;
if (pos > seq->size)
{
printf("不合法的插入位置!\n");
return;
}
if (seq->size == MIX_SIZE)
{
printf("顺序表已满!\n");
return;
}
end = (seq->size)++;
for (; end >= pos-1; --end)
{
seq->seqList[end] = seq->seqList[end - 1];
}
seq->seqList[pos-1] = x;
}
int Find(SeqList* seq, DataType x)
{
size_t pos = 0;
assert(seq);
for (; pos < seq->size; ++pos)
{
if (seq->seqList[pos] == x)
return pos;
}
return -1;
}
void Erase(SeqList* seq, size_t pos)//按位置删除
{
size_t start = 0;
assert(seq);
if (seq->size == 0)
{
printf("顺序表已空!\n");
return;
}
start = pos - 1;
for (; start <seq->size; ++start)
{
seq->seqList[start] = seq->seqList[start + 1];
}
--seq->size;
}
void Remove(SeqList* seq, DataType x)
{
int tag = 0;
assert(seq);
if (seq->size == 0)
{
printf("顺序表为空!\n");
return;
}
size_t index = 0;
for (; index < seq->size; ++index)
{
if (seq->seqList[index] == x)
{
for (; index < seq->size; ++index)
{
seq->seqList[index] = seq->seqList[index + 1];
}
--seq->size;
tag = 1;
}
}
if (tag == 0)
printf("未找到该元素!\n");
}
void RemoveAll(SeqList* seq, DataType x)
{
size_t pos = 0,start=0;
assert(seq);
if (seq->size == 0)
{
printf("顺序表为空!\n");
return;
}
for (; pos < seq->size; ++pos)
{
if (seq->seqList[pos] == x)
{
for (start = pos; start < seq->size; ++start)
{
seq->seqList[start] = seq->seqList[start + 1];
}
--seq->size;
--pos;
}
}
}
void Modify(SeqList* seq, size_t pos, DataType x)
{
assert(seq);
if (pos > seq->size)
{
printf("不合法的插入位置!\n");
return;
}
seq->seqList[pos-1] = x;
}
#include"SeqList.h"//主函数文件
//void test1(SeqList * seq)
//{
// PushFront(seq, 0);
// PushFront(seq, 1);
// PushFront(seq, 2);
// PushFront(seq, 3);
// PushFront(seq, 3);
// PrintSeqList(seq);
// PopFront(seq);
// PopFront(seq);
// PopFront(seq);
// PrintSeqList(seq);
// PopFront(seq);
// PopFront(seq);
// PrintSeqList(seq);
//}
void test2(SeqList * seq)
{
PushFront(seq, 0);
PushFront(seq, 3);
PushFront(seq, 3);
PushFront(seq, 3);
Insert(seq, 2, 4);
PrintSeqList(seq);
printf("%d\n",Find(seq, 5));
/*Modify(seq, 2, 5);
Erase(seq, 2);*/
PrintSeqList(seq);
RemoveAll(seq, 3);
PrintSeqList(seq);
}
int main()
{
SeqList seq;
/*test1(&seq);*/
test2(&seq);
return 0;
}
C语言静态顺序表的实现
最新推荐文章于 2023-11-29 13:36:42 发布