seqlist.h
//头文件,存放函数的声明
#pragma once// 防止头文件被重复引用
#define SIZE 10//顺序表的总格子数
typedef struct SeqList//定长顺序表
{
int elem[10];
int length;//有效数据个数
}SeqList,*PSeqList;//44
//typedef SeqList *PSeqList;
//初始化顺序表ps
void InitSeqList(PSeqList ps);//PSeqList == SeqList *;
//在顺序表ps中的第pos位置插入val
bool Insert(PSeqList ps,int pos,int val);
//在顺序表ps中查找key,如果找到返回下标,没有找到返回-1
int Search(PSeqList ps,int key);
//在ps中删除关键字key,成功返回true
bool DeleteVal(PSeqList ps,int key);
//删除第pos位置的值
bool DeletePos(PSeqList ps,int pos);
//获取pos位置的值
bool GetElem(PSeqList ps,int pos,int *rtval);
//将pos位置的值设为val
bool SetElem(PSeqList ps,int pos,int val);
//获取关键字key的前驱值
//rtval:输出参数
bool GetPrio(PSeqList ps,int key,int *rtval);
//获取关键字key的后继值
bool GetNext(PSeqList ps,int key,int *rtval);
//获取顺序表的有效长度
int GetLength(PSeqList ps);
//判断ps是否为空
bool IsEmpty(PSeqList ps);
//将ps数据清空
void Clear(PSeqList ps);
//销毁ps
void Destroy(PSeqList ps);
//打印顺序表
void Show(PSeqList ps);
源文件
//源文件:函数的实现
#include "seqlist.h"
#include <stdio.h>
#include <assert.h>
/*线性表
1、顺序表(逻辑相邻,物理也相邻)。一维数组可以看做是特殊的顺序表
2、链表(逻辑相邻,物理不一定相邻)
*/
//初始化顺序表ps
void InitSeqList(PSeqList ps)
{
assert(ps!=NULL);
if(ps==NULL)
{
return;
}
ps->length=0;//有效数据个数为0
}
//判断顺序表是否为满
static bool IsFull(PSeqList ps)
{
return ps->length==SIZE;
}
//在顺序表ps中的第pos位置插入val
bool Insert(PSeqList ps,int pos,int val)
{
if(pos<0||pos>ps->length||IsFull(ps))
{
return false;
}
for(int i=ps->length-1;i>=pos;i--)//1、移动后面的数据
{
ps->elem[i+1]=ps->elem[i];
}
ps->elem[pos]=val;//2、插入
ps->length++;//3、长度+1
return true;
}
//在顺序表ps中查找key,如果找到返回下标,没有找到返回-1
int Search(PSeqList ps,int key)
{
for(int i=0;i<ps->length;i++)
{
if(ps->elem[i]==key)
{
return i;
}
}
return -1;
}
//在ps中删除关键字key,成功返回true
bool DeleteVal(PSeqList ps,int key)
{
int index=Search(ps,key);
return DeletePos(ps,index);
}
//删除第pos位置的值
bool DeletePos(PSeqList ps,int pos)
{
if(pos<0||pos>=ps->length)
{
return false;
}
for(int i=pos;i<ps->length-1;i++)
{
ps->elem[i]=ps->elem[i+1];
}
ps->length--;
return true;
}
//获取pos位置的值
bool GetElem(PSeqList ps,int pos,int *rtval)
{
if(pos<0||pos>ps->length)
{
return false;
}
*rtval=ps->elem[pos];
return true;
}
//将pos位置的值设为val
bool SetElem(PSeqList ps,int pos,int val)
{
if(pos<0||pos>ps->length)
{
return false;
}
ps->elem[pos]=val;
return true;
}
//获取关键字key的前驱值
bool GetPrio(PSeqList ps,int key,int *rtval)
{
int index=Search(ps,key);
if(index<=0)
{
return false;
}
*rtval=ps->elem[index-1];
return true;
}
//获取关键字key的后继值
bool GetNext(PSeqList ps,int key,int *rtval)
{
int index=Search(ps,key);
if(index<0&&index>=ps->length-1)
{
return false;
}
*rtval=ps->elem[index+1];
return true;
}
//获取顺序表的有效长度
int GetLength(PSeqList ps)
{
if(ps==NULL)
{
return -1;
}
return ps->length;
}
//判断ps是否为空
bool IsEmpty(PSeqList ps)
{
if(ps->length==0)
{
return true;
}
return false;
}
//将ps数据清空
void Clear(PSeqList ps)
{
ps->length=0;
printf("%d ",ps->length);
printf("\n");
}
//销毁ps
void Destroy(PSeqList ps)
{
Clear(ps);
printf("%d ",ps);
printf("\n");
}
//打印顺序表
void show(PSeqList ps)
{
for(int i=0;i<ps->length;i++)
{
printf("%d ",ps->elem[i]);
}
printf("\n");
}
测试函数
int main()
{
SeqList s;
InitSeqList(&s);
for(int i=0;i<10;i++)
{
Insert(&s,i,i);
}
show(&s);
printf("%d\n",Search(&s,4));
printf("%d\n",Search(&s,2));
printf("%d\n",Search(&s,5));
DeletePos(&s,2);
DeletePos(&s,5);
show(&s);
DeleteVal(&s,5);
show(&s);
int a;
if(GetElem(&s,5,&a))
{
printf("第5位的值为%d\n",a);
}
SetElem(&s,5,2);
show(&s);
if(GetPrio(&s,2,&a))
{
printf("2的前驱为%d\n",a);
}
if(GetNext(&s,2,&a))
{
printf("2的后继为%d\n",a);
}
printf("%d\n",GetLength(&s));
printf("%d\n",IsEmpty(&s));
Clear(&s);
Destroy(&s);
}
结果