每日学习-2月1日

知识点:顺序表(后续)

检查是否有存放空间:

void SeListCheckCapacity(SL*ps)
{
	if(ps->size==ps->capacity)
	{
		int newcapacity=ps->capacity==0?4:ps->capacity*2;
		SLDataType*tmp=(SLDataType*)realloc(ps->a,newcapacity*sizeof(SLDataType));
		if(tmp==NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a=tmp;
		ps->capacity=newcapacity;
	}
 } 

头插:

void SeListPushFront(SL*ps,SLDataType x)
 {
 	SeListCheckCapacity(ps);
 	int end=ps->size-1;
 	while(end>=0)
 	{
 		ps->a[end+1]=ps->a[end];
 		--end;
	 }
	 ps->a[0]=x;
	 ps->size++;
  } 

 头删:

 void SeqListPopFront(SL*ps)
  {
  	assert(ps->size>0);
  	int begin=1;
  	while(begin<ps->size)
  	{
  		ps->a[begin-1]=ps->a[begin];
  		++begin;
	  }
	  ps->size--;
  }

查找一个数在顺序表中的位置:

 int SeqListFind(SL*ps,SLDataType x)
  {
  	for(int i=0;i<ps->size;i++)
  	{
  		if(ps->a[i]==x)
  		{
  			return i;
		  }
	  }
	  return -1;
  }

在指定的pos下标位置插入:

 void SeqListInsert(SL *ps,int pos,SLDataType x)
  {
//  	if(pos>ps->size||pos<0)
//  	{
//  		printf("pos invalid\n");
//  		return;
//	  }
    assert(pos>=0&&pos<=ps->size);
    SeqListCheckCapacity(ps);
    int end=ps->size-1;
    while(end>=pos)
    {
    	pd->a[end+1]=ps->a[end];
    	--end;
	}
	ps->a[pos]=x;
	ps->size++;
   } 

在指定的pos下标位置删除:

void SeqListErase(SL*ps,int pos) 
   {
   	asser(pos>=0&&pos<ps->size);
   	int begin=pos+1;
   	while(begin<ps->size)
   	{
   		ps->a[begin-1]=ps->a[begin];
   		++begin;
	   }
	   ps->size--;
   }

 可用SeqListInert和SeqListErase函数来实现头插,尾插,头删,尾删,更加方便简洁。

SeqListFind可以和SeqListInsert和SeqListErase配合使用,更加简单便捷

相关推荐

  1. 每日学习-21

    2024-02-02 20:48:02       26 阅读
  2. 13每日信息差

    2024-02-02 20:48:02       36 阅读
  3. 112每日信息差

    2024-02-02 20:48:02       43 阅读
  4. 116每日信息差

    2024-02-02 20:48:02       31 阅读
  5. 125每日信息差

    2024-02-02 20:48:02       32 阅读
  6. 205每日信息差

    2024-02-02 20:48:02       30 阅读
  7. 219每日信息差

    2024-02-02 20:48:02       31 阅读
  8. 220每日信息差

    2024-02-02 20:48:02       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-02 20:48:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-02 20:48:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-02 20:48:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-02 20:48:02       18 阅读

热门阅读

  1. 1. 两数之和

    2024-02-02 20:48:02       22 阅读
  2. 今日分享个三级联动

    2024-02-02 20:48:02       31 阅读
  3. HTTP请求传递参数方式【2024-02-01】

    2024-02-02 20:48:02       29 阅读
  4. git pull的时候报错

    2024-02-02 20:48:02       29 阅读