当前位置: 首页 > news >正文

阿里巴巴网站基础建设首保服务附近电脑培训速成班一个月

阿里巴巴网站基础建设首保服务,附近电脑培训速成班一个月,做网站更新维护工资高吗,进一步推进网站集约化建设目录 1.线性表概念 1.1 什么是顺序列表 1.2 线性表 2.顺序表实现 将有以下功能: 详细过程 顺序表的动态存储 顺序表初始化 尾插 扩容 头插 更改后的尾插 尾删 头删 打印 释放内存 优化顺序表 (任意位置插入删除) 优化后的头插尾插 优化后的头删尾…

目录

1.线性表概念

1.1 什么是顺序列表

1.2 线性表

2.顺序表实现

将有以下功能:

详细过程

顺序表的动态存储

顺序表初始化

尾插

扩容

头插

更改后的尾插

尾删

头删

打印

释放内存

优化顺序表 (任意位置插入删除)

优化后的头插尾插

优化后的头删尾删

查找和删除

进行装饰(菜单)

成品

SeqList.h

SeqList.c

Test.c:


1.线性表概念

1.1 什么是顺序列表

顺序列表(Sequential List)是一种使用连续的内存空间存储元素的线性数据结构。顺序列表中的元素按照其在内存中的物理顺序依次排列,同时通过索引来访问元素。

顺序列表可以使用数组来实现,数组的下标就是元素的索引。由于数组具有随机访问的特性,即可以通过索引直接访问元素,因此顺序列表在查找指定位置的元素时具有较高的效率。

顺序列表的特点包括:

  1. 连续的内存空间:顺序列表中的元素在内存中是连续存储的,这样可以通过索引进行快速访问,提高了访问效率。

  2. 固定大小:顺序列表的大小在创建时就确定,一旦分配了固定大小的内存空间,就无法自动扩展或缩小。需要预估元素的个数,以避免空间浪费或溢出。

  3. 随机访问效率高:由于顺序列表基于数组实现,并支持随机访问,可以在O(1)的时间复杂度内获取指定位置的元素值。

  4. 插入和删除的效率较低:当需要在顺序列表的中间位置插入或删除元素时,需要移动部分元素,导致时间复杂度为O(n)。因此,在有频繁的插入和删除操作时,顺序列表的效率可能较低。

需要注意的是,顺序列表适用于元素个数固定且随机访问较为频繁的场景。当需要频繁进行插入和删除操作,或者元素个数不确定时,可以考虑其他数据结构,如链表。

1.2 线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使
用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
线性表在物理上存储时,通常以数组和链式结构的形式存储。

2.顺序表实现

将有以下功能:

// 顺序表的动态存储
typedef struct SeqList// 基本增删查改接口  
// 顺序表初始化  
void SeqListInit(SeqList* psl);  
// 顺序表销毁  
void SeqListDestory(SeqList* psl);  
// 顺序表打印  
void SeqListPrint(SeqList* psl);  
// 检查空间,如果满了,进行增容  
void CheckCapacity(SeqList* psl);  
// 顺序表尾插  
void SeqListPushBack(SeqList* psl, SLDataType x);  
// 顺序表尾删  
void SeqListPopBack(SeqList* psl);  
// 顺序表头插  
void SeqListPushFront(SeqList* psl, SLDataType x);  
// 顺序表头删  
void SeqListPopFront(SeqList* psl);  
// 顺序表查找  
int SeqListFind(SeqList* psl, SLDataType x);  
// 顺序表在pos位置插入x  
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);  
// 顺序表删除pos位置的值  
void SeqListErase(SeqList* psl, size_t pos);

详细过程

定义三个文件:
头文件 SeqList.h
函数的实现SeqList.c
代码的测试 Test.c

顺序表的动态存储

//SeLqist.h
#define N 200
typedef int SLDataType;//静态顺序表 -- N太小,可能不够用 N太大,可能浪费空间
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//	int capa;
//};//动态顺序表
typedef struct SeqList
{SLDataType* a;// 指向数组的指针int size;	  // 数据个数int capacity;//  容量-空间大小
}SL;

