c語言版仿c++封閉動態數組

來源:互聯網
上載者:User

#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int DataType;           //是這樣用嗎?

struct DArray
{
 int capacity;            //數組容量
 DataType *parray;           //動態數組其實就是一個指向一塊內在的指標.
 int count;             //已使用的元素個數
 void (*init_array)(struct DArray *pthis, int size);   //初始化數組的大小.
 void (*insert)(struct DArray *pthis, int index, DataType element);
 DataType (*get_element)(struct DArray *pthis, int index);
 void (*change_size)(struct DArray *pthis, int newsize);
 int (*get_capacity)(struct DArray *pthis);
    void (*free)(struct DArray *pthis);

};                //結構體後面的分號一定不能再忘了..

void array_change_size(struct DArray *pthis, int newsize)
{
 int limit = 0;
 int i;
 if((newsize<=0) || (newsize==pthis->capacity))
 {
  printf("array_change_size error!: newsize <=0 || newsize == pthis->capacity /n");
  return;
 }
 DataType *pnew = (DataType*)malloc(sizeof(DataType)*newsize);
 if(pnew==NULL)
 {
  printf("memory exhaust!/n");
  exit(1);
 }
 limit = (newsize > pthis->capacity) ? pthis->capacity : newsize;
 for(i=0; i<limit; i++)
 {
  pnew[i] = (pthis->parray)[i];
 }
 free(pthis->parray);
 pthis->parray = pnew;
 pthis->capacity = newsize;
}

void array_insert(struct DArray *pthis, int index, DataType element)     //在添加元素時就是這樣.
{
  #ifdef DEBUGARRAY                //用於對數組的索引進行調試
  if(index < 0)
  {
   printf("array_insert error : index < 0/n");
   exit(1);
  }
 #endif
 (pthis->parray)[index] = element;
 ++pthis->count;                 //記錄數組中有效元素個數
}

DataType array_get_element(struct DArray *pthis, int index)
{
 #ifdef DEBUGARRAY                //用於對數組的索引進行調試
 if(index < 0)
 {
   printf("array_insert error : index < 0/n");
   exit(1);
 }
 #endif
 --pthis->count;
 return (pthis->parray)[index];
}

void array_free(struct DArray *pthis)
{
  free(pthis->parray);
  pthis->parray = NULL;          //別記了這步很關鍵
  pthis->capacity = 0;
  pthis->count = 0;
}

int array_get_capacity(struct DArray *pthis)
{
 return pthis->capacity;
}

void array_init_array(struct DArray *pthis, int size)
{
 int capacity;
 capacity = (size>1)?size:2;         //數組初始化時容量必須為2的倍數
 pthis->capacity = capacity;
 pthis->parray = (DataType *)malloc(sizeof(DataType)*capacity);
 if(pthis->parray == NULL)
 {
  printf("memory exhoust!/n");
  exit(1);
 }
 pthis->count = 0;
 pthis->init_array = array_init_array;      //初始化所有的函數指標.
 pthis->insert = array_insert;
 pthis->get_element = array_get_element;
 pthis->change_size = array_change_size;
    pthis->free = array_free;
 pthis->get_capacity = array_get_capacity;
}

 

int main()
{
  struct DArray array;
  array_init_array(&array,2);        //初始大小為2的倍數
  for(int i=0; i<1000; i++)
  {
   if(i == array.get_capacity(&array))
   {
    array.change_size(&array, i<<1);    //如果滿了數組容量加大兩倍
   }
   array.insert(&array, i, i);   
  }
  int trysize = array.get_capacity(&array);    //數組容量.
  int num = 0;
  cout<<"trysize:"<<trysize<<endl;
  for(int j=999; j>-1; j--)
  {
   num = j+1;          //元素個數,這種錯誤的原因是馬虎嗎?嗎?嗎?要不就是那個指標的位置沒有加星,在進行代碼copy時一定要注意,變數名問題,腦子一定要想著我在哪裡用過這些變數.
   while(num<=trysize>>2 && trysize>2)
   {
    trysize >>=1;
    cout<<"newsize::"<<trysize<<endl;
   }
   if(trysize<array.get_capacity(&array))
   {
    //cout<<"newsize::"<<trysize<<endl;
    array.change_size(&array, trysize);
   }
   cout<<array.get_element(&array, j)<<"  ";
  }
  cout<<endl;
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.