01 数据结构引入 和 顺序表

news/2024/7/7 20:21:31

阅读引言: 从本文开始给大家带来我在复习过程中写的数据结构的代码, 分享给需要的同学

一、数据结构引入

1.数据结构解决什么问题

数据结构可以将杂乱无章的数据管理起来, 提高数据的访问效率

计算机处理的对象(数据)已不再单纯是数值

其中系、办公室、……教师、学生可视为数据元素。元素之间呈现的是一种层次关系

设田径比赛项目有:A(跳高)、 B(跳远)、C(标枪)、D(铅球)、E(100m跑)、F(200m跑)。参赛选手的项目表,如下表所列:

2.数据结构的逻辑关系

二、顺序表的代码实现

/* squence_list.h */
#ifndef _SQUENCE_LIST_H
#define _SQUENCE_LIST_H

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

#define N 128
typedef int data_t;           /* 在编译的时候处理 */
typedef struct {
	data_t arr[N];             /* 顺序表的存储空间 */
	int last;                  /* 数组的最后一个元素的下标, last为-1代表数组中没有元素 */
}sq_list, *sq_link;

sq_link sqlist_create(void);                                    /* 创建顺序表 */
int sq_list_clear(sq_link p);                                   
int is_empty(sq_link p);
int get_length(sq_link p);
int sqlist_insert(sq_link p, data_t value, int pos);
void sqlist_show(sq_link p);
void sqlist_delete(sq_link p);
int sqlist_delete_pos(sq_link p, int pos);
int sqlist_locate(sq_link p, data_t value);
int two_sqlist_merge(sq_link p1, sq_link p2);
int delete_repeate_element(sq_link p);

#endif

/* squence_list.c */
#include "squence_list.h"


sq_link sqlist_create(void)
{
	sq_link p = (sq_link)malloc(sizeof(sq_list));
	if(p == NULL) {
		printf("%s malloc is failed!\n", __func__);
		return NULL;
	}

	memset(p, 0, sizeof(sq_list));
	p->last = -1;

	return p;
}

int sq_list_clear(sq_link p)
{
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	memset(p, 0, sizeof(sq_list));
	p->last = -1;

	return 0;
}

int is_empty(sq_link p) 
{
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}
	if(p->last == -1) {
		return 1;
	}
	return 0;
}

int get_length(sq_link p)
{
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	return (p->last + 1);
	
}

/* 顺序表的缺点: 涉及到插入数据和删除数据的时候会有大量的数据移动 */
int sqlist_insert(sq_link p, data_t value, int pos) 
{
	int i;
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}
	if(p->last >= N) {
		printf("squence list is full!\n");
		return -1;
	}

	/* 存储位置必须连接着 */
	if(pos < 0 || pos > p->last + 1) {
		printf("index of arrary is invalid!\n");
		return -2;
	}

	/* 将位于pos位置后的数据移动 */
	for(i = p->last; i >= pos; i--) {
		p->arr[i + 1] = p->arr[i];
	}
	p->arr[pos] = value;
	p->last++;

	return 0;
	
}

void sqlist_show(sq_link p)
{
	int i;
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return ;
	}

	if(p->last == -1) {
		printf("squence list is empty!\n");
		return ;
	}
	
	for(i = 0; i <= p->last; i++) {
		printf("%d ", p->arr[i]);
	}
	puts("");
	
}


void sqlist_delete(sq_link p)
{
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return;
	}

	free(p);
	p = NULL;         /* 防止野指针页虚悬空指针 */
}


int sqlist_delete_pos(sq_link p, int pos) 
{
	int i;
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	if(pos < 0 || pos > p->last) {
		printf("index of delete is invalid!\n");
		return -1;
	}

	for(i = pos; i < p->last; i++) {
		p->arr[i] = p->arr[i + 1];
	}

	p->last--;

	return 0;
}

