用c语言 简单实现JAVA里面的ArryList 功能,实现内存自动扩展
生活随笔
收集整理的這篇文章主要介紹了
用c语言 简单实现JAVA里面的ArryList 功能,实现内存自动扩展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<stdio.h>
#include<stdlib.h>
struct Arr{
?? ?int * pbase;//數組首地址
?? ?int len;//數組所能容納的總長度
?? ?int cnt;//數組有效個數;
?? ?int increment;//內存增長因子
};
void init(struct Arr *parr,int len);
bool append(struct Arr *parr,int content);
bool append_auto(struct Arr *parr,int content);
bool insert(struct Arr *parr,int pos,int content);
bool delet(struct Arr *parr,int pos,int * deledata);
//int? get();
bool sortAsc_arr(struct Arr *parr);
void show_arr(struct Arr *arr);
bool isEmpty(struct Arr *arr);
bool? isFull(struct Arr *arr);
bool? converse(struct Arr *parr);
void? newrealloc(struct Arr *parr);
int? main(){
?? ?struct Arr arr;
?? ?int deledata;
?? ?init(&arr,3);
??? append_auto(&arr,4);
??? append_auto(&arr,5);
?? ?append_auto(&arr,6);
?? ?append_auto(&arr,7);
//?? ?append(&arr,8);
//?? ?append(&arr,9);
?? ?
?? ?insert(&arr,2, 30);//在pos位置插入數據,pos從1開始
?? ?
?? ?printf("所有數據:");
?? ?show_arr(&arr);
?? ?if(delet(&arr,3,&deledata)){
?? ??? ?printf("刪除元素為:%d\n",deledata);
?? ?}
??? printf("刪除后剩余數據:");
?? ?show_arr(&arr);
?? ?converse(&arr);
?? ?printf("倒置后的數據:");
?? ?show_arr(&arr);
?? ?printf("升序排序后的數據:");
?? ?sortAsc_arr(&arr);
??? show_arr(&arr);
?? ?return 0;
}
void init(struct Arr *parr,int len){
???? parr->pbase=(int *)malloc(sizeof(int)*len);
?? ? if((*parr).pbase==NULL){
?? ??? ? printf("內存分配失敗!");
?? ??? ? exit(-1);
?? ? }else{
?? ?
???? parr->cnt=0;
?? ? parr->len=len;
?}
????? return;
?? ?
}
bool? isFull(struct Arr *parr){
?? ?if(parr->cnt==(*parr).len)
?? ??? ?return true;
?? ?? else
?? ????? return? false;
}
bool isEmpty(struct Arr *parr){
?? ?if(parr->cnt==0)
?? ??? ?return true;
?? ??? else
?? ??? ??? return false;
}
void show_arr(struct Arr * parr){
?? ?? ?
?? ?if(isEmpty(parr)){
? ??? ?printf("數組數據為空!\n");
?? ??? ?
?? ?}else
?? ?
?? ?for(int j=0;j<parr->cnt;j++){
?? ??? ??? ?printf("%3d",(parr->pbase)[j]);
?? ??? ?}
?? ???????? printf("\n");
?? ??? ?
}
//添加數據
bool append(struct Arr * parr,int content){
?? ???? if(isFull(parr))
?? ??? ?return false;
?? ?
?? ??? ?else{
???? int cnt=parr->cnt;
?? ? parr->pbase[cnt]=content;
?? ? parr->cnt=cnt+1;
?? ??? ?}
?? ??? ??? ?return true;
???????? ?
?? ??? ?
} ?
bool? append_auto(struct Arr *parr,int content){
?? ??? ?if(isFull(parr)){
?? ??? ?parr->increment=2;
?? ?
?? ??? ?newrealloc(parr);
?? ?
?? ??? ?}
?????? ?
?? ??? ?int cnt=parr->cnt;
?? ???? parr->pbase[cnt]=content;
?? ???? parr->cnt=cnt+1;
??????? return true;
}
?
bool insert(struct Arr * parr,int pos,int content){
/*?? ??? if(isFull(aprr))?? ?
?? ??? {
?? ?????? printf("數組已滿");
?? ??? ?? return false;
?? ??? }else {
?? ??? ??? for(int i=(parr->cnt)-1;i>=pos-1;i--){
?? ??? ??? ??? (parr->pbase)[i+1]=parr->pbase[i]
?? ??? ??? }
?? ??? }
*/??? ?
?? ?if(isFull(parr))
?? ??? ?return false;
?? ?if(pos<1||pos>parr->cnt+1){
?? ??? ?printf("不符合插入要求");
?? ??? ?return false;
?? ?} else{
?? ??? ?for(int i=parr->cnt-1;i>=pos-1;i--){
?? ??? ??? ?parr->pbase[i+1]=parr->pbase[i];
?? ??? ?}
??????????? parr->pbase[pos-1]=content;
?? ??? ??? ?parr->cnt=parr->cnt+1;
? ?
?? ?}
??????????? ?
?? ??????? return true;
}
bool delet(struct Arr * parr,int pos,int * deledata){
?? ? if(isEmpty(parr))
?? ??? ? return false;
?? ?if(pos<1||pos>parr->len){
?? ??? ?printf("刪除的位置不正確");
?? ??? ?return false;
?? ?}else{
?? ??? ?*deledata=parr->pbase[pos-1];
??????? for(int i=pos;i<parr->cnt;i++)
??????? parr->pbase[i-1]=parr->pbase[i];
?? ?}
?? ???????? parr->cnt=parr->cnt-1;
?? ??? ??? ?return true;
}
bool converse(struct Arr *parr ){
?? ?if(isEmpty(parr)){
????????? printf("沒有數據不能倒置");
?? ??? ?? return false;
?? ?}
?? ??? ?? ?
?? ??? int i=0;
?? ??? int j=parr->cnt-1;
?? ??? while(i<j){
?? ??? ??? int mid=parr->pbase[i];
?? ??? ??? parr->pbase[i]=parr->pbase[j];
?? ??? ??? parr->pbase[j]=mid;
?? ??? ??? i++;
?? ??? ??? j--;
?? ??? }
????????? ?
?? ???????? return true;
}
bool sortAsc_arr(struct Arr *parr){
??? if(isEmpty(parr))
?? ??? ?return false;
?? ?int middata,i,j;
?? ?for( i=0;i<parr->cnt-1;i++){
?? ??? ?for( j=i+1;j<parr->cnt;j++){
?? ??? ??? ?if(parr->pbase[i]>=parr->pbase[j]){
??????????????? middata=parr->pbase[i];
?? ??? ??? ?????? parr->pbase[i]=parr->pbase[j];
?? ??? ??? ??? ?? parr->pbase[j]=middata;
?? ??? ??? ?}
?? ??? ?}
?? ?}
????????????? return true;
}
?????? ?
void newrealloc(struct Arr *parr){
?? ?if(parr->cnt==parr->len){
?? ??? ?parr->pbase=(int *)realloc(parr->pbase,sizeof(int)*(parr->len)*(parr->increment));
?? ??? ?parr->len=(parr->increment)*(parr->len);
?? ?}
?? ??? ?if(parr->pbase==NULL)
?? ??? ?{
?? ??? ??? printf("內存分配失敗");
?? ??? ??? exit(1);
?? ??? ?
?? ??? ?}
?? ??? ?return;
?? ?
}?
#include<stdlib.h>
struct Arr{
?? ?int * pbase;//數組首地址
?? ?int len;//數組所能容納的總長度
?? ?int cnt;//數組有效個數;
?? ?int increment;//內存增長因子
};
void init(struct Arr *parr,int len);
bool append(struct Arr *parr,int content);
bool append_auto(struct Arr *parr,int content);
bool insert(struct Arr *parr,int pos,int content);
bool delet(struct Arr *parr,int pos,int * deledata);
//int? get();
bool sortAsc_arr(struct Arr *parr);
void show_arr(struct Arr *arr);
bool isEmpty(struct Arr *arr);
bool? isFull(struct Arr *arr);
bool? converse(struct Arr *parr);
void? newrealloc(struct Arr *parr);
int? main(){
?? ?struct Arr arr;
?? ?int deledata;
?? ?init(&arr,3);
??? append_auto(&arr,4);
??? append_auto(&arr,5);
?? ?append_auto(&arr,6);
?? ?append_auto(&arr,7);
//?? ?append(&arr,8);
//?? ?append(&arr,9);
?? ?
?? ?insert(&arr,2, 30);//在pos位置插入數據,pos從1開始
?? ?
?? ?printf("所有數據:");
?? ?show_arr(&arr);
?? ?if(delet(&arr,3,&deledata)){
?? ??? ?printf("刪除元素為:%d\n",deledata);
?? ?}
??? printf("刪除后剩余數據:");
?? ?show_arr(&arr);
?? ?converse(&arr);
?? ?printf("倒置后的數據:");
?? ?show_arr(&arr);
?? ?printf("升序排序后的數據:");
?? ?sortAsc_arr(&arr);
??? show_arr(&arr);
?? ?return 0;
}
void init(struct Arr *parr,int len){
???? parr->pbase=(int *)malloc(sizeof(int)*len);
?? ? if((*parr).pbase==NULL){
?? ??? ? printf("內存分配失敗!");
?? ??? ? exit(-1);
?? ? }else{
?? ?
???? parr->cnt=0;
?? ? parr->len=len;
?}
????? return;
?? ?
}
bool? isFull(struct Arr *parr){
?? ?if(parr->cnt==(*parr).len)
?? ??? ?return true;
?? ?? else
?? ????? return? false;
}
bool isEmpty(struct Arr *parr){
?? ?if(parr->cnt==0)
?? ??? ?return true;
?? ??? else
?? ??? ??? return false;
}
void show_arr(struct Arr * parr){
?? ?? ?
?? ?if(isEmpty(parr)){
? ??? ?printf("數組數據為空!\n");
?? ??? ?
?? ?}else
?? ?
?? ?for(int j=0;j<parr->cnt;j++){
?? ??? ??? ?printf("%3d",(parr->pbase)[j]);
?? ??? ?}
?? ???????? printf("\n");
?? ??? ?
}
//添加數據
bool append(struct Arr * parr,int content){
?? ???? if(isFull(parr))
?? ??? ?return false;
?? ?
?? ??? ?else{
???? int cnt=parr->cnt;
?? ? parr->pbase[cnt]=content;
?? ? parr->cnt=cnt+1;
?? ??? ?}
?? ??? ??? ?return true;
???????? ?
?? ??? ?
} ?
bool? append_auto(struct Arr *parr,int content){
?? ??? ?if(isFull(parr)){
?? ??? ?parr->increment=2;
?? ?
?? ??? ?newrealloc(parr);
?? ?
?? ??? ?}
?????? ?
?? ??? ?int cnt=parr->cnt;
?? ???? parr->pbase[cnt]=content;
?? ???? parr->cnt=cnt+1;
??????? return true;
}
?
bool insert(struct Arr * parr,int pos,int content){
/*?? ??? if(isFull(aprr))?? ?
?? ??? {
?? ?????? printf("數組已滿");
?? ??? ?? return false;
?? ??? }else {
?? ??? ??? for(int i=(parr->cnt)-1;i>=pos-1;i--){
?? ??? ??? ??? (parr->pbase)[i+1]=parr->pbase[i]
?? ??? ??? }
?? ??? }
*/??? ?
?? ?if(isFull(parr))
?? ??? ?return false;
?? ?if(pos<1||pos>parr->cnt+1){
?? ??? ?printf("不符合插入要求");
?? ??? ?return false;
?? ?} else{
?? ??? ?for(int i=parr->cnt-1;i>=pos-1;i--){
?? ??? ??? ?parr->pbase[i+1]=parr->pbase[i];
?? ??? ?}
??????????? parr->pbase[pos-1]=content;
?? ??? ??? ?parr->cnt=parr->cnt+1;
? ?
?? ?}
??????????? ?
?? ??????? return true;
}
bool delet(struct Arr * parr,int pos,int * deledata){
?? ? if(isEmpty(parr))
?? ??? ? return false;
?? ?if(pos<1||pos>parr->len){
?? ??? ?printf("刪除的位置不正確");
?? ??? ?return false;
?? ?}else{
?? ??? ?*deledata=parr->pbase[pos-1];
??????? for(int i=pos;i<parr->cnt;i++)
??????? parr->pbase[i-1]=parr->pbase[i];
?? ?}
?? ???????? parr->cnt=parr->cnt-1;
?? ??? ??? ?return true;
}
bool converse(struct Arr *parr ){
?? ?if(isEmpty(parr)){
????????? printf("沒有數據不能倒置");
?? ??? ?? return false;
?? ?}
?? ??? ?? ?
?? ??? int i=0;
?? ??? int j=parr->cnt-1;
?? ??? while(i<j){
?? ??? ??? int mid=parr->pbase[i];
?? ??? ??? parr->pbase[i]=parr->pbase[j];
?? ??? ??? parr->pbase[j]=mid;
?? ??? ??? i++;
?? ??? ??? j--;
?? ??? }
????????? ?
?? ???????? return true;
}
bool sortAsc_arr(struct Arr *parr){
??? if(isEmpty(parr))
?? ??? ?return false;
?? ?int middata,i,j;
?? ?for( i=0;i<parr->cnt-1;i++){
?? ??? ?for( j=i+1;j<parr->cnt;j++){
?? ??? ??? ?if(parr->pbase[i]>=parr->pbase[j]){
??????????????? middata=parr->pbase[i];
?? ??? ??? ?????? parr->pbase[i]=parr->pbase[j];
?? ??? ??? ??? ?? parr->pbase[j]=middata;
?? ??? ??? ?}
?? ??? ?}
?? ?}
????????????? return true;
}
?????? ?
void newrealloc(struct Arr *parr){
?? ?if(parr->cnt==parr->len){
?? ??? ?parr->pbase=(int *)realloc(parr->pbase,sizeof(int)*(parr->len)*(parr->increment));
?? ??? ?parr->len=(parr->increment)*(parr->len);
?? ?}
?? ??? ?if(parr->pbase==NULL)
?? ??? ?{
?? ??? ??? printf("內存分配失敗");
?? ??? ??? exit(1);
?? ??? ?
?? ??? ?}
?? ??? ?return;
?? ?
}?
總結
以上是生活随笔為你收集整理的用c语言 简单实现JAVA里面的ArryList 功能,实现内存自动扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一分利到底是多少?《现代汉语词典》早有解
- 下一篇: C语言写链表