C语言动态顺序表

2023-05-16

   顺序表是将表中的节点依次存放在计算机内存中一组地址连续的存储单元中,表可以动态增长,尾插元素,尾删元素,头插元素,头删元素,打印元素,查找元素,插入指定位置的元素,删除指定元素,删除所有指定的元素,逆序顺序表,排序顺序表,二分查找。

 ☆☆☆代码实现

seqlist.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define DEFAULT_MAX 2  //默认大小
#define INC 1  //默认增容大小

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

typedef int DataType;

typedef struct Seqlist
{
	DataType* data;
	int sz;
	int capacity;
}Seqlist, *pSeqlist;

void InitSeqlist(pSeqlist pList);
void PushBack(pSeqlist pList, DataType d);
void PopBack(pSeqlist pList);
void PushFront(pSeqlist pList, DataType d);
void PopFront(pSeqlist pList);
int Find(pSeqlist pList, DataType d);
void Display(const pSeqlist pList);
void Insert(pSeqlist pList, DataType d, int pos);
void Remove(pSeqlist pList, DataType d);
void RemoveAll(pSeqlist pList, DataType d);
void Reverse(pSeqlist pList);
void Sort(pSeqlist pList);
int BinarySearch(pSeqlist pList, DataType d);
void Release(pSeqlist pList);

#endif //__SEQLIST_H__
seqlist.c

#include "seqlist.h"

void InitSeqlist(pSeqlist pList)  //顺序表初始化
{
	assert(pList != NULL);
	pList->sz = 0;
	pList->data = (DataType*)malloc(DEFAULT_MAX*sizeof(DataType));
	if (pList->data == NULL)
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	memset(pList->data, 0x00, DEFAULT_MAX*sizeof(DataType));
	pList->capacity = DEFAULT_MAX;
}

void CheckCapacity(pSeqlist pList)  //检查容量
{
	assert(pList != NULL);
	if (pList->sz == pList->capacity)    
	{
		DataType *ptr = realloc(pList->data,(pList->capacity + INC)*sizeof(DataType));
		if (ptr == NULL)
		{
			perror("realloc");
			exit(EXIT_FAILURE);
		}
		pList->data = ptr;
		pList->capacity += INC;    //满了进行增容。
	}
}

void PushBack(pSeqlist pList, DataType d)  //尾插元素
{
	assert(pList != NULL);
	CheckCapacity(pList);
	pList->data[pList->sz] = d;
	pList->sz++;
}

void PopBack(pSeqlist pList)  //后进先出,尾删元素
{
	int i = 0;
	assert(pList != NULL);
	if (pList->sz == 0)
		return;
	pList->sz--;
}

void PushFront(pSeqlist pList, DataType d)  //头插元素
{
	assert(pList != NULL);
	CheckCapacity(pList);
	memmove(pList->data + 1, pList->data, pList->sz*sizeof(DataType));  //pdest,psrc,字节数
	pList->data[0] = d;
	pList->sz++;
}

void PopFront(pSeqlist pList)  尾删元素
{
	assert(pList != NULL);
	if (pList->sz == 0)
	{
		return;
	}
	memmove(pList->data, pList->data + 1, pList->sz*sizeof(DataType));
	pList->sz--;
}

void Display(const pSeqlist pList)  //打印元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		printf("%d ", pList->data[i]);
	}
	printf("\n");


}

int Find(pSeqlist pList, DataType d)  //查找元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		if (pList->data[i] == d)
		{
			return i;
		}
	}
	return -1;
}

void Insert(pSeqlist pList, DataType d, int pos)  //指定位置插入元素
{
	CheckCapacity(pList);
	assert(pList != NULL);
	int len = pList->sz;
	memmove(pList->data + pos, pList->data + pos - 1, (len - pos + 1)*sizeof(DataType)); // 不允许越界访问
	pList->data[pos - 1] = d;
	pList->sz++;
}

void Remove(pSeqlist pList, DataType d)  //删除指定元素
{
	assert(pList != NULL);
	int ret=Find(pList,d);
	memmove(pList->data + ret, pList->data + ret + 1, (pList->sz - ret - 1)*sizeof(DataType));
	if (ret == -1)
	{
		return;
	}
	pList->sz--;
}

void RemoveAll(pSeqlist pList, DataType d)  //删除所有指定的元素
{
	int i = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz; i++)
	{
		if (pList->data[i] == d)
		{
			memmove(pList->data + i, pList->data + i + 1, (pList->sz - i - 1)*sizeof(DataType));
			pList->sz--;
		}
	}
}

