当前位置 : 主页 > 编程语言 > java >

数据结构:线性表的顺序存储结构

来源:互联网 收集:自由互联 发布时间:2022-07-19
听了国嵌唐老师的课,用C语言实现: SeqList.h #ifndef _SEQLIST_H_ #define _SEQLIST_H_ typedef void SeqList; typedef void SeqListNode; SeqList* SeqList_Create(int capacity);//创建并返回一个空的线性表 void SeqList_De


听了国嵌唐老师的课,用C语言实现:

数据结构:线性表的顺序存储结构_复用


SeqList.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);//创建并返回一个空的线性表

void SeqList_Destory(SeqList* list);//销毁线性表SeqList

void SeqList_Clear(SeqList* list);//清空线性表

int SeqList_Length(SeqList* list);//返回线性表长度

int SeqList_Capacity(SeqList* list);//返回线性表的最大容量

int SeqList_Insert(SeqList* list,SeqListNode* node,int pos);//在pos插入node,返回1表示插入成功

SeqListNode* SeqList_Get(SeqList* list,int pos);//获取pos位置元素,返回pos位置元素

SeqListNode* SeqList_Delete(SeqList* list,int pos);//删除pos位置元素,返回值为被删元素
#endif

SeqList.c

#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"

typedef unsigned int TSeqListNode; //可复用

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_Destory(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)
{
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)(unsigned int*)node;

sList->length++;
}

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;
}

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);

for(index = 0;index<SeqList_Length(list);index++)
{
int *p = (int*)SeqList_Get(list,index);

printf("%d\n",*p);
}

while(SeqList_Length(list)>0)
{
int *p = (int*)SeqList_Delete(list,0);

printf("\n%d\n",*p);
}

SeqList_Destory(list);

return 0;
}

运行效果:

数据结构:线性表的顺序存储结构_C语言_02


网友评论