线性表顺序存储实现

/************头文件***********/
#ifndef _SEQLIST_H_
#define _SEQLIST_H_ 
 
typedef void SeqList;
typedef void SeqListNode;
 
//创建并且返回一个空的线性表
SeqList* SeqList_Create();
 
//销毁一个线性表
void SeqList_Destroy(SeqList* list);
 
//将一个线性表list中的所有元素清空,线性表回到创建时的初始状态
void SeqList_Clear(SeqList* list);
 
//返回一个线性表list中的所有元素个数
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
 
//向一个线性表list的pos位置处插入新元素node
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
 
//获取一个线性表list的pos位置处的元素
SeqListNode* SeqList_Get(SeqList* list, int pos);
 
//删除一个线性表list的pos位置处的元素 返回值为被删除的元素, NULL表示删除失败
SeqListNode* SeqList_Delete(SeqList* list, int pos);
 
#endif

/****************实现代码*************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"
//在结构体中套2级指针
typedef struct _tag_Seqlist
{
	int length;
	int capacity;
	unsigned int **node; //int *node[] 指针数组退化为二级指针
	//unsigned int* array[capacity];//数组元素不能用capacity,不确定值!
}TSeqlist;
//创建并且返回一个空的线性表
SeqList* SeqList_Create(int capacity)
{
	int ret = 0;
	TSeqlist* tmp = NULL;
	tmp = (TSeqlist*)malloc(sizeof(TSeqlist));//此处长度 sizeof(TSeqlist)
	if (tmp == NULL)
	{
		ret = -1;
		printf("func Seqlist_Create() err: %d\n", ret);
		return NULL;
	}
	memset(tmp, 0, sizeof(TSeqlist));
	//根据capacity的大小分配节点的空间
	tmp->node = (unsigned int **)malloc(sizeof(unsigned int*)*capacity);//此处长度 sizeof(unsigned int *),因为int *node[],存放的是指针;
	if (tmp->node == NULL)
	{
		ret = -2;
		printf("func Seqlist_Create() err: malloc err %d\n", ret);
		return NULL;
	}
	tmp->capacity = capacity;
	tmp->length = 0; 
	return tmp;
}
//销毁一个线性表
void SeqList_Destroy(SeqList* list)
{
	TSeqlist* tlist = NULL;
	if(list == NULL)
	{
		return ;
	}
	tlist = (TSeqlist*)list;
	if (tlist->node != NULL)
	{
		free(tlist->node);
		tlist->node = NULL;
	}
	free(tlist);
	return;
}
//将一个线性表list中的所有元素清空,线性表回到创建时的初始状态
void SeqList_Clear(SeqList* list)
{
	TSeqlist* tlist = NULL;
	if (list == NULL)
	{
		return;
	}
	tlist = (TSeqlist*)list;
	tlist->length = 0;
	return;
}
//返回一个线性表list中的所有元素个数
int SeqList_Length(SeqList* list)
{
	TSeqlist* tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}
	tlist = (TSeqlist*)list;
	return tlist->length;
}
// 返回一个线性表list的总大小
int SeqList_Capacity(SeqList* list)
{
	TSeqlist* tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}
	tlist = (TSeqlist*)list;
	return tlist->capacity;
}
//向一个线性表list的pos位置处插入新元素node
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int i = 0, ret = 0;
	TSeqlist* tlist = NULL;
	if (list == NULL || list == NULL || pos < 0)
	{
		ret = -1;
		printf("func SeqList_Insert() err; %d \n", ret);
		return ret;
	}
	tlist = (TSeqlist*)list;
	//判断是不是满了
	if (tlist->length >= tlist->capacity)
	{
		ret = -2;
		printf("func SeqList_Insert() err; %d \n", ret);
		return ret;
	}
	//容错修正 6个长度 容量20 用户pos10位置插入
	if (pos >= tlist->length)
	{
		pos = tlist->length;
	}
	
	//元素后移
	for (i = tlist->length; i > pos; i--)
	{
		tlist->node[i] = tlist->node[i - 1];
	}
	//插入元素
	tlist->node[i] =(unsigned int*) node;//此处插入元素,给node[]的i号节点赋值 tlist->node[i] = node;
	tlist->length++;
	return 0;
}
//获取一个线性表list的pos位置处的元素
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	int i = 0;
	TSeqlist *ret = NULL;
	TSeqlist *tlist = NULL;
	if (list == NULL || pos < 0)
	{
		printf("func SeqList_Get err: %d\n", ret);
		return ret;
	}
	tlist = (TSeqlist*)list;
	ret = (void*)tlist->node[pos];
	return ret;
}
//删除一个线性表list的pos位置处的元素 返回值为被删除的元素, NULL表示删除失败
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int i = 0;
	TSeqlist *ret = NULL;
	TSeqlist *tlist = NULL;
	if (list == NULL || pos < 0)//检查																																						
	{
		printf("func SeqList_Get err: %d\n", ret);
		return ret;
	}
	tlist = (TSeqlist*)list;
	ret = (SeqListNode*)tlist->node[pos];//缓存pos位置
	
	for (i = pos + 1; i < tlist->length; i++)//pos位置后面元素前移
	{
		tlist->node[i - 1] = tlist->node[i];
	}
	tlist->length--;
	return ret;
}

/****************测试代码*************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"
typedef struct teacher 
{
	char name[64];
	int age;
}Teacher;
int main()
{
	int ret = 0, i = 0;
	SeqList* list = NULL;
	Teacher t1, t2, t3, t4, t5;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	t4.age = 34;
	t5.age = 35;
	list = SeqList_Create(10);
	if (list == NULL)
	{
		printf("func Seqlist_Create() ret; %d\n", ret);
		return ret;
	}
	ret = SeqList_Insert(list, (SeqListNode*)&t1, 0); //头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t2, 0); //头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t3, 0); //头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t4, 0); //头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t5, 6); //头插法
	//遍历
	for (i = 0; i < SeqList_Length(list); i++)
	{
		Teacher* tmp = (Teacher*)SeqList_Get(list, i);
		if (tmp == NULL)
		{
			return ret;
		}
		printf("tmp->age:%d\n", tmp->age);
	}
	
	//删除链表中的节点
	while (SeqList_Length(list) >0)
	{
		SeqList_Delete(list, 0);
	}
	system("pause");
	return ret;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值