Seqlist.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
#define N 100
typedef struct Seqlist
{
int arr[N];
int size;
}Seqlist;
//初始化顺序表
void SeqlistInit(Seqlist*psl);
//增加(头插)
void SeqlistPushFront(Seqlist*psl, DataType data);
//增加(尾插)
void SeqlistPushBack(Seqlist*psl, DataType data);
//删除(头删)
void SeqlistDelFront(Seqlist*psl);
//删除(尾删)
void SeqlistDelBack(Seqlist*psl);
//增加(随机插)
void SeqlistPush(Seqlist*psl, int pos, DataType data);
//删除(随机删)
void SeqlistDel(Seqlist*psl, int pos);
//查找(找到第一次命中的返回下标,没找到返回-1)
int SeqlistFind(Seqlist*psl, DataType data);
//删除(删除第一次命中的值)
int SeqlistRemove(Seqlist*psl, DataType data);
//删除(删除所有命中的值)
int SeqlistRemoveAll(Seqlist*psl, DataType data);
//修改(修改第一次命中的值)
void SeqlistXG(Seqlist*psl, DataType data, DataType newdata);
//排序
void SeqlistBubbleSort(Seqlist*psl);
//打印
void SeqlistPrint(Seqlist*psl);
void TestSeqlist();
#endif //__SEQLIST_H__
Seqlist.c
#include "Seqlist.h"
//初始化顺序表
void SeqlistInit(Seqlist*psl)
{
memset(psl->arr, 0, N*sizeof(DataType));
psl->size = 0;
}
//增加(头插)
void SeqlistPushFront(Seqlist*psl, DataType data)
{
assert(psl != NULL); //断言
//assert(psl->size > 0);
if (psl->size == N) //判断是否已满
{
printf("顺序表已满\n");
return;
}
int i = 0;
for (i = psl->size - 1; i >= 0; i--) //从后往前覆盖
{
psl->arr[i + 1] = psl->arr[i];
}
psl->arr[0] = data;
psl->size++;
printf("增添成功\n");
}
//增加(尾插)
void SeqlistPushBack(Seqlist*psl, DataType data)
{
assert(psl != NULL); //断言
assert(psl->size > 0);
if (psl->size == N) //判断是否已满
{
printf("顺序表已满\n");
return;
}
//尾插
psl->arr[psl->size] = data;
psl->size++;
printf("增添成功\n");
}
//删除(头删)
void SeqlistDelFront(Seqlist*psl)
{
assert(psl != NULL);
assert(psl->size > 0);
int i = 0;
for (i = 0; i <psl->size - 1; i++)
{
psl->arr[i] = psl->arr[i + 1];
}
psl->size--;
printf("删除成功\n");
}
//删除(尾删)
void SeqlistDelBack(Seqlist*psl)
{
assert(psl != NULL);
assert(psl->size > 0);
psl->size--;
printf("删除成功\n");
}
//增加(随机插)
void SeqlistPush(Seqlist*psl, int pos, DataType data)
{
assert(psl != NULL);
assert(pos >= 0 && pos<psl->size);
if (psl->size == N) //判断是否已满
{
printf("顺序表已满\n");
return;
}
int i = 0;
for (i = psl->size - 1; i >= pos; i--)//需注意循环条件i>=pos
{
psl->arr[i + 1] = psl->arr[i];
}
psl->arr[pos] = data;
psl->size++;
printf("增添成功\n");
}
//删除(随机删)
void SeqlistDel(Seqlist*psl, int pos)
{
assert(psl != NULL);
assert(pos >= 0 && pos<psl->size);
assert(psl->size > 0);
int i = 0;
for (i = pos; i <psl->size - 1; i++) //注意循环条件
{
psl->arr[i] = psl->arr[i + 1];
}
psl->size--;
printf("删除成功\n");
}
//查找(找到第一次命中的返回下标,没找到返回-1)
int SeqlistFind(Seqlist*psl, DataType data)
{
assert(psl != NULL);
assert(psl->size > 0);
int i = 0;
for (i = 0; i <= psl->size - 1; i++)
{
if (psl->arr[i] == data)
return i; //找到了,下标为i
}
return -1; //找不到,返回-1
}
//删除(删除第一次命中的值)
int SeqlistRemove(Seqlist*psl, DataType data)
{
int ret = 0;
ret = SeqlistFind(psl, data);
if (ret != -1);
{
SeqlistDel(psl, ret);
printf("删除成功\n");
}
}
//删除(删除所有命中的值)
int SeqlistRemoveAll(Seqlist*psl, DataType data)
{
int i = 0;
for (i = 0; i <= psl->size - 1; i++)
{
if (SeqlistFind(psl, data) != -1)
{
SeqlistDel(psl, SeqlistFind(psl, data));
}
}
}
void SeqlistXG(Seqlist*psl, DataType data, DataType newdata)
{
int ret = 0;
ret = SeqlistFind(psl, data);
if (ret != -1);
{
psl->arr[ret] = newdata;
printf("修改成功\n");
}
}
//排序
void SeqlistBubbleSort(Seqlist*psl)
{
int i = 0;
DataType tmp = 0;
int end = psl->size;
while (end > 1)
{
int exchange = 0;
//单趟排序,冒到end之前
for (i = 0; i < end-1; i++)
{
if ((psl->arr[i])>(psl->arr[i + 1]))
{
tmp = psl->arr[i];
psl->arr[i] = psl->arr[i + 1];
psl->arr[i + 1] = tmp;
int exchange = 1;
}
}
if (exchange == 0)
{
break;
}
}
}
void SeqlistPrint(Seqlist*psl)
{
int i = 0;
for (i = 0; i <= psl->size - 1; i++)
{
printf("%d", psl->arr[i]);
}
printf("\n");
}
void TestSeqlist()
{
Seqlist s1;
SeqlistInit(&s1);
SeqlistPushFront(&s1, 8);
SeqlistPushFront(&s1, 3);
SeqlistPushFront(&s1, 6);
SeqlistPushFront(&s1, 6);
SeqlistPushFront(&s1, 1);
SeqlistPushFront(&s1, 10);
SeqlistPrint(&s1);//头插
SeqlistPushBack(&s1, 1);
SeqlistPushBack(&s1, 2);
SeqlistPushBack(&s1, 5);
SeqlistPushBack(&s1, 3);
SeqlistPushBack(&s1, 7);
SeqlistPushBack(&s1, 5);
SeqlistPrint(&s1);//尾插
SeqlistDelBack(&s1);//尾删
SeqlistDelFront(&s1);//头删
SeqlistPrint(&s1);//查找
printf("%d\n", SeqlistFind(&s1, 3));//找到打印第一次匹配的下标,未找到打印-1
SeqlistDel(&s1, 3);//随机删
SeqlistPush(&s1, 6, -1);//随机插
SeqlistPrint(&s1);
SeqlistRemove(&s1, 5);
SeqlistPrint(&s1);
SeqlistRemoveAll(&s1, 6);
SeqlistPrint(&s1);
SeqlistXG(&s1, 3, 5); //修改
SeqlistPrint(&s1);
SeqlistBubbleSort(&s1); //排序
SeqlistPrint(&s1);
}
test.c
#include "Seqlist.h"
int main()
{
TestSeqlist();
system("pause");
return 0;
}