标签:
SeqLsit.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList; //是用来封装,不被外界改变
typedef void SeqListNode;
SeqList* Creat_SeqList(int capacity);
SeqList* Get_Node_SeqList(SeqList* list, int pos);
SeqList* Delet_Node_SeqList(SeqList* list, int pos);
int Get_Cpacity_SeqList(SeqList* list);
int Get_Length_SeqList(SeqList* list);
int Clear_SeqList(SeqList* list);
int Add_SeqList(SeqList* list, SeqListNode* node, int pos);
void Destory_SeqList(SeqList* list);
#endif
SeqList.c
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _tar_SeqList
{
int capacity;
int length;
TSeqListNode* node;
}TSeqList;
SeqList* Creat_SeqList(int capacity)
{
TSeqList* ret = NULL;
if (capacity >= 0)
{
ret = (TSeqList*)malloc((sizeof(TSeqList) + sizeof(TSeqListNode)*capacity));
if (NULL != ret)
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}
}
else
{
ret = NULL;
}
return (SeqList*)ret;
}
int Add_SeqList(SeqList* list, SeqListNode* node, int pos)
{
int i = 0;
TSeqList* Tlist = (TSeqList*)list;
int ret = (NULL !=Tlist);
ret = ret && (pos >= 0);
ret = ret && (Tlist->length + 1 <= Tlist->capacity);
if (1 == ret)
{
if (pos > Tlist->length)
{
pos = Tlist->length;
}
for (i = Tlist->length; i > pos; i--)
{
Tlist->node[i] = Tlist->node[i - 1];
printf("Tlist->node[i] = %p\n", Tlist->node[i]);
}
Tlist->node[i] = (TSeqListNode)node;
// printf("Tlist->node[i] = %p\n", Tlist->node[i]);//申请的内存区域是连续的,但是和结构体的内存脱节
Tlist->length++;
// printf("%d\n", Tlist->length);
}
else
{
ret = 0;
}
return ret;
}
int Get_Cpacity_SeqList(SeqList* list)
{
TSeqList* Tlist = (TSeqList*)list;
int ret = 0;
if (Tlist != NULL)
{
ret = Tlist->capacity;
}
else
{
ret = -1;
}
return ret;
}
int Get_Length_SeqList(SeqList* list)
{
TSeqList* Tlist = (TSeqList*)list;
int ret = 0;
if (Tlist != NULL)
{
ret = Tlist->length;
}
else
{
ret = -1;
}
return ret;
}
int Clear_SeqList(SeqList* list)
{
TSeqList* Tlist = (TSeqList*)list;
int ret = 0;
if (Tlist != NULL)
{
ret = Tlist->length = 0;
}
else
{
ret = -1;
}
return ret;
}
void Destory_SeqList(SeqList* list)
{
free(list);
}
SeqList* Get_Node_SeqList(SeqList* list, int pos)
{
TSeqList* Tlist = (TSeqList*)list;
SeqListNode* ret = NULL;
if ((NULL != Tlist) && (pos >= 0) && (pos <= Tlist->length))
{
ret = (SeqListNode*)Tlist->node[pos];
}
return ret;
}
SeqList* Delet_Node_SeqList(SeqList* list, int pos)
{
TSeqList* Tlist = (TSeqList*)list;
SeqListNode* ret = NULL;
int i = 0;
if ((NULL != Tlist) && (pos >= 0) && (pos < Tlist->length))
{
ret = (SeqListNode*)Tlist->node[pos];
for (i = pos + 1; i < Tlist->length; i++)
{
Tlist->node[i - 1] = Tlist->node[i];
}
Tlist->length--;
}
return ret;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main(void)
{
int capacity = 5;
int* num = NULL;
int a[] = {1,2,3,4,5};
SeqList* My_SeqList = NULL;
// printf("a[] = %p\n", &a);
My_SeqList = Creat_SeqList(5); //创建线性表
if (NULL != My_SeqList)
{
Add_SeqList(My_SeqList, &a[0], 0);
Add_SeqList(My_SeqList, &a[1], 1);
Add_SeqList(My_SeqList, &a[2], 2);
Add_SeqList(My_SeqList, &a[3], 3);
Add_SeqList(My_SeqList, &a[4], 4);
}
//查看线性表容量
printf("创建的线性表容量 = %d\n", Get_Cpacity_SeqList(My_SeqList));
//查看线性表容量
printf("创建的线性表长度 = %d\n", Get_Length_SeqList(My_SeqList));
//查看指定位置的值
num = Get_Node_SeqList(My_SeqList, 3);
printf("指定位置的值为 = %d\n", *num);
num = Delet_Node_SeqList(My_SeqList, 1);
printf("删除的指定位置的值为 = %d\n", *num);
system("pause");
return 0;
}
标签:
原文地址:http://blog.csdn.net/qq_24085431/article/details/51344623