数据结构:顺序表

一、概念

顺序表中的元素之间是一对一关系,多个同一特征的数据元素

物理结构、逻辑结构都是连续的

二、实现

1.可以通过数组实现,在栈区申请空间,也可以在堆区申请空间

2.由于要对元素进行增删改查操作,需要记录顺序表的实际长度

三、功能

1.创建顺序表

cs 复制代码
//创建顺序表
seq_p create_seq_list(){
	seq_p L =(seq_p)malloc(sizeof(seq_list));
	//在堆区申请一个顺序表的空间
	if(L==NULL){
		printf("空间申请失败\n");
		return NULL;
	}
	L->len=0;//长度置0
	bzero(L,sizeof(L->data));
	return L;
}

2.判空

cs 复制代码
//判空
int seq_empty(seq_p L){
	//容错判断
	if(L==NULL)
		return -1;
	return L->len==0?1:0;//是空就返回1
}

3.判满

cs 复制代码
//判满
int seq_full(seq_p L){
	// 容错判断
	if(L==NULL)
		return -1;
	return L->len==MAX?1:0;//是满就返回1
}

4.头插

cs 复制代码
//顺序表头插
//参数一:指向顺序表的指针
//参数二:要插入的元素
void insert_head(seq_p L,int data){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判满
	if(seq_full(L)){
		printf("表已满,不能插入\n");
		return;
	}
	//循环后移已有元素
	for(int i=L->len-1;i>=0;i--){
		L->data[i+1]=L->data[i];
	}
	//头插,给下标为0的位置插入新元素
	L->data[0]=data;
	L->len++;//长度自增
}

5.尾插

cs 复制代码
//尾插
void insert_tail(seq_p L,int value){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判满
	if(seq_full(L)){
		printf("表已满,不能插入\n");
		return;
	}
	L->data[L->len]=value;
	L->len++;

}

6.按位置插入

cs 复制代码
//按位置插入
//从第0的位置开始计数
void insert_pos(seq_p L,int value,int pos){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判满
	if(seq_full(L)){
		printf("表已满,不能插入\n");
		return;
	}
	//判断位置合理性
	if(pos>L->len){
		printf("插入位置不合理\n");
		return;
	}
	for(int i=L->len-1;i>=pos;i--){
		L->data[i+1]=L->data[i];
	}
	L->data[pos]=value;
	L->len++;

}

7.头删

cs 复制代码
//头删
void del_head(seq_p L){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	for(int i=0;i<L->len-1;i++){
		L->data[i]=L->data[i+1];
	}
	L->len--;

}

8.尾删

cs 复制代码
//尾删
void del_tail(seq_p L){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	L->data[L->len-1]=0;
	L->len--;
}

9.按位置删除

cs 复制代码
//按位置删除
void del_pos(seq_p L,int pos){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	for(int i=pos;i<L->len-1;i++){
		L->data[i]=L->data[i+1];
	}
	L->len--;

}

10.打印顺序表

cs 复制代码
//打印顺序表
void out_put(seq_p L){
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	for(int i=0;i<L->len;i++){
		printf("%d ",L->data[i]);
	}
	putchar(10);
}

11.按值查找,返回下标

cs 复制代码
//按值查找,返回下标
int search_value(seq_p L,int value){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return -1;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return -2;
	}
	for(int i=0;i<L->len;i++){
		if(L->data[i]==value){
			return i;
		}
	}
	printf("查找失败\n");
	return -3;
}

12.按值查找,返回值

cs 复制代码
//按位置查找,返回值
int search_pos(seq_p L,int pos){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return -1;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return -2;
	}
	//判断位置合理性
	if(pos<0 || pos>=L->len){
		printf("位置不合理\n");
		return -3;
	}

	return L->data[pos];
}

13.顺序表去重

cs 复制代码
//顺序表去重
void del(seq_p L){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	if(L->len==1){
		printf("只有一个元素\n");
		return;
	}
	for(int i=0;i<L->len;i++){ //删除了重复数字后,L->len也会减小,相应的执行次数也会减少
		for(int j=i+1;j<L->len;j++){
			if(L->data[i]==L->data[j]){
				del_pos(L,j);
				j--;//如果不加会跳过一个元素
			}

		}
	}

}

注意最后一行的 j--;

14. 清空顺序表

cs 复制代码
void clean_seq(seq_p L){
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	L->len=0;//直接将长度置0
}

15.释放空间