顺序表初始化

//SeqList.c
void SLInit(SL* ps)
{ps->a = NULL;ps->size =ps->capacity= 0;
}

尾插

void SLPushBack(SL* ps, SLDataType x)
{//检查容量空间,满了扩容if (ps->capacity == ps->size){int newCapacity = 0;if (ps->capacity == 0)newCapacity = ps->capacity = 4;elsenewCapacity = ps->capacity * 2;SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));if (tmp == NULL){printf("realloc fail\n");//exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->size] = x;ps->size++;
}

扩容

//动态增容
void SLCheckCapacity(SL* ps)
{//检查容量空间,满了扩容if (ps->capacity == ps->size){int newCapacity = 0;if (ps->capacity == 0)newCapacity = ps->capacity = 4;elsenewCapacity = ps->capacity * 2;SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));if (tmp == NULL){printf("realloc fail\n");//exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}
}

头插

因为多处要进行数据扩容,故将数据扩容单独用写为一个函数

void SLPushBack(SL* ps, SLDataType x)
{SLCheckCapacity(ps);ps->a[ps->size] = x;ps->size++;
}

更改后的尾插

//尾插
void SLPushBack(SL* ps, SLDataType x)
{SLCheckCapacity(ps);ps->a[ps->size] = x;ps->size++;
}

尾删

void SLPopBack(SL* ps)
{//尾部要删除的数字无需重新定义数字,意义不大//只需将 size-- 即可(要防止越界)assert(ps->size>0);//防止空了还继续删除ps->size--;
}

头删

//头删
void SLPopFront(SL* ps)
{assert(ps->size > 0);int begin = 1;while (begin<ps->size){ps->a[begin - 1] = ps->a[begin];++begin;}ps->size--;
}

打印

//打印
void SLPrint(SL* ps)
{assert(ps!=NULL);for (int i = 0;i < ps->size;i++){printf("%d ", ps->a[i]);}printf("\n");
}

释放内存

//释放内存
void SLDestory(SL* ps)
{assert(ps != NULL);if (ps->a){free(ps->a);ps->a = NULL;ps->capacity = ps->size = 0;}
}

优化顺序表 (任意位置插入删除)

增加顺序表功能:在中间部分 插入/删除 数字,也可简化头尾插删代的码量

//任意位置插入 (插入数据都要防止越界)
void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos < ps->size);SLCheckCapacity(ps);int end = ps->size - 1;while (end>=pos){ps->a[end + 1] = ps->a[end];--end;}ps->a[pos] = x;ps->size++;
}//任意位置删除
void SLErase(SL* ps, int pos)
{assert(pos >= 0 && pos < ps->size);int begin = pos;while (begin<ps->size){ps->a[begin] = ps->a[begin + 1];++begin;}ps->size--;
}

既然已经做到在任意位置可以插入代码,则可以对之前写的代码进行简化:

优化后的头插尾插

//尾插
void SLPushBack(SL* ps, SLDataType x)
{SLInsert(ps, ps->size, x);
}//头插
void SLPushFront(SL* ps, SLDataType x)
{SLInsert(ps, 0, x);
}

优化后的头删尾删

//尾删
void SLPopBack(SL* ps)
{SLErase(ps, ps->size - 1);
}//头删
void SLPopFront(SL* ps)
{SLErase(ps, 0);
}

查找和删除

//查找
int SLFind(SL* ps, SLDataType x)
{for (int i = 0; i <ps->size; i++){if (ps->a[i] == x)return i;}return -1;//没找到
}//修改
int SLModify(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos < ps->size);ps->a[pos] = x;
}

测试该两项功能test.c:

