顺序表(定长的、扩容)

1、定长线性表

​ 相当于权限更多的数组,存在唯一的头(第一个节点),唯一的尾(最后一个节点),除了第一个有效节点,其余的节点都唯一有前驱节点,除了最后一个有效节点,其余的节点都有唯一后继节点。

请添加图片描述

//sqlist   sequence list
// 结构体的定义   函数的声明
#pragma once//保证程序制备编译器编译一次
//定长的顺序表结构体定义
typedef struct Sqlist
{
	int arr[10];//数据域
	int length;//有效数据节点的个数
}Sqlist, * PSqlist;
//typedef struct Sqlist Sqlist;
//typedef struct Sqlist* PSqlist;
//下面写可执行函数声明:
//不外乎增删改查

//初始化
void Init_sqlist(PSqlist plist);//struct Sqlist* plist

//按位置插入  
bool Insert_Pos(PSqlist plist, int pos, int val);

//按值删  删除这个值出现的第一次的位置
bool Del_val(PSqlist plist, int val);

//按位置删除
bool Del_pos(PSqlist plist, int pos);

//查找值为val的节点
int Search(PSqlist plist, int val);

//判空
bool IsEmpty(PSqlist plist);

//判满
bool IsFull(PSqlist plist);

//清空
void Clear(PSqlist plist);

//销毁
void Destroy(PSqlist plist);

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Sqlist.h"//未在c语言函数库里的头文件需要用双引号
//typedef struct Sqlist Sqlist;
//typedef struct Sqlist* PSqlist;
//在头文件中已经定义
//初始化线性表
void Init_sqlist(PSqlist plist)//struct Sqlist* plist
{
	assert(plist != NULL);
	plist->length = 0;//由于已经在头文件里定义了
	return;
}
//按下标插入
bool Insert_Pos(PSqlist plist, int pos, int val)
{
	assert(plist != NULL);
	if (IsFull(plist))
	{
		return false;
	}
	if (pos<0 || pos>plist->length)
	{
		return false;
	}
	else
	{
		for (int i = plist->length-1; i >= pos; i--)//从插入的位置起将元素依次向后移,若是在最后一个插的话,for循环不成立
		{
			plist->arr[i + 1] = plist->arr[i];
		}
		plist->arr[pos] = val;
		plist->length++;
		return true;
	}
}
//按值删除这个值出现的第一次的位置
bool Del_val(PSqlist plist, int val)
{
	assert(plist != NULL);
	if (IsEmpty(plist))//
	{
		return false;
	}
	int pos=Search(plist, val);//通过寻找函数找出val的下标
	if (pos == -1)//如果没有找到,则return false
	{
		return false;
	}
	for (int i = pos; i < plist->length - 1; i++)
	{
		plist->arr[i] = plist->arr[i + 1];
	}
	plist->length--;
	return true;
}
//按下标删除
bool Del_pos(PSqlist plist, int pos)
{
	assert(plist != NULL);
	if (pos<0 || pos>plist->length - 1)
	{
		return false;
	}
	else
	{
		for (int i = pos; i < plist->length - 1; i++)
		{
			plist->arr[i] = plist->arr[i + 1];
		}
		plist->length--;
		return true;
	}
}
//查找值为val的节点
int Search(PSqlist plist, int val)
{
	assert(plist != NULL);
	for (int i = 0; i < plist->length; i++)
	{
		if (plist->arr[i] == val)
		{
			return i;
		}
	}
	return -1;
}
//判空
bool IsEmpty(PSqlist plist)
{
	assert(plist != NULL);
	if (plist->length > 0)
	{
		return false;
	}
	return true;
}
//判满
bool IsFull(PSqlist plist)
{
	assert(plist != NULL);
	if (plist->length == sizeof(plist->arr) / sizeof(plist->arr[0]))
	{
		return true;
	}
	return false;
}
//清空
void Clear(PSqlist plist)
{
	assert(plist != NULL);
	plist->length = 0;
}
//销毁
void Destroy(PSqlist plist)
{
	assert(plist != NULL);
	Clear(plist);
	plist = NULL;
}
void Print(PSqlist plist)
{
	assert(plist != NULL);
	for (int i = 0; i < plist->length; i++)
	{
		printf("%d ", plist->arr[i]);
	}
	printf("\n");
}
int main()
{
	Sqlist x = {};
	PSqlist p = &x;
	Init_sqlist(p);
	int ar[10] = { 12,23,34,45,56,67,78,89,90,100 };
	int n = sizeof(ar) / sizeof(ar[0]);
	for (int i = 0; i < n; i++)
	{
		Insert_Pos(p, i, ar[i]);
		Print(p);
	}
	for (int i = 9; i >= 0; i--)
	{
		Del_pos(p, i);
		Print(p);
	}
	Clear(p);
	printf("%d ", IsEmpty(p));
	Destroy(p);
}

2、不定长线性表

请添加图片描述

扩容原理:(recalloc())

