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

长沙h5网站建设天津网站建站公司

长沙h5网站建设,天津网站建站公司,微网站app制作,wordpress模板 简单一.通讯录的创建 首先我们要理解的是通讯录本身就是以顺序表为底层的 只不过顺序表中的数组,这里我们是用结构体来替代,用来存储用户的信息 由于是通讯录的本质就是顺序表,所以顺序表的任何方法它都能套用 Contact.h: #pragma once #def…

一.通讯录的创建

首先我们要理解的是通讯录本身就是以顺序表为底层的

只不过顺序表中的数组,这里我们是用结构体来替代,用来存储用户的信息

由于是通讯录的本质就是顺序表,所以顺序表的任何方法它都能套用

Contact.h:

#pragma once
#define Name_MAX 20
#define Gender_MAX 20
#define Tel_MAX 20
#define ADDR_MAX 100//定义联系人数据 自定义结构体
//姓名 性别 年龄 电话 地址
typedef struct personInfo
{char name[Name_MAX];char gender[Gender_MAX];int age;char tel[Tel_MAX];char addr[ADDR_MAX];
}peoInfo;//重命名为peoInfo//把顺序表改个名字,叫做通讯录
//前置声明,用结构体SL没命名之前的声明
//SL是在结构体创建后才改的名
typedef struct SeqList Contact;//初始化
void ContactInit(Contact* con);//销毁
void ContactDesTroy(Contact*con);//添加数据
void ContactAdd(Contact* con);//删除
void ContactDel(Contact* con);//修改
void ContactModify(Contact* con);
//
//查找
void ContactFind(Contact* con);
//
//展示
void ContactShow(Contact* con);

SeqList.h:

这个是顺序表本身的运用代码的头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#include<string.h>
//定义顺序表的结构//#define N 100
//
静态顺序表
//struct SeqList
//{
//	int arr[N];
//	int size;//有效数据个数
//};typedef peoInfo SLDataType;
//动态顺序表
typedef struct SeqList
{SLDataType* arr;int size; //有效数据个数int capacity; //空间大小
}SL;//typedef struct SeqList SL;//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//void SLPrint(SL s);//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);void SLPopBack(SL* ps);
void SLPopFront(SL* ps);//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);//查找数据
//int SLFind(SL* ps, SLDataType x);

Contact.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"void ContactInit(Contact* con)
{SLInit(con);
}void ContactDesTroy(Contact* con)
{SLDestroy(con);
}//添加数据
void ContactAdd(Contact* con)
{peoInfo info;printf("请输入联系人姓名:\n");scanf("%s", info.name);printf("请输入联系人性别:\n");scanf("%s", info.gender);printf("请输入联系人年龄:\n");scanf("%d", &info.age);printf("请输入联系人电话:\n");scanf("%s", info.tel);printf("请输入联系人地址:\n");scanf("%s", info.addr);//添加SLPushBack(con,info);
}//以名字为依据查找数据
int FindByName(Contact* con, char name[])
{for (int i = 0; i < con->size; i++){if (0 == strcmp(con->arr[i].name, name))return i;}return -1;
}//删除
void ContactDel(Contact* con)
{//删除的数据一定要存在才能删除//查找(名字为依据)(依据不固定)char name[Name_MAX];printf("请输入你要删除的联系人的名字:\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("删除失败,联系人不存在\n");return;}SLErase(con, find);printf("删除成功\n");
}//展示
void ContactShow(Contact* con)
{printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");for (int i = 0; i < con->size; i++){printf("%3s %3s  %3d  %3s  %3s\n",con->arr[i].name,con->arr[i].gender,con->arr[i].age,con->arr[i].tel,con->arr[i].addr);}
}//修改
void ContactModify(Contact* con)
{char name[Name_MAX];printf("请输入你要修改的联系人的名字:\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("修改失败,联系人不存在\n");return;}printf("请输入新的姓名:\n");scanf("%s", con->arr[find].name);printf("请输入新的性别:\n");scanf("%s", con->arr[find].gender);printf("请输入新的年龄:\n");scanf("%d", &con->arr[find].age);printf("请输入新的电话:\n");scanf("%s", con->arr[find].tel);printf("请输入新的住址:\n");scanf("%s", con->arr[find].addr);printf("修改成功!\n");
}//查找
void ContactFind(Contact* con)
{char name[Name_MAX];printf("请输入要查找的联系人姓名\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("要查找的联系人数据不存在!\n");return;}// 姓名 性别 年龄 电话  地址// 11   11   11   11   11printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%3s   %3s   %3d   %3s   %3s\n", con->arr[find].gender,con->arr[find].age,con->arr[find].tel,con->arr[find].addr);
}

SeqList.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLInit(SL* ps)
{ps->arr = NULL;ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{if (ps->arr) //等价于  if(ps->arr != NULL){free(ps->arr);}ps->arr = NULL;ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{//插入数据之前先看空间够不够if (ps->capacity == ps->size){//申请空间//malloc calloc realloc  int arr[100] --->增容realloc//三目表达式int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//要申请多大的空间if (tmp == NULL){perror("realloc fail!");exit(1);//直接退出程序,不再继续执行}//空间申请成功ps->arr = tmp;ps->capacity = newCapacity;}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{温柔的解决方式//if (ps == NULL)//{//	return;//}assert(ps); //等价与assert(ps != NULL)//ps->arr[ps->size] = x;//++ps->size;SLCheckCapacity(ps);ps->arr[ps->size++] = x;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{assert(ps);SLCheckCapacity(ps);//先让顺序表中已有的数据整体往后挪动一位for (int i = ps->size; i > 0; i--){ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]}ps->arr[0] = x;ps->size++;
}//void SLPrint(SL s)
//{
//	for (int i = 0; i < s.size; i++)
//	{
//		printf("%d ", s.arr[i]);
//	}
//	printf("\n");
//}
void SLPopBack(SL* ps)
{assert(ps);assert(ps->size);//顺序表不为空//ps->arr[ps->size - 1] = -1;--ps->size;
}
void SLPopFront(SL* ps)
{assert(ps);assert(ps->size);//数据整体往前挪动一位for (int i = 0; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1]; //arr[size-2] = arr[size-1]}ps->size--;
}//指定位置之前添加
void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);for (int i = ps->size; i > pos; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;ps->size++;
}//删除指定位置的数据
void SLErase(SL* ps, int pos)
{assert(ps);assert(pos >= 0 && pos < ps->size);for (int i = pos; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1];}ps->size--;
}查找数据
//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	int i = 0;
//	for (int i =0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//			return i;
//	}
//	return -1;
//}

