当前位置:网站首页>Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
2022-06-11 06:46:00 【Dare you look at the avatar for three seconds?】
This is an exercise before refactoring
Use linear continuous storage structure to write an information management program
requirement Try to improve reusability , Secondly, because of the disadvantage of using this storage structure , Here, dynamic memory allocation is used to make up for this
function : Features include : initialization , Additional data , Insert data from a location , Delete data , Judge whether the data is empty , Sort data , Traverse the output data , Invert data
Code display :
Preparatory work
To begin with, you must initialize the data , I defined a structure before the initial words ( class ) It contains three members , Namely :
struct Arr
{
int* pBase; // The first address of the first element of the array is stored
int len; // The maximum number of elements that the array can hold
int cnt; // The number of valid elements in the current array
//int increment; // Automatic growth factor
};
This structure is defined to allow me to better operate on arrays , Let's talk about it later .
initialization
void init_arr(struct Arr* parr,int len) // Initialize structure
{
//parr->len=99;
parr->pBase = (int*)malloc(sizeof(int) * len);
if (NULL == parr->pBase)
{
printf(" Dynamic memory allocation failed ");
exit(-1); // End
}
else
{
parr->cnt = 0;
parr->len = len;
}
return;
}
In fact, initialization requires the user to give an array length that the user needs , Dynamically create space in memory , This completes the initialization , Here, you need to judge whether the memory is successfully opened , And determine whether the returned address is empty , If it is empty, it means there is no success , Otherwise, we can initialize the length and the number of effective elements in the structure
Traverse the output data
I compare lazy , I think this function is relatively simple, so I wrote this first , Of course, it makes no difference to write first and then write
void show_arr(struct Arr* parrshow) // The output array
{
if (is_empty(parrshow))
printf(" Array is empty \n");
else
{
//printf("%d %d %s", parrshow->cnt, parrshow->len, parrshow->pBase);
for (int i = 0; i < parrshow->cnt; ++i)
{
//printf("%d", *(parrshow[i].pBase));
printf("%d\t", (parrshow->pBase)[i]);
//printf("%d", parrshow->pBase[i]);--ok
}
}
printf("\n");
}
Traversing the output array , This should not be too simple , Don't explain , Just be careful , We need to determine whether the array is empty before traversing the output , Although we have created the array according to the user's requirements, it is still garbage before output , So we must first determine whether there is a valid array inside , The machine determines whether it is empty
Determine whether the array is empty
bool is_empty(struct Arr* parrempty)// Determine whether it is null
{
if (0 == parrempty->cnt)
return true;
else
return false;
}
Additional data
There are two ways to add data , One is to append data , One is that the two methods of inserting data are different , Let's have a simple one first —— Add data after the last valid data of the data , At this point, we need to consider whether the array is full , Another thing to consider is , Adding elements may succeed or fail , So we need to return a Boolean value to remind the user whether the addition is successful
bool append_arr(struct Arr* parr, int val)// Append data to the array
{
// Return when full false
if (is_full(parr))
return false;
else// When dissatisfied, add
{
parr->pBase[parr->cnt] = val;
parr->cnt ++;
return true; // Indicates that the element is successfully added
}
}
Determine if the array is full
This is simpler , Of course, it involves returning Boolean values , That means the program can be lazy
The following is laziness ( perfect ) Writing
bool is_full(struct Arr* parr)// Determine if the array is full
{
return(parr->cnt == parr->len);
}
And don't be lazy ( Not perfect ) Writing :
bool is_full(struct Arr* parr)// Determine if the array is full
{
// Bad code
if (parr->cnt == parr->len)
return true;
else
return false;
}
Of course, I chose the last one .
insert data
Combined with the heat of additional data , We started thinking about how to insert data into an array
The first is to judge whether the data is full , If it's slow, you can't insert it .
The second is to understand that data cannot be inserted randomly , Only the number of valid data can be checked +1 Before , For example, there are only two elements in your array , But you want to insert the subscript 4 The location of , Then it won't work
Secondly, if you insert it , To move the insertion position and subsequent valid data back by one bit
Finally, the location given by the user may be illegal , You should also think of
Look at the results :
bool insert_arr(struct Arr* parr, int pos, int val)
{
if (is_full(parr) || pos<1 || pos>parr->cnt+1) //
return false;
else
{
int i;
for (i = parr->cnt - 1;i>=pos-1;--i)
{
parr->pBase[i+1] = parr->pBase[i];
}
parr->pBase[pos - 1] = val;
parr->cnt++;
return true;
}
}
Delete specified location data
Deleting data is similar to inserting data
bool delete_arr(struct Arr* parr, int pos, int* pval)
{
if (is_empty(parr) || pos<1 || pos>parr->cnt)
return false;
else
{
*pval = parr->pBase[pos - 1];
for (int i = pos; i < parr->cnt; ++i)
{
parr->pBase[i - 1] = parr->pBase[i];
}
parr->cnt--;
return true;
}
}
Data inversion
void inversion_arr(struct Arr* parr)
{
int i = 0;
int j = parr->cnt - 1;
int t;
while (i < j)
{
t = parr->pBase[i];
parr->pBase[i] = parr->pBase[j];
parr->pBase[j] = t;
++i;
--j;
}
return;
}
This was introduced before , It's very simple, not much
Sort
Whether it's a simple choice or bubbling or simple + Bubbles are OK ,
void sort_arr(struct Arr* parr)
{
int i, j,t;
for (i = 0; i < parr->cnt-1; ++i)
{
for (j = i+1; j <= parr->cnt-1;++j)
{
if (parr->pBase[i] > parr->pBase[j])
{
t = parr->pBase[i];
parr->pBase[i] = parr->pBase[j];
parr->pBase[j] = t;
}
}
}
}
//void sort_arr(struct Arr* parr)
//{
// int i, j, t;
// for (i = 0; i < parr->cnt - 1; ++i)
// {
// for (j = i; j < parr->cnt - 1-i; ++j)
// {
// if (parr->pBase[i] > parr->pBase[j+1])
// {
// t = parr->pBase[i];
// parr->pBase[i] = parr->pBase[j+1];
// parr->pBase[j+1] = t;
// }
// }
// }
//}
//void sort_arr(struct Arr* parr)
//{
//int i, j, t;
// for (i = 0; i < parr->cnt - 1; ++i)
//{
// for (j = 0; j < parr->cnt - 1 - i; ++j)
// {
// if (parr->pBase[j] > parr->pBase[j + 1])
// {
// t = parr->pBase[j];
// parr->pBase[j] = parr->pBase[j + 1];
// parr->pBase[j + 1] = t;
// }
// }
//}
//}
This previous article also has , Not much to say
Source code
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<ostream>
#include<stdlib.h> // Contains exit function
struct Arr
{
int* pBase; // The first address of the first element of the array is stored
int len; // The maximum number of elements that the array can hold
int cnt; // The number of valid elements in the current array
//int increment; // Automatic growth factor
};
void init_arr(struct Arr * parr,int len);// initialization
bool append_arr(struct Arr * parr,int val);// Additional
bool insert_arr(struct Arr* parr, int pos,int val);// Insert pos From 1 Start
bool delete_arr(struct Arr* parr, int pos,int*pval);// Delete Examination rate
bool is_empty(struct Arr* parrempty);// Determine whether it is null
bool is_full(struct Arr* parr);// Judge whether it is full
void sort_arr(struct Arr* parr);// Sort
void show_arr(struct Arr*parrshow);// Output
void inversion_arr(struct Arr* parr);// The horse
int main(void)
{
struct Arr arr;
int len = 6;
int val;
init_arr(&arr,len);
show_arr(&arr);// Output
//printf("%d", arr.len);
append_arr(&arr, 44);
append_arr(&arr, 21);
append_arr(&arr, 111);
append_arr(&arr, 3);
//append_arr(&arr, 5);
if (append_arr(&arr, 66))
printf(" Append succeeded \n");
else
printf(" Append failed \n");
// Insert
insert_arr(&arr, 1, 99);
if (append_arr(&arr, 7))
printf(" Append succeeded \n");
else
printf(" Append failed \n");
insert_arr(&arr, 3, 199);
if (delete_arr(&arr, 1, &val))
{
printf(" Delete successful ,, The element you deleted is %d\n",val);
}
else
printf(" Delete failed balls \n");
if (delete_arr(&arr, 5, &val))
{
printf(" Delete successful ,, The element you deleted is %d\n", val);
}
else
printf(" Delete failed balls \n");
if (delete_arr(&arr, 5, &val))
{
printf(" Delete successful ,, The element you deleted is %d\n", val);
}
else
printf(" Delete failed balls \n");
//append_arr(&arr, 7);
show_arr(&arr);// Output
inversion_arr(&arr);// The horse
show_arr(&arr);// Output
sort_arr(&arr);// Sort
show_arr(&arr);// Output
return 0;
}
void init_arr(struct Arr* parr,int len) // Initialize structure
{
//parr->len=99;
parr->pBase = (int*)malloc(sizeof(int) * len);
if (NULL == parr->pBase)
{
printf(" Dynamic memory allocation failed ");
exit(-1); // Terminate the whole program
}
else
{
parr->cnt = 0;
parr->len = len;
}
return;
}
void show_arr(struct Arr* parrshow) // The output array
{
if (is_empty(parrshow))
printf(" Array is empty \n");
else
{
//printf("%d %d %s", parrshow->cnt, parrshow->len, parrshow->pBase);
for (int i = 0; i < parrshow->cnt; ++i)
{
//printf("%d", *(parrshow[i].pBase));
printf("%d\t", (parrshow->pBase)[i]);
//printf("%d", parrshow->pBase[i]);--ok
}
}
printf("\n");
}
bool is_empty(struct Arr* parrempty)// Determine whether it is null
{
if (0 == parrempty->cnt)
return true;
else
return false;
}
bool append_arr(struct Arr* parr, int val)// Append data to the array
{
// Return when full false
if (is_full(parr))
return false;
else// When dissatisfied, add
{
parr->pBase[parr->cnt] = val;
parr->cnt ++;
return true; // Indicates that the element is successfully added
}
}
bool is_full(struct Arr* parr)// Determine if the array is full
{
// Bad code
//if (parr->cnt == parr->len)
// return true;
//else
// return false;
return(parr->cnt == parr->len);
}
bool insert_arr(struct Arr* parr, int pos, int val)
{
if (is_full(parr) || pos<1 || pos>parr->cnt+1) //
return false;
else
{
int i;
for (i = parr->cnt - 1;i>=pos-1;--i)
{
parr->pBase[i+1] = parr->pBase[i];
}
parr->pBase[pos - 1] = val;
parr->cnt++;
return true;
}
}
bool delete_arr(struct Arr* parr, int pos, int* pval)
{
if (is_empty(parr) || pos<1 || pos>parr->cnt)
return false;
else
{
*pval = parr->pBase[pos - 1];
for (int i = pos; i < parr->cnt; ++i)
{
parr->pBase[i - 1] = parr->pBase[i];
}
parr->cnt--;
return true;
}
}
void inversion_arr(struct Arr* parr)
{
int i = 0;
int j = parr->cnt - 1;
int t;
while (i < j)
{
t = parr->pBase[i];
parr->pBase[i] = parr->pBase[j];
parr->pBase[j] = t;
++i;
--j;
}
return;
}
// Three ways to write bubble sort ( Two kinds of substance )
void sort_arr(struct Arr* parr)
{
int i, j,t;
for (i = 0; i < parr->cnt-1; ++i)
{
for (j = i+1; j <= parr->cnt-1;++j)
{
if (parr->pBase[i] > parr->pBase[j])
{
t = parr->pBase[i];
parr->pBase[i] = parr->pBase[j];
parr->pBase[j] = t;
}
}
}
}
//void sort_arr(struct Arr* parr)
//{
// int i, j, t;
// for (i = 0; i < parr->cnt - 1; ++i)
// {
// for (j = i; j < parr->cnt - 1-i; ++j)
// {
// if (parr->pBase[i] > parr->pBase[j+1])
// {
// t = parr->pBase[i];
// parr->pBase[i] = parr->pBase[j+1];
// parr->pBase[j+1] = t;
// }
// }
// }
//}
//void sort_arr(struct Arr* parr)
//{
//int i, j, t;
// for (i = 0; i < parr->cnt - 1; ++i)
//{
// for (j = 0; j < parr->cnt - 1 - i; ++j)
// {
// if (parr->pBase[j] > parr->pBase[j + 1])
// {
// t = parr->pBase[j];
// parr->pBase[j] = parr->pBase[j + 1];
// parr->pBase[j + 1] = t;
// }
// }
//}
//}
边栏推荐
- Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function
- Résoudre le problème de la durée inexacte du fichier audio AAC obtenu par ffmpeg
- About the principle and code implementation of Siou (review IOU, giou, Diou, CIO)
- Post exam summary
- 021-MongoDB数据库从入门到放弃
- Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand
- Redux learning (III) -- using Redux saga, writing middleware functions, and splitting reducer files
- 双周投融报:资本抢滩元宇宙游戏
- Text overflow failure
- 538.把二叉搜索树转换成累加树
猜你喜欢

