成都网站建设设计

将想法与焦点和您一起共享

c语言复习之顺序表(十五)-创新互联

在这里插入图片描述

创新互联建站主要从事成都网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务海勃湾,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:189820811081.概念
  1. 数组(存储):
  • 静态数组
  • 动态int* parr=calloc(size,sizeof(type));
  1. 容量:capacity
  2. 有效元素个数:size
2.定义
typedef int Elementtype;   //ADT抽象数据类型
typedef struct seqlist
{int capacity;          //容量
    int size;              //有效元素个数
    Elementtype* data;     //定义数组指针
}seqlest;
3.断言
#include
assert(...);   //断言...为真,如果不为真,则会中断程序
4.初始化
//法一:
void seqlist_init(seqlist* list)
{list->size = 0;
	list->capacity = 8;
	list->data = calloc(list->capacity,sizeof(Elementtype));
	if (!list->data)
	{assert(list->data);
	}
}

//法二:
seqlist* seqlist_creat()
{seqlist* list = calloc(1, sizeof(seqlist));
	if(!list)
	{assert(list);
	}
	seqlist_init(list);
	return list;
}

void seqlist_destory(seqlist* list)
{//先释放数组
	free(list->data);
	//再释放顺序表
	if (list)
	{free(list);
	}
}
5.元素的插入 1.尾插
//尾插
void seqlist_pushback(seqlist* list, Elementtype val)
{if(seqlist_full(list))
	{printf("seqlist is full , can't insert ! \n");
		return;
	}
	list->data[list->size++] = val;
}
2.头插
//头插
void seqlist_pushfront(seqlist* list, Elementtype val)
{if (seqlist_full(list))
	{printf("seqlist is full , can't insert !\n");
		return;
	}
	//先移动后面的元素,为新插入的元素的元素的腾出空间
	for ( int i = list->size; i >0 ; i--)
	{list->data[i] = list->data[i - 1];
	}
	//插入新元素
	list->data[0] = val;
	list->size++;
}
3.任意插
//任意插
void seqlist_insert(seqlist* list, int pos, Elementtype val)
{if (seqlist_full(list))
	{printf("seqlist is full , can't insert !\n");
		return;
	}
	//判断pos是否合法
	if( pos< 0 || pos >list->size )
	{pos = list->size;
	}
	//移动
	for ( int i = list->size ; i >pos ; i--)
	{list->data[i] = list->data[i - 1];
	}
	list->data[pos] = val;
	list->size++;
}
6.元素的删除 1.尾删
//尾删
void seqlist_popback(seqlist* list)
{if(seqlist_empty(list))
	{printf("seqlist is emoty , can't delete !\n");
		return;
	}
	list->size--;
}
2.头删
//头删
void seqlist_popfront(seqlist* list)
{if (seqlist_empty(list))
	{printf("seqlist is emoty , can't delete !\n");
		return;
	}
	for ( int i = 0; i< list->size-1; i++)
	{list->data[i] = list->data[i + 1];
	}
	list->size--;
}
3.查找
//查找
int seqlist_find(seqlist* list, Elementtype val)
{for (int i = 0; i< list->size; i++)
	{if (list->data[i] == val)
		{	return i;
		}
	}
	return -1;
}
4.指定删(元素)
//指定删(元素)
void seqlist_removeOne(seqlist* list, Elementtype val)
{if (seqlist_empty(list))
	{printf("seqlist is emoty , can't delete !\n");
		return;
	}
	if(seqlist_find(list,val))
	{for (int j = seqlist_find(list, val); j< list->size - 1; j++)
		{	list->data[j] = list->data[j + 1];
		}
	}
	list->size--;
}
5.指定位置删除
//指定位置删除
void seqlist_removePos(seqlist* list, int pos)
{if (seqlist_empty(list))
	{printf("seqlist is emoty , can't delete !\n");
		return;
	}
	assert(pos >0 && pos< list->size);
	for (int i = pos; i< list->size-1 ; i++)
	{list->data[i] = list->data[i + 1];
	}
	list->size--;
}
7.扩容
bool inc(seqlist* list)
{seqlist* newlist = realloc(list->data, list->capacity + list->capacity * 2, sizeof(Elementtype));
	if( !newlist )
	{return false;
	}
	list->data = newlist;
	list->capacity += list->capacity * 2;
	return true;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


标题名称:c语言复习之顺序表(十五)-创新互联
转载注明:http://chengdu.cdxwcx.cn/article/codiog.html