Data Structures and Algorithms linear table (sequential storage)

First, data structures, surely it should be more understanding, but for its code to achieve it? It may be a bit harder.

Review the honor today to get some useful study notes, hoping to be useful to you.

 

Sequentially stores (dynamic array implementation)

Ideas: first order for a linked list, as we go to line up to buy movie tickets, like, at the physical level are linked together one by one, and also the logical layer, so for this case, the array is a better choice but when considering the storage, we do not know what the size of the future will be stored, so a lot of time if a fixed array size will result in use of the program is small, so the use of dynamic array is a better choice. I personally looked at learning some information about dynamic arrays, it is recommended here in this blog, written not bad.

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

// dynamic growth of memory, memory data storage strategy will be there? Heap mountains
// dynamic array
// capacity of the capacity of this memory space to express my total of how many elements can be stored
// size concept record specific number of elements in the array current

// definition of a dynamic array structure
typedef struct {DYNAMICARRY
int * pAddr; // store specific address data (dynamic array)
int size; // current number of elements
int capacity; the current maximum capacity of the capacity of the container //
} Dynamic_Array ;

 

// Initialize
Dynamic_Array * Init_Array () {

Dynamic_Array* myArray = (Dynamic_Array*)malloc(sizeof(Dynamic_Array));
if (myArray == NULL)
return NULL;
myArray->size = 0;
myArray->capacity = 20;
myArray->pAddr = (int*)malloc(sizeof(int) * myArray->capacity);
return myArray;
}


//插入
void PushBack_Array(Dynamic_Array* arr, int value) {

IF (ARR == NULL) {
return;
}
// determines whether sufficient space
IF (arr-> arr- size ==> Capacity) {
// a first step of applying a larger memory space is two new old space times
int * Newspace = (int *) the malloc (the sizeof (int) * arr-> Capacity * 2);
// copy data to the second step Reviews space
memcpy (newSpace, arr-> pAddr, arr-> capacity * sizeof ( int));
// third step to release memory space
Free (arr-> pAddr);
// update capacity
arr-> capacity = arr-> capacity * 2;
arr-> pAddr = Newspace;
}
// insert a new element
arr-> pAddr [arr-> size] = value;
arr-> size ++;

}
// delete the position
void RemoveByPos_Array (ARR Dynamic_Array *, int POS) {
IF (ARR == NULL) {
return;
}

if (pos >= arr->size || pos < 0) {
return;
}

//删除某位置的数
for (int i = pos; i < arr->size - 1; i++) {
arr->pAddr[i] = arr->pAddr[i + 1];
}

arr->size--;
}
//根据值第一次出现的删除
void RemoveByValue_Array(Dynamic_Array* arr, int value) {
if (arr == NULL) {
return;
}
int pos = Find_Array(arr, value);
//根据位置删除
RemoveByPos_Array(arr, pos);

}
//查找
int Find_Array(Dynamic_Array* arr, int value) {
if (arr == NULL) {
return -1;
}

int pos = -1;
for (int i = 0; i < arr->size; i++)
if (arr->pAddr[i] == value) {
pos = i+1;
break;
}

return pos;
}
//打印
void Print_Array(Dynamic_Array* arr) {
for (int i = 0; i < arr->size; i++) {
printf("%d ", arr->pAddr[i]);
}
printf("\n");
}
//释放动态数组的内存
void FreeSpace_Array(Dynamic_Array* arr) {
if (arr == NULL) {
return;
}

if (arr->pAddr!= NULL) {
free(arr->pAddr);
}
free(arr);
}
//清空数组
void Clear_Array(Dynamic_Array* arr) {
if (arr == NULL) {
return;
}

arr->size = 0;

}
// 获得动态数组当前的元素个数
int Size_Array(Dynamic_Array* arr) {
if (arr == NULL) {
return -1;
}

return arr->size;
}
//获得动态数组当前的容量
int Capacity_Array(Dynamic_Array* arr) {
if (arr == NULL) {
return -1;
}

 

return arr->capacity;
}
//根据位置获得某个位置元素
int At_Array(Dynamic_Array* arr, int pos) {

return arr->pAddr[pos];
}

 

Guess you like

Origin www.cnblogs.com/Justina/p/11432509.html