//
int x = 0;
printf("请输入你要删除的值:>");
scanf("%d", &x);
int pos = SLFind(&sl, x);
if (pos != -1)
{SLErase(&sl, pos);
}
elseprintf("没有找到%d\n",x);
SLPrint(&sl);
//
int y, z;
printf("请输入你要修改的值和修改后的值:>");
scanf("%d %d", &y,&z);
pos = SLFind(&sl, y);
if (pos != -1)
{SLModify(&sl, pos,z);
}
elseprintf("没有找到%d\n", y);
SLPrint(&sl);
//
int f = 0;
printf("请输入你要删除的值,并删除所有与之相同的值:>");
scanf("%d", &f);
pos = SLFind(&sl, f);
while (pos!=-1)
{SLErase(&sl, pos);pos = SLFind(&sl, f);
}

进行装饰(菜单)

void menu()
{printf("*******************************\n");printf("1.头插  2.尾插	     3.查找 \n");printf("4.删除  5.连续删除   6.修改  \n");printf("7.打印  8.退出 \n");printf("*******************************\n");
}

成品

SeqList.h

#pragma once#include <stdio.h>
#include <assert.h>
#include <stdlib.h>typedef int SLDataType;//静态顺序表 -- N太小,可能不够用 N太大,可能浪费空间
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//	int capa;
//};//动态顺序表
typedef struct SeqList
{SLDataType* a;// 指向数组的指针int size;	  // 数据个数int capacity;//  容量-空间大小
}SL;//初始化
void SLInit(SL* ps);//头插
void SLPushFront(SL* ps, SLDataType x);//头删
void SLPopFront(SL* ps);//尾插
void SLPushBack(SL* ps, SLDataType x);//尾删
void SLPopBack(SL* ps);//任意位置插入
void SLInsert(SL* ps,int pos, SLDataType x);//任意位置删除
void SLErase(SL* ps, int pos);//打印
void SLPrint(SL* ps);//动态增容
void SLCheckCapacity(SL* ps);//释放内存
void SLDestory(SL* ps);//查找
int SLFind(SL* ps, SLDataType x);//修改
void SLModify(SL* ps, int pos, SLDataType x);

SeqList.c

#include "SeqList.h"void SLInit(SL* ps)
{ps->a = NULL;ps->size = ps->capacity = 0;
}//打印
void SLPrint(SL* ps)
{assert(ps!=NULL);for (int i = 0;i < ps->size;i++){printf("%d ", ps->a[i]);}printf("\n");
}//动态增容
void SLCheckCapacity(SL* ps)
{assert(ps != NULL);//检查容量空间,满了扩容if (ps->capacity == ps->size){int newCapacity = 0;if (ps->capacity == 0)newCapacity = ps->capacity = 4;elsenewCapacity = ps->capacity * 2;SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));if (tmp == NULL){printf("realloc fail\n");//exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}
}//任意位置插入 (插入数据都要防止越界)
void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);//挪动数据int end = ps->size - 1;while (end>=pos){ps->a[end + 1] = ps->a[end];--end;}ps->a[pos] = x;ps->size++;
}//任意位置删除
void SLErase(SL* ps, int pos)
{assert(pos >= 0 && pos < ps->size);int begin = pos;while (begin<ps->size){ps->a[begin] = ps->a[begin + 1];++begin;}ps->size--;
}//尾插
void SLPushBack(SL* ps, SLDataType x)
{SLInsert(ps, ps->size, x);
}//头插
void SLPushFront(SL* ps, SLDataType x)
{SLInsert(ps, 0, x);
}//尾删
void SLPopBack(SL* ps)
{SLErase(ps, ps->size - 1);
}//头删
void SLPopFront(SL* ps)
{SLErase(ps, 0);
}//查找
int SLFind(SL* ps, SLDataType x)
{for (int i = 0; i <ps->size; i++){if (ps->a[i] == x)return i;}return -1;//没找到
}//修改
void SLModify(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos < ps->size);ps->a[pos] = x;
}//释放内存
void SLDestory(SL* ps)
{assert(ps != NULL);if (ps->a){free(ps->a);ps->a = NULL;ps->capacity = ps->size = 0;}
}