void Reverse(pSeqlist pList)  //逆序顺序表
{
	int left = 0;
	assert(pList != NULL);
	int right = pList->sz - 1;
	while (left <= right)
	{
		DataType tmp = pList->data[left];
		pList->data[left] = pList->data[right];
		pList->data[right] = tmp;
		left++;
		right--;
	}
}

void Sort(pSeqlist pList)  //排序顺序表,采用了冒泡排序
{
	int i = 0;
	int j = 0;
	assert(pList != NULL);
	for (i = 0; i < pList->sz - 1; i++)
	{
		for (j = 0; j < pList->sz - 1 - i; j++)
		{
			if (pList->data[j]>pList->data[j + 1])
			{
				DataType tmp = pList->data[j];
				pList->data[j] = pList->data[j + 1];
				pList->data[j + 1] = tmp;
			}
		}
	}
}

int BinarySearch(pSeqlist pList, DataType d)   //二分查找,前提是顺序表必须是有序表
{
	int left = 0;
	int right = pList->sz - 1;
	while (left <= right)
	{
		DataType mid = left + ((right-left) >> 1); //加比右移位优先级高
		if (pList->data[mid] > d)
		{
			right=mid-1;
		}
		else if (pList->data[mid] < d)
		{
			left=mid+1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

void Release(pSeqlist pList)
{
	free(pList->data);
	pList->data = NULL;
	pList->capacity = 0;
	pList->sz = 0;
}

test.c

#include "seqlist.h"

void test()
{
	Seqlist my_pcon;
	InitSeqlist(&my_pcon);
	PushBack(&my_pcon, 1);
	PushBack(&my_pcon, 2);
	PushBack(&my_pcon, 3);
	PushBack(&my_pcon, 4);
	Display(&my_pcon);
	PopBack(&my_pcon);
	PopBack(&my_pcon);
	PopBack(&my_pcon);
	Display(&my_pcon);
	PushFront(&my_pcon, 5);
	Display(&my_pcon);
	PushFront(&my_pcon, 4);
	Display(&my_pcon);
	PopFront(&my_pcon);
	Display(&my_pcon);
	PushFront(&my_pcon, 4);
	Display(&my_pcon);
	PushFront(&my_pcon, 3);
	Display(&my_pcon);
	PushFront(&my_pcon, 6);
	Display(&my_pcon);
	int ret=Find(&my_pcon, 5);
	printf("%d\n", ret);
	Insert(&my_pcon, 2, 3);
	Display(&my_pcon);
	Insert(&my_pcon, 8, 4);
	Display(&my_pcon);
	Remove(&my_pcon, 2);
	Display(&my_pcon);
	PushFront(&my_pcon, 5);
	Display(&my_pcon);
	RemoveAll(&my_pcon, 5);
	Display(&my_pcon);
	RemoveAll(&my_pcon, 6);
	Display(&my_pcon);
	Reverse(&my_pcon);
	Display(&my_pcon);
	Sort(&my_pcon);
	Display(&my_pcon);
	int ret1=BinarySearch(&my_pcon, 6);
	printf("%d", ret1);
	Release(&my_pcon);  //程序最后应该释放动态开辟的内存空间,防止内存泄漏
}

int main()
{
	test();
	getchar();
	return 0;
}
程序运行图



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言动态顺序表 的相关文章

随机推荐

  • QML入门----C++与QML交互快速应用

    文章目录 前言一 Qt中有关QML的C 43 43 类1 QQmlEngine2 QQmlContext3 QQmlComponent4 QQmlExpresssssion 二 其他1 使用C 43 43 属性 xff08 Q PROPER
  • QML错误:Component is not ready

    一 原因 终极原因 xff1a 组件没有构建好 xff0c 有可能是加载的QML路径不对 xff0c 或者是QML代码错误 xff0c 或者是QML组件还没有加载完 二 解决办法 打印详细错误 QQmlEngine engine span
  • QT 打开程序闪烁cmd窗口

    包含多种原因 xff0c 我的原因是Pro文件多写了一些其他的 xff0c 删除了下面这句OK了 DISTFILES span class token operator 43 span span class token operator 6
  • QT UTC(T和Z格式)时间转换为北京时间

    一 UTC 协调世界时 xff0c 又称世界统一时间 世界标准时间 国际协调时间 由于英文 xff08 CUT xff09 和法文 xff08 TUC xff09 的缩写不同 xff0c 作为妥协 xff0c 简称UTC 和北京时间相差八小
  • QT 文件操作大全

    文章目录 常用文件模式一 创建文件二 读文件三 写文件四 清空文件夹五 计算文件夹个数六 计算文件夹总大小七 转换大小为B KB M G八 批量修改文件名 常用文件模式 模式含义QIODevice ReadOnly只读方式QIODevice
  • QT QScrollArea 滑动到指定item位置

    一 QT自带的api QListWidget QTableWidget QTreeWidget都有自带的api可以调用 xff0c 如下示例 但是当自定义一个QScrollArea区域 xff0c 布局中插入多个item时 xff0c 就需
  • 马克飞象常用操作(markdown )

  • QT 移入控件展示卡片

    功能 xff1a 移入widget显示卡片 xff0c 并且可以进入卡片不消失 xff08 widget与卡片距离离得很近 xff09 xff0c 移出卡片才离开 span class token keyword bool span spa
  • 树莓派pico入门第一站:让主板上的小灯闪起来。(附代码)

    首先配置你的树莓派pico xff0c 把它插在你的电脑上 xff0c 你的电脑会多出来一个 U盘 xff0c 把这个文件复制 xff0c 粘贴 到你的树莓派pico里面 xff0c 你多出来的 U盘 会自动 消失 xff0c 这时候 xf
  • QT 网格布局插入固定列数的item

    一 场景 在网格布局插入固定列数的item xff0c 比如三列item xff0c 根据item的总数计算 span class token macro property span class token directive hash s
  • QT QMetaEnum枚举与字符串互转

    一 示例 span class token macro property span class token directive hash span span class token directive keyword include spa
  • QT 抓取widget转换为图片

    QString folder span class token operator 61 span span class token class name QStandardPaths span span class token operat
  • window11 安装linux子系统(一键安装)并连接到vs code

    文章目录 一 window 使用linux环境的几种方式二 安装wsl1 进入这个目录下 xff0c 将cmd exe已管理员身份运行2 命令行输入以下命令 xff0c 然后重启计算机3 再次已管理员身份打开 xff0c 执行命令 xff0
  • QT 利用URL Protocol实现网页调起本地程序

    一 QT 安装时脚本注入注册表或者自己添加 span class token comment 依次为目录 键 值 xff0c 34 URL Protocol 34 这个键必须有 span WriteRegStr HKCR span clas
  • PC 配置jenkins自动打包

    文章目录 一 下载jenkins运行环境二 下载jenkins三 安装 qt 5 12 2 和 VS 2017四 安装git并配置gitlab五 jenkins配置git 一 下载jenkins运行环境 java jdk 11 镜像下载地址
  • 心系Flyme

    我来自陕西省神木县 xff0c 大学我考入了陕西科技大学 xff0c 成为了一名信息与计算科学专业的学生 xff0c 希望在以后的道路中 xff0c 通过我自己的努力 xff0c 提升自己的价值 在大二大三学习编程 xff0c 希望自己可以
  • C语言的编译链接过程

    编写的一个C程序 xff08 源程序 xff09 xff0c 转换成可以在硬件上运行的程序 xff08 可执行程序 xff09 xff0c 需要进行翻译环境和运行环境 翻译环境则包括两大过程编译和链接 xff0c 经过编译和链接过程便可形成
  • 函数的调用过程(栈帧的创建和销毁)

    为了更好地认识函数的调用过程 xff0c 我们可以用反汇编代码去理解学习 一 基本概念 1 栈帧 xff08 过程活动记录 xff09 xff1a 是编译器用来实现函数调用的一种数据结构 xff0c 每个栈帧对应一个未运行完的函数 xff0
  • 树莓派pico刚买来怎么用?

    第一次使用 xff0c 首先按住主板上的白色按钮 xff0c 然后另一只手把数据线插在主板上 xff0c 直到你的电脑提示有新设备输入 xff0c 提示可以是声音 xff0c 可以是设备管理器多了一个U盘 要想得到提示 xff0c 你要打开
  • C语言动态顺序表

    顺序表是将表中的节点依次存放在计算机内存中一组地址连续的存储单元中 xff0c 表可以动态增长 xff0c 尾插元素 xff0c 尾删元素 xff0c 头插元素 xff0c 头删元素 xff0c 打印元素 xff0c 查找元素 xff0c