由于很简单,所以我把注释写的很详细,欢迎大家指正错误!!
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
//创建动态顺序表
typedef struct node {
int* arr;//指针指向头
int szie;//有效元素
int capacity;//总容量
}node;
//1.顺序表初始化
void NodeInit(node* ps, int initcapacity);
//2.顺序表释放
void NodeDestory(node* ps);
//3.顺序表打印
void NodePrint(node* ps);
//4.顺序表尾插
void NodePushback(node* ps, int x);
//5.顺序表头插
void NodePushfront(node* ps, int x);
//6.顺序表头删
void NodePopfront(node* ps);
//7.顺序表尾删
void NodePopback(node* ps);
//8.顺序表测试
void Testnode();
int NodeisEmpty(node* ps);
//9.判断顺序表是否为空
int NodeisEmpty(node* ps) {
assert(ps);
return 0 == ps->szie;
}
//10.顺序表寻找元素返回下标
int NodeFind(node* ps, int x);
//11.顺序表在pos位置插入x
void NodeInsert(node* ps, size_t pos, int x);
//12.顺序表删除pos位置的元素
void Nodedelet(node* ps, size_t pos);
//1.顺序表的初始化
void NodeInit(node* ps, int initcapacity) {
assert(ps);
ps->arr = (int*)malloc(sizeof(int) * initcapacity);
if (NULL == ps->arr) {
assert(0);
return;
}
ps->capacity = initcapacity;
ps->szie = 0;
}
//2.顺序表的释放
void NodeDestory(node* ps) {
assert(ps);
if (ps->arr) {
free(ps->arr);
ps->capacity = 0;
ps->szie = 0;
}
}
//3.顺序表的打印
void NodePrint(node* ps) {
assert(ps);
for (int i = 0; i < ps->szie; i++) {
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//4.顺序表的尾插
void NodePushback(node* ps, int x) {
assert(ps);
if (ps->capacity > ps->szie) {
ps->arr[ps->szie] = x;
ps->szie++;
}
else {
printf("添加失败!\n");
}
}
//5.顺序表的头插
void NodePushfront(node* ps, int x) {
assert(ps);
if (ps->capacity > ps->szie) {
int tmp = ps->arr[0];
for (int i = ps->szie - 1; i >= 0; i--) {
ps->arr[i+1] = ps->arr[i];
}
ps->arr[0] = x;
ps->szie++;
}
else {
printf("插入失败!\n");
}
}
//6.顺序表的头删
void NodePopfront(node* ps) {
assert(ps);
for (int i = 0; i < ps->szie - 1; i++) {
ps->arr[i] = ps->arr[i+1];
}
ps->szie--;
}
//7.顺序表的尾删
void NodePopback(node* ps) {
if (NodeisEmpty(ps)) {
return;
}
else {
ps->szie--;
}
}
//10.顺序表寻找元素返回下标
int NodeFind(node* ps, int x) {
assert(ps);
for (int i = 0; i < ps->szie - 1; i++) {
if (ps->arr[i] == x) {
return i;
}
}
return -1;
}
//11.顺序表在pos位置插入x
void NodeInsert(node* ps, size_t pos, int x) {
assert(ps);
if (ps->capacity > ps->szie) {
for (int i =ps->szie-1; i >pos-1; i--) {
ps->arr[i+1] = ps->arr[i];
}
ps->arr[pos] = x;
ps->szie++;
}
}
//12.顺序表删除pos位置的元素
void Nodedelet(node* ps, size_t pos) {
assert(ps);
for (int i = pos; i < ps->szie; i++) {
ps->arr[i] = ps->arr[i+1];
}
ps->szie--;
}
//测试用例1
void Testnode1() {
node s;//创建变量
NodeInit(&s, 10);//初始化
NodePushback(&s, 1);//尾插1
NodePushback(&s, 2);//尾插2
NodePushback(&s, 3);//尾插3
NodePushback(&s, 4);//尾插4
NodePushback(&s, 5);//尾插5
NodePushback(&s, 6);//尾插6 ---->顺序表里的数字为1 2 3 4 5 6
NodePrint(&s);
NodePopback(&s);//尾删1位元素
NodePrint(&s);
NodePushback(&s, 6);//再加回来
NodePrint(&s);
NodePushfront(&s, 0);//头插0; ---->顺序表里的数字为0 1 2 3 4 5 6
NodePrint(&s);
NodePopfront(&s, 0);//头删0; ---->顺序表里的数字为1 2 3 4 5 6
NodePrint(&s);
NodeInsert(&s, 3, 5);//在顺序表下标为3的元素前面插入5 ---->顺序表里的数字为1 2 3 5 4 5 6
NodePrint(&s);
Nodedelet(&s, 3);//删除下标为3的元素
NodePrint(&s);
NodePrint(&s);
printf("找到数字3的下标为%d\n", NodeFind(&s, 3));//找到3所对应的下标
NodeDestory(&s);
NodePrint(&s);
}
void Testnode() {
Testnode1();
}
int main() {
Testnode();
return 0;
}
?运行结果如下:
?非常简单,学会的扣1,没学会的扣眼珠子.
|