#pragma once
typedef int ELEM_TYPE;
#define INIT 10        //线性表初始长度
typedef struct DSqlist  //不定长线性表
{
	ELEM_TYPE* data;//用来承接malloc来的数组的地址
	int length;//当前数组的元素个数
	int listsize;//当前申请的数组可存放的元素个数
}DSqlist,*PDSqlist;
void Init_DSqlist(PDSqlist plist);//struct Sqlist* plist
//按位置插入  
bool Insert_Pos(PDSqlist plist, int pos, int val);

//按值删  删除这个值出现的第一次的位置
bool Del_val(PDSqlist plist, int val);

//按位置删除
bool Del_pos(PDSqlist plist, int pos);

//查找值为val的节点
int Search(PDSqlist plist, int val);

//判空
bool IsEmpty(PDSqlist plist);

//判满
bool IsFull(PDSqlist plist);

//清空
void Clear(PDSqlist plist);

//销毁
void Destroy(PDSqlist plist);

//扩容
void Dilatation(PDSqlist plist);
#include"DSqlist.h"
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
/*
顺序表的特点:
1.简单  好实现
2.逻辑上相邻,物理上也相邻
3.插入时间复杂度O(n)(挪动数据)   尾插的时间复杂度为O(1)
4.删除时间复杂度O(n)(挪动数据)   尾删的时间复杂度为O(1)
5.随机访问时间复杂度O(1)   因为有数组下标帮忙
*/
void Init_DSqlist(PDSqlist plist)//struct Sqlist* plist
{
	assert(plist != NULL);
	plist->data = (ELEM_TYPE*)malloc(sizeof(int) * INIT);
	plist->length = 0;
	plist->listsize = INIT;
	return;
}
//按位置插入  
bool Insert_Pos(PDSqlist plist, int pos, int val)
{
	assert(plist != NULL);
	if (IsFull(plist))
	{
		Dilatation(plist);
	}
	for (int i = plist->length - 1; i >= pos; i--)
	{
		plist->data[i + 1] = plist->data[i];
	}
	plist->data[pos] = val;
	plist->length++;
	return true;
}
//按值删  删除这个值出现的第一次的位置
bool Del_val(PDSqlist plist, int val)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return false;
	}
	int pos = Search(plist, val);
	if (pos ==-1)
	{
		return false;
	}
	Del_pos(plist, pos);
}
//按位置删除
bool Del_pos(PDSqlist plist, int pos)
{
	assert(plist != NULL);
	if (IsEmpty(plist))
	{
		return false;
	}
	for (int i = pos; i < plist->length - 1; i++)
	{
		plist->data[i] = plist->data[i + 1];
	}
	plist->length--;
	return true;
}
//查找值为val的节点
int Search(PDSqlist plist, int val)
{
	assert(plist != NULL);
	for (int i = 0; i < plist->length; i++)
	{
		if (val == plist->data[i])
		{
			return i;
		}
	}
	return -1;
}
//判空
bool IsEmpty(PDSqlist plist)
{
	assert(plist != NULL);
	if (plist->length == 0)
	{
		return true;
	}
	return false;
}

//判满
bool IsFull(PDSqlist plist)
{
	assert(plist != NULL);
	if (plist->length == plist->listsize)
	{
		return true;
	}
	return false;
}

//清空
void Clear(PDSqlist plist)
{
	assert(plist != NULL);
	plist->length = 0;
	plist->listsize = 0;
	return;
}

//销毁
void Destroy(PDSqlist plist)
{
	assert(plist != NULL);
	Clear(plist);
	free(plist->data);
	plist->data = NULL;
	return;
}
//扩容
void Dilatation(PDSqlist plist)
{
	assert(plist!= NULL);
	plist->data = (ELEM_TYPE*)realloc(plist->data, sizeof(ELEM_TYPE)*plist->listsize*2);
	if (plist->data == NULL)
	{
		return;
	}
	plist->listsize *= 2;
}
void Print(PDSqlist plist)
{
	assert(plist != NULL);
	for (int i = 0; i < plist->length; i++)
	{
		printf("%d ", plist->data[i]);
	}
	printf("\n");
}

//扩容有三种可能,第一种,后面的内存可用,往后面添加
//第二种,后面的内存不够,重新创建新的
//第三种,所有内存都不够,返回false
int main()
{
	int ar[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int n = sizeof(ar) / sizeof(ar[0]);
	DSqlist x = {};
	PDSqlist p = &x;
	Init_DSqlist(p);
	for (int i = 0; i < n; i++)
	{
		Insert_Pos(p, i, ar[i]);
		Print(p);
	}
	Insert_Pos(p, 10, 11111);
	Print(p);
	Clear(p);
	Destroy(p);
}
ist->data[i]);
}
printf("\n");
}
//扩容有三种可能,第一种,后面的内存可用,往后面添加
//第二种,后面的内存不够,重新创建新的
//第三种,所有内存都不够,返回false
int main()
{
int ar[10] = { 1,2,3,4,5,6,7,8,9,10 };
int n = sizeof(ar) / sizeof(ar[0]);
DSqlist x = {};
PDSqlist p = &x;
Init_DSqlist§;
for (int i = 0; i < n; i++)
{
Insert_Pos(p, i, ar[i]);
Print§;
}
Insert_Pos(p, 10, 11111);
Print(p);
Clear(p);
Destroy(p);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值