cs 复制代码
//释放空间
void seq_free(seq_p *L){
	if(L==NULL || *L==NULL){
		return;
	}
	free(*L);//传进来一个二级指针,通过*降级,变为L本身,所以free的就是L
	*L=NULL; //让主函数的L置空,传值和传址,需要用二级指针
}

//或者   再在主函数加一句L=NULL;
//void seq_list(seq_p L){
//free(L);
//}

或者 (需要在主函数中加L=NULL;)

cs 复制代码
void seq_free(seq_p L){
    if(L==NULL)
        return;
    free(L);
}    


这样写的话,需要在主函数中加入:
seq_free(L);
L=NULL;

16.冒泡排序

cs 复制代码
void maopao(seq_p L){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	int temp;
	for(int i=1;i<L->len;i++){
		for(int j=0;j<(L->len-i);j++){
			if(L->data[j]>L->data[j+1]){
				temp=L->data[j];
				L->data[j]=L->data[j+1];
				L->data[j+1]=temp;
			}
		}
	}
}

17.选择排序

cs 复制代码
//选择排序
void xuanze(seq_p L){
	//容错判断
	if(L==NULL){
		printf("入参为空,请检查\n");
		return;
	}
	//判空
	if(seq_empty(L)){
		printf("表空,不能删除\n");
		return;
	}
	int index;
	int temp;
	for(int i=0;i<L->len;i++){
		index=i;
		for(int j=i;j<L->len;j++){
			if(L->data[index]>L->data[j]){
				index=j;
			}

		}
		temp=L->data[i];
		L->data[i]=L->data[index];
		L->data[index]=temp;
	}

}

四、功能函数的申明

cs 复制代码
#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef struct seq_list{
	int data[MAX];
	int len;
}seq_list,*seq_p;

//创建顺序表
seq_p create_seq_list();
//判空
int seq_empty(seq_p L);
//判满
int seq_full(seq_p L);
//头插
void insert_head(seq_p L,int data);
//尾插
void insert_tail(seq_p L,int value);
//打印顺序表
void out_put(seq_p L);
//按位置插入
void insert_pos(seq_p L,int value,int pos);
//头删
void del_head(seq_p L);
//尾删
void del_tail(seq_p L);
//按位置删除
void del_pos(seq_p L,int pos);
//释放空间
void seq_free(seq_p *L);
//按值查找,返回下标
int search_value(seq_p L,int value);
//按位置查找,返回值
int search_pos(seq_p L,int pos);
//顺序表去重
void del(seq_p L);
//清空顺序表
void clean_seq(seq_p L);
//冒泡排序
void maopao(seq_p L);
//选择排序
void xuanze(seq_p L);
#endif

五、主函数的调用

cs 复制代码
#include "seq_list.h"
int main(){
	seq_p L = create_seq_list();
	printf("%d\n",seq_empty(L));
	printf("%d\n",seq_full(L));
	insert_head(L,10);
	insert_tail(L,90);
	insert_tail(L,50);
	insert_pos(L,60,3);
	insert_tail(L,10);
	out_put(L);
//	del_head(L);
	out_put(L);
//	del_tail(L);
	out_put(L);
//	del_pos(L,2);
	out_put(L);
	printf("下标为%d\n",search_value(L,200));
	printf("值为%d\n",search_pos(L,1));
	out_put(L);
	del(L);
	out_put(L);
//	maopao(L);
	xuanze(L);
	out_put(L);
	seq_free(&L);//传递二级指针
	//L=NULL;
	out_put(L);
	return 0;
}
相关推荐
Starry_hello world2 小时前
二叉树实现
数据结构·笔记·有问必答
嵌入式AI的盲4 小时前
数组指针和指针数组
数据结构·算法
reyas6 小时前
B树系列解析
数据结构·b树
Indigo_code6 小时前
【数据结构】【顺序表算法】 删除特定值
数据结构·算法
阿史大杯茶7 小时前
Codeforces Round 976 (Div. 2 ABCDE题)视频讲解
数据结构·c++·算法
不穿格子衬衫8 小时前
常用排序算法(下)
c语言·开发语言·数据结构·算法·排序算法·八大排序
aqua35357423588 小时前
蓝桥杯-财务管理
java·c语言·数据结构·算法
韬. .9 小时前
树和二叉树知识点大全及相关题目练习【数据结构】
数据结构·学习·算法
野草y9 小时前
数据结构(7.4_1)——B树
数据结构·b树
Word码9 小时前
数据结构:栈和队列
c语言·开发语言·数据结构·经验分享·笔记·算法