您的位置:首页 > 其它

实现基于静态数组的顺序表的以下基本操作:

2018-03-17 19:51 573 查看
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define max 100

typedef struct Seqlist
{
char value[max];
size_t size;
}seqlist;

void init(seqlist*p)
{
if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
memset(p->value,0,sizeof(p->value));
p->size=0;

}

void insert_in_tail(seqlist *p,char value)
{
int i=0;
if(p==NULL)
{
printf("p is NULL !");
exit(0);
}

if(p->size==max-1)
{
printf("This seqlist is full!");
exit(0);
}
while(p->value[i]!=0)
{
i++;
}
p->value[i]=value;

p->size++;
}

void delet_tail(seqlist *p)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
p->value[p->size]=0;
p->size--;

}

void head_insert(seqlist *p,char value)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}

if(p->size==max-1)
{
printf("The seqlist is full!");
exit(0);
}
size_t i;
for(i=p->size ; i>0 ; i--)
{
p->value[i]=p->value[i-1];
}

p->value[0]=value;

p->size++;
}

void head_delet(seqlist *p)
{
if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
size_t i=0;
while(i<p->size)
{
p->value[i]=p->value[i+1];
i++;
}
p->size--;
}

void read_value(seqlist *p ,int post)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
if(p->value[post-1]==0)
{
printf("No value in this position.");
}
else
{
printf("The value is %c .",p->value[post-1]);
}
}

void modify_value(seqlist *p,int post,char value)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
if(p->value[post-1]==0)
{
printf("No value in this position.");
}
else
{
p->value[post-1]=value;

}

}

void find_position(seqlist*p ,char value)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
int i=0;
for(i=0;i<=p->size;i++)
{
if(p->value[i]==value);
{
printf("The value' position is %d.",i);
break;
}
}
if(i==p->size+1)
{
printf("The value is not in value.");
}
}
void insert_value(seqlist *p,size_t posit,char value)
{

if(p==NULL)
{
printf("p is NULL !");
exit(0);
}
if(posit>=p->size+1)
{
p->value[p->size+1]=value;
p->size++;
}
else if(posit==0)
{
head_insert(p,value);
}
else
{
size_t i=p->size;
for(i;i>posit-1;i--)
{
p->value[i]=p->value[i+1];
}
p->value[posit]=value;

}
}
int main()
{
seqlist *p=(seqlist*)malloc(sizeof(seqlist));
init(p);
insert_in_tail(p,'q');
head_insert(p,'e');
insert_in_tail(p,'w');
read_value(p,3);
modify_value(p,2,'x');
find_position(p,'e');
insert_value(p,2,'v');
delet_tail(p);
head_delet(p);
read_value(p,1);
}

入元素                    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: