链表是线性数据结构中的一种,线性数据结构是指用接点关联起来;它包含连续存入--数组和离散存储--链表:最常用的应用如栈、队列
数组:
元素类型相同,大小相等(指存储的类型相等)
php lua 和c数组的比较
1、定义一个数组
php $arr = array(); 可以不指定大小直接指定,而且后续可以直接添加
lua arr = {} 不需要加“;”号,不需要指定大小,它是一个特殊的数组
C struct Arr arr; init(&arr, 6); 需要在初始化的时候就指定大小,而且长度在后续不可变,如果需要可变,则需要在数组类中指定可变因子函数来增加数组的长度
2、赋值
php $arr[] = 1 ; php直接指定,且下标是从0开始
lua arr[] = 1 lua直接指定,但下标是从1开始
C append(&arr, 1); c语言需要用方法添加,查询的索引是从0开始
3、得到长度
php count($arr) ; 自带的函数直接获取
lua #arr; 得到的长度只能是从1开始的下标,而且长度不一定全是“数组的元素个数”,可能还包含其它
C pArr->pBase->cnt; 在数组的类中,数组的长度是放在一个变量当中,对数组每做一步操作就要去观察数组的长度是否发生变化,并更新数组长度的值
上面是主要的最主要的区别
下面是c语言自己的一个数组类,写的不是怎么的好,但简单,希望对了解数组的朋友有所帮助:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int BOOL;
struct Arr
{
int *pBase;
int len;
int cnt;
};
void init(struct Arr *, int);
BOOL append(struct Arr *, int);
BOOL insert(struct Arr *, int, int);
BOOL deleted(struct Arr *, int, int *);
BOOL isEmpty(struct Arr *);
BOOL isFull(struct Arr *);
void sort(struct Arr *);
void show(struct Arr *);
void inversion(struct Arr *);
int main(void)
{
struct Arr arr;
init(&arr, 6);
show(&arr);
append(&arr, 1);
append(&arr, 2);
append(&arr, 3);
show(&arr);
insert(&arr, 4, 4);
show(&arr);
insert(&arr, 1, 99);
show(&arr);
int val = -1;
deleted(&arr, 1, &val);
show(&arr);
inversion(&arr);
show(&arr);
sort(&arr);
show(&arr);
printf("%d\n", val);
return 0;
}
void init(struct Arr *pArr, int len)
{
pArr->pBase = (int *)malloc(sizeof(int) * len);
if (NULL == pArr->pBase) {
printf("malloc is error\n");
exit(-1);
}
pArr->len = len;
pArr->cnt = 0;
return;
}
void show(struct Arr *pArr)
{
if (isEmpty(pArr)) {
printf("array is empty\n");
return;
}
for (int i = 0; i < pArr->cnt; i++)
{
printf("%d ", pArr->pBase[i]);
}
printf("\n");
}
BOOL isEmpty(struct Arr *pArr)
{
if (0 == pArr->cnt) {
return TRUE;
}
else {
return FALSE;
}
}
BOOL append(struct Arr *pArr, int vale)
{
if (isFull(pArr))
return FALSE;
pArr->pBase[pArr->cnt] = vale;
pArr->cnt = pArr->cnt + 1;
return TRUE;
}
BOOL isFull(struct Arr *pArr)
{
if (pArr->cnt == pArr->len)
return TRUE;
else
return FALSE;
}
BOOL insert(struct Arr *pArr, int pos, int vale)
{
int i = 1;
if (isFull(pArr))
{
return FALSE;
}
if (pos < 1 || pos > pArr->cnt + 1)
{
return FALSE;
}
for (i = pArr->cnt - 1; i>= pos-1; --i) {
pArr->pBase[i+1] = pArr->pBase[i];
}
pArr->pBase[pos - 1] =vale;
pArr->cnt = pArr->cnt + 1;
return TRUE;
}
BOOL deleted(struct Arr *pArr, int pos , int *pVale)
{
int i;
if (isEmpty(pArr)) {
return FALSE;
}
if (pos < 1 || pos > pArr->cnt)
{
return FALSE;
}
*pVale = pArr->pBase[pos - 1];
for (i = pos ; i < pArr->cnt; ++i)
{
pArr->pBase[i - 1] = pArr->pBase[i];
}
pArr->cnt = pArr->cnt - 1;
return TRUE;
}
void inversion(struct Arr *pArr)
{
int i = 0;
int j = pArr->cnt - 1;
int t;
while (i < j)
{
t = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = t;
++i;
--j;
}
return;
}
void sort(struct Arr *pArr)
{
int i, j, t;
for (i = 0; i < pArr->cnt; i++)
{
for (j = i+1; j < pArr->cnt; j++)
{
if (pArr->pBase[i] > pArr->pBase[j]) {
t = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = t;
}
}
}
}