/* 动态的循序表的操作集合*/
/* 数据的结构
struct seqlist
{
int data[Maxsize];
int cur;
int Maxsize;
}
*/
#include<stdio.h>
#include<stdlib.h>
#include"seqlist_operation.h"
// initialization
PSeqL ini (int maxsize)
{
PSeqL p;
if( (p = (PSeqL )malloc((3)*sizeof(int))) == NULL)
return NULL;
if( (p->data = (int *)malloc(maxsize * sizeof(int))) == NULL)
return NULL;
p->cur = 0;
p->Maxsize = maxsize;
return p;
}
// sequential list inputed data
void Input(int x, const PSeqL const p)
{
if(IsFull(p))
return ;
p->data[p->cur] = x;
p->cur++;
}
// whether sequential list is empty; is return 1; no return 0;
int IsEmpty(const PSeqL const p)
{
if(0 == p->cur)
return 1;
else
return 0;
}
// whether sequential list is full
int IsFull(const PSeqL const p)
{
if(p->cur == p->Maxsize)
return 1;
else
return 0;
}
// search a data in the list
int Search(const int x,const PSeqL const p)
{
int site;
for(site = 0; site<p->Maxsize ; site++)
{
if(x == p->data[site])
return (site+1);
}
return 0;
}
// get the length of the list
int Length(const PSeqL const p)
{
return (p->cur);
}
// copy a sequential list
PSeqL Copy(const PSeqL const p)
{
PSeqL c;
int i;
if( (c = ini(p->Maxsize)) == NULL)
return NULL;
for(i = 0; i<p->Maxsize; i++)
{
c->data[i] = p->data[i];
}
c->cur = p->cur;
return c;
}
// insert a data
int Insert (const int x,const int site, PSeqL const p)
{
int i;
if(IsFull(p))
return 0;
for(i = p->cur ; i>=site; i--)
{
p->data[i] = p->data[i-1];
}
p ->data[i] = x;
p->cur++;
return 1;
}
// remove a data from list
int Remove (const int site, PSeqL const p)
{
int i;
if (IsEmpty(p))
return 0;
for(i = site; i<p->cur; i++)
{
p->data[i-1] = p->data[i];
}
p ->cur--;
return 1;
}
// putlist
void PutList (const PSeqL const p)
{
int i;
for(i = 0; i<p->cur; i++)
{
printf("%d ", p->data[i]);
}
}
欢迎使用CSDN-markdown编辑器
最新推荐文章于 2021-01-07 23:01:28 发布