顺序表
顺序表的构造
#include <stdio.h>
#include <stdlib.h>
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length=0;
vector->data = (int *)malloc(sizeof(int) *size);
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
clear(a);
return 0;
}
顺序表的插入
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length = 0;
vector->data = (int *)malloc(sizeof(int) * size);
}
int insert(Vector *vector, int loc, int value) {
if(loc < 0 || loc > vector->length) {
return ERROR;
}
if(vector->length >= vector->size) {
return ERROR;
}
for(int i = vector->length; i > loc; --i) {
vector->data[i] = vector->data[i-1];
}
vector->data[loc] = value;
vector->length++;
return OK;
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
printf("%d\n", insert(a, 1, 0));
printf("%d\n", insert(a, 0, 1));
printf("%d\n", insert(a, 2, 1));
printf("%d\n", insert(a, 1, 2));
printf("%d\n", insert(a, 0, 3));
clear(a);
return 0;
}
顺序表的扩容
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length = 0;
vector->data = (int *)malloc(sizeof(int) * size);
}
void expand(Vector *vector) {
int *old_data = vector->data;
vector->size = vector->size * 2;
vector->data = (int *)malloc(sizeof(int) * vector->size);
for(int i = 0; i < vector->length; ++i) {
vector->data[i] = old_data[i];
}
free(old_data);
}
int insert(Vector *vector, int loc, int value) {
if (loc < 0 || loc > vector->length) {
return ERROR;
}
if (vector->length >= vector->size) {
expand(vector);
}
for (int i = vector->length; i > loc; --i) {
vector->data[i] = vector->data[i - 1];
}
vector->data[loc] = value;
vector->length++;
return OK;
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
printf("%d\n", insert(a, 1, 0));
printf("%d\n", insert(a, 0, 1));
printf("%d\n", insert(a, 2, 1));
printf("%d\n", insert(a, 1, 2));
printf("%d\n", insert(a, 0, 3));
clear(a);
return 0;
}
顺序表的查找
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length = 0;
vector->data = (int *)malloc(sizeof(int) * size);
}
void expand(Vector *vector) {
int *old_data = vector->data;
vector->size = vector->size * 2;
vector->data = (int *)malloc(sizeof(int) * vector->size);
for (int i = 0; i < vector->length; ++i) {
vector->data[i] = old_data[i];
}
free(old_data);
}
int insert(Vector *vector, int loc, int value) {
if (loc < 0 || loc > vector->length) {
return ERROR;
}
if (vector->length >= vector->size) {
expand(vector);
}
for (int i = vector->length; i > loc; --i) {
vector->data[i] = vector->data[i - 1];
}
vector->data[loc] = value;
vector->length++;
return OK;
}
int search(Vector *vector, int value) {
for(int i = 0; i < vector->length; ++i) {
if(vector->data[i] == value) {
return i;
}
}
return -1;
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
printf("%d\n", insert(a, 1, 0));
printf("%d\n", insert(a, 0, 1));
printf("%d\n", insert(a, 2, 1));
printf("%d\n", insert(a, 1, 2));
printf("%d\n", insert(a, 0, 3));
printf("%d\n", search(a, 1));
printf("%d\n", search(a, 4));
clear(a);
return 0;
}
顺序表的删除
顺序表的删除操作 顺序表的删除操作是指, 将顺序表中第i 个位置(0 < i < len ) 的元素从顺序表中删除,并将该元素之后的所有元素顺次向前移动一个位置,len 表示顺序表元素个数。 delete_nod 函数已经定义好了, 除了 Vector 类型的指针参数 vector , 还有一个参数 index , 表示已经删除测下标 , 返回值为int 类型, 表示是否删除成功。
首先对传入参数的合法性进行判断, 如果传入的下标比0 小, 或者大于等于len , 那么直接返回 ERROR .
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length = 0;
vector->data = (int *)malloc(sizeof(int) * size);
}
void expand(Vector *vector) {
int *old_data = vector->data;
vector->size = vector->size * 2;
vector->data = (int *)malloc(sizeof(int) * vector->size);
for (int i = 0; i < vector->length; ++i) {
vector->data[i] = old_data[i];
}
free(old_data);
}
int insert(Vector *vector, int loc, int value) {
if (loc < 0 || loc > vector->length) {
return ERROR;
}
if (vector->length >= vector->size) {
expand(vector);
}
for (int i = vector->length; i > loc; --i) {
vector->data[i] = vector->data[i - 1];
}
vector->data[loc] = value;
vector->length++;
return OK;
}
int search(Vector *vector, int value) {
for (int i = 0; i < vector->length; ++i) {
if (vector->data[i] == value) {
return i;
}
}
return -1;
}
int delete_node(Vector *vector, int index) {
if (index < 0 || index >= vector->length) {
return ERROR;
}
for(int i = index + 1; i < vector->length; ++i) {
vector->data[i-1] = vector->data[i];
}
vector->length--;
return OK;
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
printf("%d\n", insert(a, 0, 1));
printf("%d\n", insert(a, 0, 2));
printf("%d\n", delete_node(a, 1));
printf("%d\n", search(a, 0));
printf("%d\n", search(a, 1));
clear(a);
return 0;
}
顺序表的遍历
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Vector {
int size, length;
int *data;
} Vector;
void init(Vector *vector, int size) {
vector->size = size;
vector->length = 0;
vector->data = (int *)malloc(sizeof(int) * size);
}
void expand(Vector *vector) {
int *old_data = vector->data;
vector->size = vector->size * 2;
vector->data = (int *)malloc(sizeof(int) * vector->size);
for (int i = 0; i < vector->length; ++i) {
vector->data[i] = old_data[i];
}
free(old_data);
}
int insert(Vector *vector, int loc, int value) {
if (loc < 0 || loc > vector->length) {
return ERROR;
}
if (vector->length >= vector->size) {
expand(vector);
}
for (int i = vector->length; i > loc; --i) {
vector->data[i] = vector->data[i - 1];
}
vector->data[loc] = value;
vector->length++;
return OK;
}
int search(Vector *vector, int value) {
for (int i = 0; i < vector->length; ++i) {
if (vector->data[i] == value) {
return i;
}
}
return -1;
}
int delete_node(Vector *vector, int index) {
if (index < 0 || index >= vector->length) {
return ERROR;
}
for (int i = index + 1; i < vector->length; ++i) {
vector->data[i - 1] = vector->data[i];
}
vector->length = vector->length - 1;
return OK;
}
void print(Vector *vector) {
for (int i = 0; i < vector->length; ++i) {
if(i > 0) {
printf(" ");
}
printf("%d",vector->data[i]);
}
printf("\n");
}
void clear(Vector *vector) {
free(vector->data);
free(vector);
}
int main() {
Vector *a = (Vector *)malloc(sizeof(Vector));
init(a, 100);
printf("%d\n", insert(a, 0, 1));
printf("%d\n", insert(a, 0, 2));
print(a);
printf("%d\n", delete_node(a, 1));
print(a);
printf("%d\n", search(a, 0));
printf("%d\n", search(a, 1));
clear(a);
return 0;
}
|