关于 QtCreator的设计器QtDesigner完全无法正常拽托控件 的解决方法

FPGA interview topic notes (I) - FPGA development process, metastable state and competitive risk, build and hold time, asynchronous FIFO depth, etc

During unity panoramic roaming, AWSD is used to control lens movement, EQ is used to control lens lifting, and the right mouse button is used to control lens rotation.

235-二叉搜索树的最近公共祖先

解决ffmpeg获取AAC音频文件duration不准

Mongodb installation

Redux learning (III) -- using Redux saga, writing middleware functions, and splitting reducer files

617. merge binary tree

CCS安装编译器的方法

A piece of code has been refactored six times by the boss, and my mind is broken
随机推荐
JVM from getting started to abandoning 1: memory model
Alias the path with the help of craco
Check whether the filing information of the medical representative is correct
JS two methods to determine whether there are duplicate values in the array
SQL language - query statement
Oracle提示无效数字
Array de duplication....
统计某次操作(函数)耗时时长
Sentinel annotation support - @sentinelresource usage details
Throttling and anti shake
Implementation of customization function page of online Fox game server room configuration wizard service
CCS安装编译器的方法
ijkPlayer中的错误码
数组去重。。。。
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
021-MongoDB数据库从入门到放弃
572. subtree of another tree
Multimedia框架解析之MediaExtractor源码分析(一)
必读1:格局越大的人,越懂得说话
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically