動態陣列 (Array) -- C 語言

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

[フレーム]

檔案:array.h

#ifndef ARRAY_H
#define ARRAY_H
#include "Lib.h"
typedef struct {
 int size; // 陣列目前的上限 
 int count; // 陣列目前的元素個數 
 void **item; // 每個陣列元素的指標
} Array; // 動態陣列的資料結構 
typedef enum { KEEP_SPLITER, REMOVE_SPLITER } SplitMode;
extern void ArrayTest();
extern Array* ArrayNew(int size);// 建立新陣列 
extern void ArrayFree(Array *array, FuncPtr1 freeFuncPtr); // 釋放該陣列 
extern void ArrayAdd(Array *array, void *item); // 新增一個元素 
extern void ArrayPush(Array *array,void *item); // (模擬堆疊) 推入一個元素 
extern void* ArrayPop(Array *array); //(模擬堆疊) 彈出一個元素 
extern void* ArrayPeek(Array *array); //(模擬堆疊) 取得最上面的元素 
extern void* ArrayLast(Array *array); // 取得最後一個元素 
extern void ArrayEach(Array *array, FuncPtr1 f); //對每個元素都執行 f 函數 
extern Array* split(char *str, char *spliter, SplitMode mode);
#endif

檔案:array.h

#include <stdlib.h>
#include <string.h>
#include "Array.h"
void ArrayTest() {
 char *names[] = { "John", "Mary", "George", "Bob" };
 Array *array = ArrayNew(1);
 int i;
 for (i=0; i<4; i++)
 ArrayAdd(array, names[i]);
 ArrayEach(array, strPrintln);
 printf("ArrayPop()=%s\n", ArrayPop(array));
 printf("ArrayLast()=%s\n", ArrayLast(array));
 for (i=0; i<4; i++) {
 int arrayIdx = ArrayFind(array, names[i], strcmp);
 printf("ArrayFind(%s)=%d\n", names[i], arrayIdx);
 }
 ArrayEach(array, strPrintln);
 ArrayFree(array, NULL);
}
Array* ArrayNew(int size) {
 Array *array = ObjNew(Array, 1);
 array->count = 0;
 array->size = size;
 array->item = ObjNew(void*, array->size);
 return array;
}
void ArrayFree(Array *array, FuncPtr1 freeFuncPtr) {
 if (array == NULL) return;
 if (freeFuncPtr != NULL)
 ArrayEach(array, freeFuncPtr);
 ObjFree(array->item);
 ObjFree(array);
}
void ArrayAdd(Array *array, void *item) {
 ASSERT(array->count <= array->size);
 if (array->count == array->size) {
 int newSize = array->size*2;
 void **newItems = ObjNew(void*, newSize);
 memcpy(newItems, array->item, array->size*sizeof(void*));
// printf("array grow from %d to %d\n", array->count, newSize);
 ObjFree(array->item);
 array->item = newItems;
 array->size = newSize;
 }
 array->item[array->count++] = item;
//printf("add item = %s\n", item);
}
void ArrayPush(Array *array, void *item) {
 ArrayAdd(array, item);
}
void* ArrayPop(Array *array) {
 ASSERT(array->count>0);
 return array->item[--(array->count)];
}
void* ArrayPeek(Array *array) {
 return ArrayLast(array);
}
void* ArrayLast(Array *array) {
 ASSERT(array->count > 0);
 return array->item[array->count-1];
}
Array* split(char *str, char *spliter, SplitMode mode) {
 Array *tokens = ArrayNew(10);
 int si, tokenCount=0;
 int begin=0, ti = 0;
 for (si=0; si<=strlen(str); si++) {
 if (str[si]=='0円' || strMember(str[si], spliter)) {
 int len = si-begin;
 if (len > 0)
 ArrayAdd(tokens, newSubstr(str, begin, len));
 if (mode == KEEP_SPLITER)
 ArrayAdd(tokens, newSubstr(str, si, 1));
// printf("token1=%s token2=%s\n", tokens->item[ti-2], tokens->item[ti-1]);
 begin = si+1;
 }
 }
 return tokens;
}
int ArrayFind(Array *array, void *data, FuncPtr2 fcmp) {
 int i;
 for (i=0; i<array->count; i++)
 if (fcmp(array->item[i], data)==0)
 return i;
 return -1;
}
void ArrayEach(Array *array, FuncPtr1 f) {
 int i;
 for (i=0; i<array->count; i++)
 f(array->item[i]);
}

Facebook

[フレーム]

Facebook

[フレーム]

Wikidot

Post preview:

(will not be published)


本網頁的作者、授權與引用方式

作者
陳鍾誠,於金門大學資訊工程系,電子郵件:wt.ude.uqn|ccc#wt.ude.uqn|ccc,網站:http://ccckmit.wikidot.com
授權
本文採用創作共用 (Creative Common) 3.0 版的 姓名標示─非商業性─相同方式分享 授權條款,歡迎轉載或修改使用,但若做為商業使用時必須取得授權,引用本文時請參考下列格式。
中文版 (APA格式)
陳鍾誠 (20 Oct 2010 06:28),(網頁標題) 動態陣列 (Array) — C 語言,(網站標題) 陳鍾誠的網站,取自 http://ccckmit.wikidot.com/code:array ,網頁修改第 1 版。
英文版 (APA格式)
Chung-Chen Chen (20 Oct 2010 06:28), Retrieved from http://ccckmit.wikidot.com/code:array , Page Revision 1.
page revision: 1, last edited: 20 Oct 2010 06:30
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /