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

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);
}

入元素                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值