add.c:

这个是代码运行的源文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"//void SLTest01()
//{
//	SL sl;
//	SLInit(&sl);
//	SLPushBack(&sl, 1);
//	SLPushBack(&sl, 2);
//	SLPushBack(&sl, 3);
//	SLPushBack(&sl, 4);
//	SLPrint(sl);//1 2 3 4
//
//	SLPushFront(&sl, 5);
//	SLPushFront(&sl, 6);
//
//	βɾ
//	SLPopBack(&sl);
//	SLPrint(sl);//1 2 3 
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopFront(&sl);
//	SLPrint(sl);
//	...........
//	SLDestroy(&sl);
//}
//
//void SLTest02()
//{
//	SL sl;
//	SLInit(&sl);
//
//
//	SLPushBack(&sl, 1);
//	SLPushBack(&sl, 2);
//	SLPushBack(&sl, 3);
//	SLPushBack(&sl, 4);
//
//
//	/*SLInsert(&sl, 0, 5);*/
//
//	/*SLErase(&sl, 0);*/
//	/*SLPrint(sl);
//	int find=SLFind(&sl, 4);
//	if (find< 0)
//		printf("没找到\n");
//	else
//		printf("找到了,下标是:%d\n",find);*/
//
//
//
//	SLDestroy(&sl);
//}void ContactTest01()
{Contact con;ContactInit(&con);ContactAdd(&con);ContactAdd(&con);ContactShow(&con);/*ContactDel(&con);*/ContactModify(&con);ContactShow(&con);ContactFind(&con);ContactDesTroy(&con);}//int main()
//{
//	/*SLTest01();*///SLTest02();//ContactTest01();
//	return 0;
//}int main()
{ContactTest01();
}

二.移除元素

代码详解:

int removeElement(int* nums, int numsSize, int val) {int str = 0 ;int dst = 0 ;while(str<numsSize){if(nums[str]==val){str++;}else{nums[dst++]=nums[str++];}}return dst;
}

tip:

此处使用双指针的方法,还可以使用新的数组进行替换,但是题目要求受限

三.合并两个有序数组

代码详解:

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {int l1 = m - 1;int l2 = n - 1;int l3 = m + n - 1;while(l1 >= 0 && l2 >= 0 ){if(nums1[l1]<nums2[l2]){nums1[l3--]=nums2[l2--];}else{nums1[l3--]=nums1[l1--];}}while(l2>=0){nums1[l3 -- ] = nums2[l2--];}
}

http://www.dtcms.com/a/450231.html

相关文章:

  • 购物网站开发目的网页设计与制作作业成品
  • RNN在自然语言处理中的应用:文本分类实战(代码演示)
  • 嵌入式开发面试八股文详解教程
  • 图形打印方法:从正方形到三角形的编程实践(洛谷P5725)
  • 阿里云对象存储做静态网站成都装修公司哪家口碑最好
  • kanass入门到实战(9) - 如何自定义事项类型,满足个性化需求
  • 企业商城网站建设在哪里买域名
  • 【11408学习记录】考研数学核心突破:线性代数之线性方程组深度解析
  • 舟山网站建设哪家好网站建设者
  • 个人网站备案简介wordpress alipay
  • 王野电动车名风seo软件
  • 彩网站开发天琥设计
  • 大型网站开发工具洛阳小程序开发公司
  • 一个虚拟空间做两个网站中国建设工程造价管理系统
  • 网站开发与网页制作的区别自助企业建站模板
  • 【LeetCode热题100(35/100)】LRU 缓存
  • 长沙网站seo推广中华商标交易网官方网站
  • 如何利用单北斗变形监测提升地质灾害预警能力?
  • 制作广告网站的步骤加强公司网站建设
  • 同字形结构布局网站电子商务网站开发毕业设计
  • 博物建设公司网站网上找家装设计师
  • 建设网站的多少钱定安网站制作
  • MySQL的MHA高可用集群解决方案应用实战(下)
  • 图说刚体运动概念凸显须重新认识测度论和“点无大小,线无宽度”公理
  • 人防网站建设查国外企业信息的网站
  • 做旅游网站的目的是什么wordpress批量修改文章内的代码
  • 禅城网站建设免费网站制作 优帮云
  • cms网站建设有多少条数据wordpress 框架解析
  • 网站的推广优化赣州网站建设哪家好
  • 智能建站系统怎么更换网站模板wordpress国产主题推荐