正在看国嵌唐老师数据结构,留着记录
//main.c文件
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
SeqList* list = SeqList_Create(5);
int i = 0;
int j = 1;
int k = 2;
int x = 3;
int y = 4;
int z = 5;
int index = 0;
SeqList_Insert(list, &i, 0);
SeqList_Insert(list, &j, 0);
SeqList_Insert(list, &k, 0);
SeqList_Insert(list, &x, 0);
SeqList_Insert(list, &y, 0);
SeqList_Insert(list, &z, 0);
printf("length = %d\n", SeqList_Length(list));
printf("\n--------------------------------\n");
//遍历顺序表
for (index=0; index<SeqList_Length(list); index++) {
int* p = (int*)SeqList_Get(list, index); //取元素
printf("%d\n", *p);
}
printf("\n--------------------------------\n");
//反转顺序表
SeqList_Reverse(list);
//遍历顺序表
for (index=0; index<SeqList_Length(list); index++) {
int* p = (int*)SeqList_Get(list, index); //取元素
printf("%d\n", *p);
}
printf("\n--------------------------------\n");
//删除元素
while (SeqList_Length(list) > 0) {
int* p = (int*)SeqList_Delete(list, 0);
printf("%d\n", *p);
}
//销毁顺序表
SeqList_Destroy(list);
return 0;
}
//SeqList.c文件
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode; //数据元素类型 存放的是 各种数据的地址 所以才用unsigned int
typedef struct _tag_SeqList {
int capacity; //容量
int length; //长度
TSeqListNode* node;
} TSeqList;
/*创建顺序表*/
SeqList* SeqList_Create(int capacity)
{
TSeqList* ret = NULL;
if (capacity >= 0 ) {
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}
if (ret != NULL) {
ret->capacity = capacity; //容量
ret->length = 0; //长度
ret->node = (TSeqListNode*)(ret + 1); //结点 指针运算
}
return ret;
}
/*销毁顺序表*/
void SeqList_Destroy(SeqList* list)
{
free(list);
}
/*清空顺序表*/
void SeqList_Clear(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
if (sList != NULL) {
sList->length = 0;
}
}
/*返回长度*/
int SeqList_Length(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if (sList != NULL) {
ret = sList->length;
}
return ret;
}
/*返回容量*/
int SeqList_Capacity(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if (sList != NULL) {
ret = sList->capacity;
}
return ret;
}
/*插入元素*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL); //是否有效
int i = 0;
//是否有位置可插入
ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos);
if (ret) {
//如果大于等于长度,调整pos值
if (pos >= sList->length) {
pos = sList->length;
}
//开始移动元素 向后退
for (i=sList->length; i>pos; i--) {
sList->node[i] = sList->node[i-1];
}
//存放元素
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
/*反转元素*/
int SeqList_Reverse(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL); //是否有效
int i = 0;
int j = sList->length - 1;
SeqListNode* tmp = NULL;
//开始反转
if (ret) {
for (i=0; i<j; i++, j--) {
tmp = (SeqListNode*)sList->node[i];
sList->node[i] = sList->node[j];
sList->node[j] = (unsigned int)tmp;
}
}
return ret;
}
/*获取元素*/
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
//获取元素
if ((sList != NULL) && (0 <= pos) && (pos <= sList->length)) {
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
/*删除元素*/
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list, pos); //保存删除的元素
int i = 0;
//开始向前移动元素
if (ret != NULL) {
for (i=pos+1; i<sList->length; i++) {
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
//SeqList.h文件
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
//声明void 类型 数据封装 避免误操作
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
int SeqList_Reverse(SeqList* list);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif