#include <stdio.h>
#include <stdbool.h>
/**定义一个数组*/
struct ArrayList{
int currentNode;//从1开始(如果从0开始,数组为空的时currentNode等于负整数)
int arrayLenght;
int *pBase;//pBase指向第一个元素的地址
};
/**初始化数组*/
void initArray(struct ArrayList *pArray);
bool insertArray(struct ArrayList *pArray);//插入
bool appendArray(struct ArrayList *pArray, int num);//最后面追加
bool isFull(struct ArrayList *pArray);
bool isEmpty(struct ArrayList *pArray);
void showArray(struct ArrayList *pArray);
int getArray(struct ArrayList *pArray);
bool deleteArray(struct ArrayList *pArray, int *pNum);
bool sortArray(struct ArrayList *pArray);//排序数组
/**倒置数组*/
bool inversionArray(struct ArrayList *pArray);
int main(void) {
struct ArrayList arrary;
initArray(&arrary);
// int num;
// deleteArray(&arrary, &num);
// printf("删除的元素为%d\n", num);
int num = getArray(&arrary);
showArray(&arrary);
return 0;
}
void initArray(struct ArrayList *pArray)
{
printf("请输入数组长度:");
scanf("%d",&pArray->arrayLenght);
printf("请输入数组个数:");
scanf("%d",&pArray->currentNode);
pArray->pBase = (int *)malloc(sizeof(int) * pArray->arrayLenght);
if (NULL == pArray->pBase) {
printf("分配空间失败!");
exit(-1);
}else{
printf("请输入数组元素:\n");
for (int i = 0; i<pArray->currentNode; i++) {
scanf("%d", (pArray->pBase + i));
}
}
return;
}
bool isEmpty(struct ArrayList *pArray)
{
if (pArray->currentNode) {
return false;
}else{
return true;
}
}
bool isFull(struct ArrayList *pArray)
{
if (pArray->arrayLenght == pArray->currentNode) {
return true;
}else{
return false;
}
}
void showArray(struct ArrayList *pArray)
{
if (isEmpty(pArray)) {
printf("这是一个空数组\n");
return;
}
printf("数组长度为%d,数组个数为%d,元素分别为:", pArray->arrayLenght, pArray->currentNode);
for (int i = 0; i<pArray->currentNode; i++) {
printf("%d ", *(pArray->pBase + i));
}
printf("\n");
}
bool appendArray(struct ArrayList *pArray, int num)
{
if (isFull(pArray)) {
printf("数组已满,插入失败\n");
return false;
}
*(pArray->pBase + pArray->currentNode) = num;
pArray->currentNode++;
return true;
}
bool insertArray(struct ArrayList *pArray)
{
if (isFull(pArray)) {
printf("数组已满,插入失败\n");
return false;
}
int insertLocation = 1;//位置从1开始
printf("插入的数组位置为(从1开始):");
scanf("%d", &insertLocation);
if (insertLocation > pArray->currentNode + 1 || insertLocation < 1) {
printf("插入的数组位置不正确\n");
return false;
}
int num = 0;
printf("插入的数组元素为:");
scanf("%d", &num);
for (int i = pArray->currentNode; i>=insertLocation; i--) {
*(pArray->pBase + i) = *(pArray->pBase + i - 1);
}
*(pArray->pBase + insertLocation - 1) = num;
pArray->currentNode++;
return true;
}
bool deleteArray(struct ArrayList *pArray, int *pNum)
{
if (isEmpty(pArray)) {
printf("数组为空,插入失败\n");
return false;
}
int deleteLocation = 1;//位置从1开始
printf("删除的数组位置为(从1开始):");
scanf("%d", &deleteLocation);
if (deleteLocation > pArray->currentNode || deleteLocation < 1) {
printf("删除的数组位置不正确\n");
return false;
}
*pNum = *(pArray->pBase + deleteLocation - 1);
for (int i = deleteLocation; i<=pArray->currentNode; i++) {
*(pArray->pBase + i - 1) = *(pArray->pBase + i);
}
pArray->currentNode--;
return true;
}
bool sortArray(struct ArrayList *pArray)
{
for (int i = 0; i < pArray->currentNode-1; i++) {
for (int j = 0; j < pArray->currentNode - 1 - i; j++) {
if (*(pArray->pBase + j) < *(pArray->pBase + j + 1)) {
int temp = *(pArray->pBase + j);
*(pArray->pBase + j) = *(pArray->pBase + j + 1);
*(pArray->pBase + j + 1) = temp;
}
}
}
return true;
}
bool inversionArray(struct ArrayList *pArray)
{
//方法一:
int i = 0;
while (i < pArray->currentNode - 1 - i) {
int temp = *(pArray->pBase + i);
*(pArray->pBase + i) = *(pArray->pBase + pArray->currentNode - 1 - i);
*(pArray->pBase + pArray->currentNode - 1 - i) = temp;
i++;
}
//方法二:
// for (int i = 0; i<pArray->currentNode; i++) {
// int temp = *(pArray->pBase + i);
// *(pArray->pBase + i) = *(pArray->pBase + pArray->currentNode - 1 - i);
// *(pArray->pBase + pArray->currentNode - 1 - i) = temp;
// if (i >= pArray->currentNode - 1 - i) {
// break;
// return true;
// }
// }
return true;
}
int getArray(struct ArrayList *pArray)
{
if (isEmpty(pArray)) {
printf("数组为空,查找失败\n");
return -1;
}
int findLocation = 1;//位置从1开始
printf("数组位置为(从1开始):");
scanf("%d", &findLocation);
if (findLocation > pArray->currentNode || findLocation < 1) {
printf("查找数组位置不正确\n");
return -1;
}
return *(pArray->pBase + findLocation - 1);
}
数据结构之自制数组及数组操作(二)
最新推荐文章于 2024-02-27 16:18:37 发布