(一)线性表的顺序存储实现

线性表的顺序存储设计与实现

1.概念

    用一段地址连续的存储单元依次存储线性表的数据元素

2.设计与实现

(1)插入元素

    a 判断线性表是否合法

    b 判断插入位置是否合法

    c 把最后一个元素到插入位置的元素后移一个位置

    d 将新元素插入

    e 线性表长度加1

(2)获取元素位置

        

  a 判断线性表是否合法

  b 判断插入位置是否合法

  c 直接通过数组下标的方式获取元素

(3)删除元素

   a 判断线性表是否合法

   b 判断插入位置是否合法

   c 将元素取出

   d 将删除位置元素后的元素分别向前移动一个位置

   e 线性表长度减1

代码如下:

(1)seqlist.h
#ifndef __MY_SEQLIST_H__
#define __MY_SEQLIST_H__

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

void SeqList_Destory(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);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);

#endif  __MY_SEQLIST_H__
(2) seqlist.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "seqlist.h"

typedef struct _tag_SeqList
{
	int length;
	int capacity;
	unsigned int **node; //int* node[] //指针数组
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
	int ret = 0;
	TSeqList *tmp = NULL;

	tmp = (TSeqList*)malloc(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);
	if (tmp->node == NULL)
	{
		ret = -2;
		printf("func malloc() err:%d\n", ret);
		return NULL;
	}
	tmp->capacity = capacity;
	tmp->length = 0;

	return tmp;
}

void SeqList_Destory(SeqList* list)
{
	TSeqList* tlist = NULL;
	if (list == NULL)
	{
		return ;
	}

	tlist = (TSeqList*)list;
	if (tlist->node != NULL)
	{
		free(tlist->node);
	}
	free(tlist);

	return ;
}

//清空链表 回到初始化状态
void SeqList_Clear(SeqList* list)
{
	TSeqList* tlist = NULL;
	if (list == NULL)
	{
		return;
	}

	tlist = (TSeqList*)list;
	tlist->length = 0;

	return ;
}

int SeqList_Length(SeqList* list)
{
	TSeqList* tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}

	tlist = (TSeqList*)list;

	return tlist->length;
}

int SeqList_Capacity(SeqList* list)
{
	TSeqList* tlist = NULL;
	if (list == NULL)
	{
		return -1;
	}

	tlist = (TSeqList*)list;

	return tlist->capacity;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int ret = 0;
	int i = 0;
	TSeqList* tlist = NULL;
	if (list == NULL || node == 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() (tlist->length >= tlist->capacity) err:%d\n", ret);
		return ret;
	}

	//容错修正
	if (pos > tlist->length)
	{
		pos = tlist->length;
	}

	//1 元素后移
	for (i = tlist->length; i > pos; i--)
	{
		tlist->node[i] = tlist->node[i - 1];
	}
	//2 插入值
	tlist->node[i] = (unsigned int)node;
	tlist->length++;

	return 0;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	int ret = 0;
	SeqListNode* tmp = 0;
	TSeqList* tlist = NULL;
	//1 判断线性表及删除位置是否合法
	if (list == NULL || pos < 0)
	{
		ret = -1;
		printf("func SeqList_Get() err:%d\n", ret);
		return NULL;
	}
	tlist = (TSeqList*)list;
	//2 直接通过数组下标的方式获取元素
	tmp = (void*)tlist->node[pos];
	return tmp;
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int i = 0, ret = 0;
	SeqListNode* tmp = 0;
	TSeqList* tlist = NULL;
	//1 判断线性表及删除位置是否合法
	if (list == NULL || pos < 0)
	{
		ret = -1;
		printf("func SeqList_Get() err:%d\n", ret);
		return NULL;
	}
	tlist = (TSeqList*)list;
	//2 将元素取出
	tmp = (SeqListNode *)tlist->node[pos];

	//3 将删除位置之后的元素分别向前移动一个位置
	for (i = pos + 1; i < tlist->length; i++)
	{
		tlist->node[i - 1] = tlist->node[i];
	}
	//4 线性表长度减1
	tlist->length--;
	return tmp;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线性顺序存储可以通过静态分配和动态分配两种方式实现。静态分配是指使用数组来存储线性的元素,而动态分配是指使用指针和malloc函数来分配存储空间。下面是一段简单的代码实现线性顺序存储: ``` typedef struct { ElemType data[MaxSize]; // 存放顺序元素 int length; // 存放顺序的长度 } SqList; void InitList(SqList *&L) { L = (SqList *)malloc(sizeof(SqList)); // 分配存放线性的空间 L->length = 0; // 顺序长度置0 } void DestroyList(SqList *&L) { free(L); // 释放线性的空间 } bool ListEmpty(SqList *L) { return L->length == 0; } int ListLength(SqList *L) { return L->length; } void GetElem(SqList *L, int i, ElemType &e) { if (i < 1 || i > L->length) { return; } e = L->data[i - 1]; } int LocateElem(SqList *L, ElemType e) { for (int i = 0; i < L->length; i++) { if (L->data[i] == e) { return i + 1; } } return 0; } bool ListInsert(SqList *&L, int i, ElemType e) { if (i < 1 || i > L->length + 1) { return false; } if (L->length == MaxSize) { return false; } for (int j = L->length - 1; j >= i - 1; j--) { L->data[j + 1] = L->data[j]; } L->data[i - 1] = e; L->length++; return true; } bool ListDelete(SqList *&L, int i, ElemType &e) { if (i < 1 || i > L->length) { return false; } e = L->data[i - 1]; for (int j = i; j < L->length; j++) { L->data[j - 1] = L->data[j]; } L->length--; return true; } ``` 这段代码实现线性顺序存储的基本操作,包括初始化线性、销毁线性、判断线性是否为空、获取线性长度、获取指定位置的元素、查找指定元素的位置、在指定位置插入元素和删除指定位置的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值