当前位置:网站首页>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;
	// }
	// }
	//}
//}




原网站

版权声明
本文为[Dare you look at the avatar for three seconds?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020525471686.html