int sqlist_locate(sq_link p, data_t value)
{
	int i;
	if(p == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	for(i = 0; i <= p->last; i++) {
		if(p->arr[i] == value) {
			return i;
		}
	}

	return -1;
}


/* 将p1和p2的并集插入到p1中 */
int two_sqlist_merge(sq_link p1, sq_link p2)
{

	int i = 0;
	if(p1 == NULL || p2 == NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	while(i < p2->last) {
		if(sqlist_locate(p1, p2->arr[i]) == -1) {
			sqlist_insert(p1, p2->arr[i], p1->last + 1);
		}
		i++;
	}

	return 0;
}


/* 删除顺序表中的重复元素 */
int delete_repeate_element(sq_link p)
{
	int i = 0, j = 0;
	if(p ==NULL) {
		printf("param of %s is NULL!\n", __func__);
		return -1;
	}

	if(p->last == 0) {
		return -1;
	}

	while(i < p->last) {
		j = i + 1;
		while(j <= p->last) {
			if(p->arr[i] == p->arr[j]) {
				sqlist_delete_pos(p, j);
			} 
			j++;
		}
		i++;
	}

	return 0;
}


http://lihuaxi.xjx100.cn/news/2085241.html

相关文章

爬虫入门到精通_框架篇16(Scrapy框架基本使用_名人名言的抓取

1 目标站点分析 抓取网站&#xff1a;http://quotes.toscrape.com/ 主要显示了一些名人名言&#xff0c;以及作者、标签等等信息&#xff1a; 点击next&#xff0c;page变为2&#xff1a; 2 流程框架 抓取第一页&#xff1a;请求第一页的URL并得到源代码&#xff0c;进行下…

一篇搞定mysql数据库基础

目录 一、MySQL具体的特点 1.关系型数据库&#xff08;RDBMS&#xff09;&#xff1a; 2.MySQL是一个“客户端-服务器”结构的程序 Q1:服务器能不能知道客户端什么时候发请求&#xff1f;&#xff1f; Q2:服务器是只给一个客户端提供服务吗&#xff1f;&#xff1f; 二、M…

什么是智慧公厕?智慧公厕的应用价值有哪些?

在现代社会&#xff0c;城市的发展与人民生活质量息息相关。作为城市基础设施中的重要一环&#xff0c;公共厕所的建设及管理一直备受关注。智慧公厕作为一种公共厕所使用、运行、管理的综合应用解决方案&#xff0c;正逐渐在智慧城市的建设中崭露头角。那么&#xff0c;智慧公…

【Oracle之DataGuard的初步学习】

** 以下所有均是基于11G版本的 ** 一、DataGuard的部署方式 DG的部署最常用的方式就是直接在备库端部署一个空库然后再设置参数&#xff0c;但是这样做在初始同步时如果数据量过大会耗费较长的时间&#xff1b;相对来说这中方式比较简单不易出错。 还有一种方式就是通过rman的备…

[Leetcode] 904. 水果成篮 —— 滑动窗口

Problem: 904. 水果成篮 文章目录 思路解题方法复杂度Code 思路 需要找到连续的最多两种类型的最长序列 通过例子讲解思路&#xff1a;34335&#xff0c;left0&#xff0c;mid1&#xff0c;new_mid2 定义&#xff1a;现有三个下标left,mid,new_mid&#xff1b;其中left和mid分别…

淘宝商品详情数据

淘宝商品详情数据接口的应用场景有很多&#xff0c;以下是一些典型的应用场景&#xff1a; 商品信息展示&#xff1a;通过调用淘宝商品详情数据接口&#xff0c;可以获取商品的详细信息&#xff0c;如标题、价格、销量、评价等。这些信息可以用于在自己的网站或应用程序中展示…

蓝桥杯省赛 Java b组 2015年 第六届

一、题目 加法变乘法 我们都知道&#xff1a;123 ... 49 1225 现在要求你把其中两个不相邻的加号变成乘号&#xff0c;使得结果为2015 比如&#xff1a; 123...10*1112...27*2829...49 2015 就是符合要求的答案。 请你寻找另外一个可能的答案&#xff0c;并把位置靠前的…

大模型日报|今日必读的7篇大模型论文

大家好&#xff0c;今日必读的大模型论文来啦&#xff01; 1.谷歌Gemini 1.5 Pro技术报告出炉&#xff0c;共计671位作者 在 Gemini 1.5 Pro 技术报告中&#xff0c;谷歌团队写道&#xff0c;“Gemini 1.5 Pro 是一种计算效率极高的多模态专家混合模型&#xff0c;能够从包括…