1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插
入元素
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);
}入元素
本文介绍了一个使用 C 语言实现的序列列表(Seqlist)的数据结构。该数据结构支持多种基本操作,包括初始化、尾部插入与删除、头部插入与删除、读取任意位置元素、修改任意位置元素、查找指定元素值的下标以及在任意位置插入元素。通过具体代码展示了这些功能的实现细节。
4742

被折叠的 条评论
为什么被折叠?