Test.c:

#include "SeqList.h"
void menu()
{printf("*******************************\n");printf("1.头插  2.尾插	     3.查找 \n");printf("4.删除  5.连续删除   6.修改  \n");printf("7.打印  8.退出 \n");printf("*******************************\n");
}int main()
{//初始化SL sl;SLInit(&sl);int option = -1;int x,y,z,f;do{menu();scanf("%d", &option);int val, pos;switch (option){case 1:printf("请输入要头插的数据,以0结束:>");scanf("%d", &val);while (val!=0){SLPushFront(&sl, val);scanf("%d",&val);}break;case 2:printf("请输入要尾插的数据,以0结束:>");scanf("%d", &val);while (val != 0){SLPushBack(&sl, val);scanf("%d", &val);}break;case 3:printf("请输入要查找的数字:>");scanf("%d", &y);pos = SLFind(&sl, y);if (pos != -1){printf("找到了%d\n", y);}elseprintf("没有找到%d\n", y);SLPrint(&sl);break;case 4:printf("请输入你要删除的值:>");scanf("%d", &x);int pos = SLFind(&sl, x);if (pos != -1){SLErase(&sl, pos);}elseprintf("没有找到%d\n", x);SLPrint(&sl);break;case 5:printf("请输入你要删除的值,并删除所有与之相同的值:>");scanf("%d", &f);pos = SLFind(&sl, f);if (pos != -1){while (pos != -1){SLErase(&sl, pos);pos = SLFind(&sl, f);}}elseprintf("没有找到要删除的值%d\n", f);break;case 6:printf("请输入你要修改的值和修改后的值:>");scanf("%d %d", &y, &z);pos = SLFind(&sl, y);if (pos != -1){SLModify(&sl, pos, z);}elseprintf("没有找到%d\n", y);SLPrint(&sl);break;case 7:SLPrint(&sl);break;case 8:break;default:printf("输入错误,请重新输入\n");break;}} while (option!=8);printf("退出成功\n");//释放内存SLDestory(&sl);return 0;
}

到这里就结束啦,创作不易,求求点个赞啦╰(°▽°)╯

 

http://www.tj-hxxt.cn/news/33774.html

相关文章:

  • 自助网站免费郑州搜狗关键词优化顾问
  • vr 做的网站全球热门网站排名
  • 深圳软件公司工资有多少上海seo推广
  • 北京工程信息网站360推广和百度推广哪个好
  • 园林设计公司网站百度浏览器下载官方免费
  • ui素材网站如何优化关键词排名
  • 上海建个人网站比较好的公司百度风云榜官网
  • 关于做网站常见的问题百度网址浏览大全
  • 产品经理培训网站关键词优化排名公司
  • 华文细黑做网站有版权吗朝阳网站建设公司
  • 网站开发合同 doc今日头条网页版入口
  • html教程电子书什么叫优化关键词
  • 做yy头像的网站百度官网下载
  • 做网站 用什么语言好网络推广服务合同
  • 网站在只有域名了山东网站seo
  • 字牌标识公司网站网站编号 60192024年2月疫情又开始了吗
  • ps ui做响应式网站要求百度行发代理商
  • 网站建设要用多少种字体百度手机网页版入口
  • 成都服装网站建设北京疫情最新数据
  • 爱网站免费一站二站简述网站制作的步骤
  • 免费单页网站建设海外短视频软件
  • 网络浏览器潍坊关键词优化平台
  • 金华专业做网站重庆seo
  • 花钱做网站百度预测大数据官网
  • 中小企业的网站建设论文网站流量查询站长之家
  • 深圳开发的购物网站正规seo关键词排名哪家专业
  • 建网站原型图建设网站
  • 崇信门户网站留言回复百度贴吧网页版登录
  • 网站排名优化外包公司baidu优化
  • 电子商务网站的建设与规划书关键词seo排名优化软件