静态:
seqlist.h
#pragma once//防止头文件多次引用
#define MAX_SIZE 100
typedef struct seqlist
{
int arry[MAX_SIZE];
int size;//有效数据的长度
}seqlist;
void InitSeqList(seqlist *seq);//初始化
bool InsertSeqlist(seqlist *seq, int pos, int val);//在 pos 位置插入 val 值
int SearchSeqlist(seqlist *seq, int pos, int key);//查找 key 值
bool DeletePos(seqlist *seq, int pos, int *rtv);//删除 pos 位置的值
bool Delete(seqlist *seq,int pos,int key);//删除一个 key 值
bool GetArryelem(seqlist *seq, int pos, int *rtv);//得到pos位置的值
int GetLength(seqlist *seq);//得到长度
void Clear(seqlist *seq);//清空
void SeqListPrint(seqlist *seq);//打印
seqlist.cpp
#include"stdafx.h"
#include"seqlist.h"
#include<stdio.h>
#include<assert.h>
void InitSeqlist(seqlist *seq)//初始化
{
assert(seq!=NULL);
if(seq==NULL)
{
return;
}
seq->size=0;
}
static bool Isfull(seqlist *seq)
{
return seq->size=MAX_SIZE;
}
bool InsertSeqlist(seqlist *seq,int pos,int val)//在pos位置插入val的值
{
assert(seq!=NULL);
if(seq==NULL||Isfull(seq))
{
return false;
}
for(int i=seq->size-1;i>=pos;i--)
{
seq->arry[i+1]=seq->arry[i];
}
seq->arry[pos]=val;
seq->size++;
return true;
}
int Searchseqlist(seqlist *seq,int pos,int key)//从pos位置开始,查找等于key的值,返回其下标
{
assert(seq!=NULL);
if(pos<0||pos>=seq->size)
{
return -1;
}
for(int i=pos;i<=seq->size-1;i++)
{
if(seq->arry[i]==key)
{
return i;
}
}
return -1;
}
bool Deletpos(seqlist *seq,int pos,int *rtv)//删除pos位置的值,存放在*rtv中
{
assert(seq!=NULL);
if(pos<0||pos>=seq->size)
{
return false;
}
*rtv=seq->arry[pos];
for(int i=pos;i<=eq->size-1;i++)
{
seq->arry[i]=seq->arry[i+1];
}
seq->size--;
return true;
}
bool Delet(seqlist *seq,int pos,int *rtv)//删除一个从pos位置开始等于key的值
{
assert(seq!=NULL);
if(pos<0||pos>=seq->size)
{
return false;
}
int index=Searchseqlist(seq,pos,key);
int tmp=0;
Deletpos(seq,index,&tmp);
return true;
}
bool GetArryelem(seqlist *seq,int pos,int *rtv)//得到pos位置的值
{
assert(seq!=NULL);
if(pos<0||pos>=seq->size||rtv==NULL)
{
return false;
}
*rtv=seq->arry[pos];
return true;
}
int Getlength(seqlist *seq)
{
return seq->size;
}
void Clear(seqlist *seq)
{
return seq->size=0;
}
void Show(seqlist *seq)
{
assert(seq!=NULL);
if(seq==NULL)
{
return;
}
for(int i=0;i<seq->size;i++)
{
printf("%d ",seq->arry[i]);
}
printf("\n");
}
顺序表.cpp
#include "stdafx.h"
#include "seqlist.h"
int main()
{
seqlist seq;
InitSeqList(&seq);
//验证插入函数
//InsertSeqlist(&seq,0,10);
for (int i = 0; i < 10; i++)
{
InsertSeqlist(&seq, i, i);
}
SeqListPrint(&seq);
//验证查找函数
int index = SearchSeqlist(&seq, 0, 8);
printf("index==%d\n", index);
//验证删除pos位置函数
int tmp = 0;
DeletePos(&seq, 6, &tmp);
printf("Deletedata==%d\n", tmp);
SeqListPrint(&seq);
//验证删除key值的函数
Delete(&seq, 1, 3);
SeqListPrint(&seq);
//验证得到pos位置的值
int tmp1 = 0;
GetArryelem(&seq, 2, &tmp1);
printf("Getdata==%d\n", tmp1);
//验证清理
Clear(&seq);
return 0;
}
动态:
seqlist.h
#pragma once
#define LIST_INIT_SIZE 10
typedef struct Sqlist
{
int *elem;//存储数据的内存
int usedsize;//有效数据个数
int size;//总单元数
}SqList;
void InitList(SqList *L);//初始化顺序表
int ListLength(SqList *L);//顺序表长度
bool Insert(SqList *L, int pos, int val);//在pos位置插入val值
int Search(SqList *L, int pos, int key);//查找 key 值
bool DeletePos(SqList *L, int pos, int *rtv);//删除 pos 位置的值
bool Delete(SqList *L, int pos, int key);//删除一个 从pos位置开始的key 值
void Clear(SqList *L);
void Destroy(SqList *L);
void Show(SqList *L);
seqlist.cpp
#include "stdafx.h"
#include "seqlist.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
void InitList(sqlist *L)//初始化
{
assert(L!=NULL);
if(L==NULL)
{
return;
}
L->elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
L->usedsize=0;
L->size=LIST_INIT_SIZE;
}
static bool Isfull(sqlist *L)
{
return L->usedsize==L->size;
}
static void Inc(sqlist *L)
{
L->elem=(int *)realloc(L->elem,L->size*sizeof(int)*2);
L->size*=2;
}
//在pos位置插入val值
bool Insert(sqlist *L,int pos,int val)
{
assert(L!=NULL);
if(L=NULL)
{
return false;
}
if(Isfull(L)
{
Inc(L);
}
for(int i=L->usedsize-1;i>=pos;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[pos]=val;
L->usedsize++;
return true;
}
//查找 key 值
int Search(SqList *L, int pos, int key)
{
assert(L != NULL);
if (pos<0 || pos >= L->usedsize)
{
return -1;
}
for (int i = pos; i < L->usedsize; i++)
{
if (L->elem[i] == key)
{
return i;
}
}
return -1;
}
//删除 pos 位置的值
bool DeletePos(SqList *L, int pos, int *rtv)
{
assert(L != NULL);
if (pos<0 || pos >= L->usedsize)
{
return false;
}
*rtv = L->elem[pos];
for (int i = pos; i < L->usedsize - 1; i++)
{
L->elem[i] = L->elem[i + 1];
}
L->usedsize--;
return true;
}
////删除一个 从pos位置开始的key 值
bool Delete(SqList *L, int pos, int key)
{
assert(L != NULL);
if (pos < 0 || pos >=L->size)
{
return false;
}
int index = Search(L, pos, key);
int tmp = 0;
DeletePos(L, index, &tmp);
return true;
}
void Clear(SqList *L)
{
L->usedsize = 0;
}
void Destroy(SqList *L)
{
free(L->elem);
L->elem = NULL;
L->usedsize = 0;
L->size = 0;
}
void Show(SqList *L)
{
assert(L!= NULL);
if (L== NULL)
{
return;
}
for (int i = 0; i < L->usedsize; i++)
{
printf("%d ", L->elem[i]);
}
printf("\n");
}
顺序表.cpp
#include "stdafx.h"
#include "seqlist.h"
int main()
{
SqList L;
InitList(&L);
//验证插入函数
for (int i = 0; i < 20; i++)
{
Insert(&L, i, i);
}
//Insert(&L, 0, 10);
Show(&L);
//验证查找函数
int index = Search(&L, 0, 8);
printf("index==%d\n", index);
//验证删除pos位置函数
int tmp = 0;
DeletePos(&L, 6, &tmp);
printf("Deletedata==%d\n", tmp);
Show(&L);
//验证删除key值的函数
Delete(&L, 1, 3);
Show(&L);
//验证清理
Clear(&L);
//验证摧毁
Destroy(&L);
return 0;
}