看了国嵌唐老师的数据结构视频,看了两遍这章写了两遍,一遍跟着老师写,有一遍完全自己写了一次,感觉还是很有收获的。
一、主函数:
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main(int argc, char *argv[])
{
SeqList* LIST = (SeqList*)Create(5);
int i = 1;
int j = 2;
int x = 3;
int y = 4;
int z = 5;
int m = 0;
Insert(LIST,&i,0);
Insert(LIST,&j,0);
Insert(LIST,&x,0);
Insert(LIST,&y,0);
Insert(LIST,&z,0);
for(m=Length(LIST)-1;m>=0;m--)
{
int *p = Get(LIST,m);
printf("%d\n",*p);
}
printf("------------\n");
int* q = (int*)Delete(LIST,0);
printf("%d\n",*q);
Destory(LIST);
getchar();
return 0;
}
二、使用宏定义:
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqListNode;
typedef void SeqList;
SeqList* Create(int n);//创建表
void Destory(SeqList* list);//销毁表
void Clean(SeqList* list);//清空表
int Length(SeqList* list); //表长
int capacity(SeqList* list);//表容量
int Insert(SeqList* list,SeqListNode* node,int n);
SeqList* Get(SeqList* list,int n);
SeqListNode* Delete(SeqList* list,int n);
#endif
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqListNode;
typedef void SeqList;
SeqList* Create(int n);//创建表
void Destory(SeqList* list);//销毁表
void Clean(SeqList* list);//清空表
int Length(SeqList* list); //表长
int capacity(SeqList* list);//表容量
int Insert(SeqList* list,SeqListNode* node,int n);//插入数据
SeqList* Get(SeqList* list,int n);//从表中获取数据
SeqListNode* Delete(SeqList* list,int n); //从表中删除数据
#endif
三、功能实现:
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _SeqList_
{
int capacity;
int length;
TSeqListNode* node;
}TSeqList;
SeqList* Create(int n)//创建表
{
TSeqList* ret = NULL;
if( n >0)
{
ret = (TSeqList*)malloc(sizeof(TSeqList*) + sizeof(ret->node)*n);//给创建的表申请空间
}
if(ret != NULL)
{
ret->capacity = n;
ret->length = 0;
ret->node = (TSeqListNode*)(ret+1);
}
return ret;
}
void Destory(SeqList* list)//销毁表
{
TSeqList* Tlist = (TSeqList*)list;
free(Tlist);
}
void Clean(SeqList* list)//清空表
{
TSeqList* Tlist = (TSeqList*)list;
if( Tlist != NULL)
{
Tlist->length = 0 ;
}
}
int Length(SeqList* list)//求表长
{
TSeqList* Tlist = (TSeqList*)list;
int ret = -1;
if( Tlist != NULL)
{
ret = Tlist->length;
}
return ret;
}
int Capacity(SeqList* list)//求表容量
{
TSeqList* Tlist = (TSeqList*)list;
int ret = -1;
if(Tlist != NULL)
{
ret = Tlist->capacity;
}
return ret;
}
int Insert(SeqList* list,SeqListNode* node,int n)//向表中插入数据
{
TSeqList* Tlist = (TSeqList*)list;
int ret = ( Tlist != NULL);
int i=0;
ret = ret && ((Tlist->length+1 )<= (Tlist->capacity));
ret = ret && ((0<=n)&&(n <= Tlist->length));
if( ret )
{
if(n >=(Tlist->length))
{
n= Tlist->length;
}
for(i=Tlist->length;i>n;i--)
{
Tlist->node[i] = Tlist->node[i-1];
}
Tlist->node[i]=(TSeqListNode)node;
Tlist->length++;
}
return ret;
}
SeqList* Get(SeqList* list,int n)//从表中获取数据
{
TSeqList* Tlist = (TSeqList*)list;
TSeqList* ret = NULL;
if( (Tlist != NULL)&&((0<=n)&&(n<=Tlist->length)))
{
ret = (SeqListNode*)Tlist->node[n];
}
return ret;
}
SeqListNode* Delete(SeqList* list,int n)//从表中删除指定的数据
{
TSeqList* Tlist = (TSeqList*)list;
SeqListNode* ret = NULL;
int i = 0;
if( (Tlist != NULL)&&((0<=n)&&(n<=Tlist->length)))
{
ret = (SeqListNode*)Tlist->node[n];
}
if( ret != NULL )
{
for(i=n;i<=Tlist->length;i++)
{
Tlist->node[i-1] = Tlist->node[i];
}
Tlist->length--;
}
return ret;
}