用c实现部分java数组功能,很烂,留个参考吧
生活随笔
收集整理的這篇文章主要介紹了
用c实现部分java数组功能,很烂,留个参考吧
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include<stdio.h>
#include<malloc.h>
void initMyarry(struct Myarry *arr);
int isFull(struct Myarry *arr);
void expandMyarry(struct Myarry *arr);
void show(struct Myarry * arr);
void add(struct Myarry *arr,int data);
struct Myarry{
? ?//數(shù)組的首地址
? ?int * firstAddress;
? ? //當(dāng)前數(shù)組的大小
? ?int currentLength;
? ? //初始化數(shù)組大小
? ?int initLength;
? ? //當(dāng)前元素
? ?int current;
? //增長因子
? ?double times;
};
main(){
? struct Myarry myarry;
?//初始化數(shù)組
? initMyarry(&myarry);
? //增加元素
? int i = 1 ;
? for( ; i <=25 ; i++){
? ? ? add(&myarry,i);
? }
?//show出元素
? show(&myarry);
}
//判斷數(shù)組是否已經(jīng)滿了
int isFull(struct Myarry *arr){
? if(arr->current > arr->currentLength){
? ? return 1;
? }else{
? ? return ?0;
? }
}
//按照增長因子來增加數(shù)組長度
void expandMyarry(struct Myarry *arr){
? ?printf("expandMyarry");
? //開辟新的空間
? ?int spaceSize = arr->currentLength + arr->initLength * arr->times ;
? ?printf("spaceSize = %d\n",spaceSize);
? ?int * newSpaceAddress = (int * ) malloc(sizeof(int) * spaceSize );
? ?if(newSpaceAddress==NULL){
? ? ?printf("has no nough space for newSpaceAddress ");
? ? ?exit(1);
? ?}else{
? ? ? ? //復(fù)制元素
? ? ? int i = 0;
? ? ? for(; i < arr->current ; i++){
? ? ? ? *(newSpaceAddress + i ) = ?*(arr->firstAddress + i );
? ? ? }
? ? ? arr->currentLength = spaceSize;
? ? ? arr->firstAddress = newSpaceAddress;
? ?}
}
//在數(shù)組末尾插入數(shù)據(jù)
void add(struct Myarry *arr,int data){
? ?// printf("data = %d",data);
? ? //判斷數(shù)組是否已經(jīng)滿0
? ? ?// 1 是
? ? ?if( isFull(arr)){
? ? ? ? expandMyarry(arr);
? ? ? ? add(arr,data);
? ? ?}
? ? ? // 2 否
? ? ?else{
? ? ? ? ?printf("\n");
? ? ? ? ? *( arr->firstAddress + arr->current ) = ?data;
? ? ? ? ? arr->current += 1;
? ? ?}
}
//顯示數(shù)組里面的值
void show(struct Myarry * arr){
? ? int i = 0;
? ? for(;i < arr->current ; i++){
? ? ? ? printf("arr[%d] = %d\n" ,i , *(arr->firstAddress + i));
? ? }
}
//初始化數(shù)組
void initMyarry(struct Myarry * arr){
? ? //當(dāng)前數(shù)組大小為20
? ? arr->currentLength = 20;
? ? //初始化數(shù)組大小
? ? arr->initLength = 20;
? ? //增長因子為1.5
? ? arr->times = 1.5;
? ? //當(dāng)前元素為0
? ? arr->current=0;
? ? //開辟空間
? ? arr->firstAddress = (int *)malloc(sizeof(int)*(arr->currentLength));
? ? ?if(arr->firstAddress == NULL){
? ? ? ? printf("has no enough space");
? ? }
}
#include<malloc.h>
void initMyarry(struct Myarry *arr);
int isFull(struct Myarry *arr);
void expandMyarry(struct Myarry *arr);
void show(struct Myarry * arr);
void add(struct Myarry *arr,int data);
struct Myarry{
? ?//數(shù)組的首地址
? ?int * firstAddress;
? ? //當(dāng)前數(shù)組的大小
? ?int currentLength;
? ? //初始化數(shù)組大小
? ?int initLength;
? ? //當(dāng)前元素
? ?int current;
? //增長因子
? ?double times;
};
main(){
? struct Myarry myarry;
?//初始化數(shù)組
? initMyarry(&myarry);
? //增加元素
? int i = 1 ;
? for( ; i <=25 ; i++){
? ? ? add(&myarry,i);
? }
?//show出元素
? show(&myarry);
}
//判斷數(shù)組是否已經(jīng)滿了
int isFull(struct Myarry *arr){
? if(arr->current > arr->currentLength){
? ? return 1;
? }else{
? ? return ?0;
? }
}
//按照增長因子來增加數(shù)組長度
void expandMyarry(struct Myarry *arr){
? ?printf("expandMyarry");
? //開辟新的空間
? ?int spaceSize = arr->currentLength + arr->initLength * arr->times ;
? ?printf("spaceSize = %d\n",spaceSize);
? ?int * newSpaceAddress = (int * ) malloc(sizeof(int) * spaceSize );
? ?if(newSpaceAddress==NULL){
? ? ?printf("has no nough space for newSpaceAddress ");
? ? ?exit(1);
? ?}else{
? ? ? ? //復(fù)制元素
? ? ? int i = 0;
? ? ? for(; i < arr->current ; i++){
? ? ? ? *(newSpaceAddress + i ) = ?*(arr->firstAddress + i );
? ? ? }
? ? ? arr->currentLength = spaceSize;
? ? ? arr->firstAddress = newSpaceAddress;
? ?}
}
//在數(shù)組末尾插入數(shù)據(jù)
void add(struct Myarry *arr,int data){
? ?// printf("data = %d",data);
? ? //判斷數(shù)組是否已經(jīng)滿0
? ? ?// 1 是
? ? ?if( isFull(arr)){
? ? ? ? expandMyarry(arr);
? ? ? ? add(arr,data);
? ? ?}
? ? ? // 2 否
? ? ?else{
? ? ? ? ?printf("\n");
? ? ? ? ? *( arr->firstAddress + arr->current ) = ?data;
? ? ? ? ? arr->current += 1;
? ? ?}
}
//顯示數(shù)組里面的值
void show(struct Myarry * arr){
? ? int i = 0;
? ? for(;i < arr->current ; i++){
? ? ? ? printf("arr[%d] = %d\n" ,i , *(arr->firstAddress + i));
? ? }
}
//初始化數(shù)組
void initMyarry(struct Myarry * arr){
? ? //當(dāng)前數(shù)組大小為20
? ? arr->currentLength = 20;
? ? //初始化數(shù)組大小
? ? arr->initLength = 20;
? ? //增長因子為1.5
? ? arr->times = 1.5;
? ? //當(dāng)前元素為0
? ? arr->current=0;
? ? //開辟空間
? ? arr->firstAddress = (int *)malloc(sizeof(int)*(arr->currentLength));
? ? ?if(arr->firstAddress == NULL){
? ? ? ? printf("has no enough space");
? ? }
}
總結(jié)
以上是生活随笔為你收集整理的用c实现部分java数组功能,很烂,留个参考吧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codeblock下载
- 下一篇: c语言 typedef的用法