線性表的順序儲存
#ifndef _DYNAMIC_ARRAY_H
#define _DYNAMIC_ARRAY_H
#include <stdlib.h>
typedef struct DynamicArray { int* pAddr; //元素 int size; //長度 int capacity; //容量 }Dynamic_Array;
Dynamic_Array* Init_Array();
void Push_Back_Array(Dynamic_Array* array, int value);
void Remove_Pos_Array(Dynamic_Array* array, int pos);
void Remove_Value_Array(Dynamic_Array* array, int value);
int Find_Array(Dynamic_Array* array, int value);
void Print_Array(Dynamic_Array* array);
void Free_Array(Dynamic_Array* array);
void Clear_Array(Dynamic_Array* array);
int Capacity_Array(Dynamic_Array* array);
int Size_Array(Dynamic_Array* array);
int At_Array(Dynamic_Array* array, int pos);
#endif
#include "DynamicArray.h"
Dynamic_Array* Init_Array() { Dynamic_Array* myarray = (Dynamic_Array*)malloc(sizeof(Dynamic_Array)); myarray->size = 0; myarray->capacity = 20; myarray->pAddr = (int*)malloc(sizeof(int)*myarray->capacity); return myarray; }
void Push_Back_Array(Dynamic_Array* array, int value) //插入元素 { if(array->size == array->capacity) { int* newspace = (int*)malloc(sizeof(int)*array->capacity * 2); memcpy(newspace, array->pAddr, array->capacity*sizeof(int)); free(array->pAddr); // 或者realloc(array->pAddr, sizeof(int)*array->capacity * 2) array->capacity = array->capacity * 2; array->pAddr = newspace;
// 或者僅此一行realloc(array->pAddr, sizeof(int)*array->capacity * 2) } array->pAddr[array->size] = value; array->size++; }
void Remove_Pos_Array(Dynamic_Array* array, int pos) //移除pos位置的資料 { if (array == NULL) { return; } if (pos < 0 || pos >= array->size) { return; } for (int i = pos; i < array->size - 1; i++) { array->pAddr[i] = array->pAddr[i + 1]; } array->size--; }
void Remove_Value_Array(Dynamic_Array* array, int value) //移除vakue值 { if (array == NULL) { return; } for (int i = 0; i < array->size; i++) { if (array->pAddr[i] == value) { Remove_Pos_Array(array, i); break; } } }
int Find_Array(Dynamic_Array* array, int value) //按值查詢 { if (array == NULL) { return -1; } int pos = -1; for (int i = 0; i < array->size; i++) { if (array->pAddr[i] == value) { pos = i; return pos; } } if (pos == -1) { return -1; } }
void Print_Array(Dynamic_Array* array) //列印資料 { for (int i = 0; i < array->size; i++) { printf("%d ", array->pAddr[i]); } printf("\n"); }
void Free_Array(Dynamic_Array* array) { if (array == NULL) { return 0; } else if (array->pAddr != NULL) { free(array->pAddr); } free(array); }
void Clear_Array(Dynamic_Array* array) { if (array == NULL) { return; } array->size = 0; }
int Capacity_Array(Dynamic_Array* array) { if (array == NULL) { return 0; } return array->capacity; }
int Size_Array(Dynamic_Array* array) { if (array == NULL) { return 0; } return array->size; }
int At_Array(Dynamic_Array* array, int pos) //返回特定位置的值 { if (array == NULL) { return 0; } return array->pAddr[pos]; }
#include <stdio.h>
#include "DynamicArray.h"
void text() { Dynamic_Array* myarray = Init_Array(); for (int i = 0; i < 30; i++) { Push_Back_Array(myarray, i); } /*Remove_Value_Array(myarray, 10); Remove_Pos_Array(myarray, 9);*/ Print_Array(myarray); printf("%d\n", Find_Array(myarray, 5)); Free_Array(myarray); } int main() { text(); system("pause"); return 0; }