顺序表的实现

目录

一、线性表的概念

二、顺序表

2.1概念

2.2动态实现

1.定义:

2.初始化和销毁:

3.增容

4.尾增尾删:

5.首增首删:

6.查找

7.在pos位置插入和删除

8.打印

全部代码:


顺序表是线性表的一种,我们需要先大概了解一下线性表。

一、线性表的概念

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...


线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

二、顺序表

2.1概念

顺序表是用一段物理地址连续的存储单元,也就是在内存上是连续存储的,通常使用数组存储或开辟一块空间来对这块空间上的数据进行增删查改。

顺序表又分两种静态和动态,静态是不可对空间进行增加的,动态可以。通常都是使用动态居多。

2.2动态实现

1.定义:

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	int size;
	int capacity;
}SeqList;

SLDateype:后面增加内存时,还会在用到元素的类型,任何类型都是可以使用顺序表的,所以重新定义类型名会方便使用。

size:存放当前顺序表使用的元素个数。

capacity:存放开辟的内存大小。

2.初始化和销毁:

//初始化
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

初始化时不需要为a开辟空间,在增加元素时在开辟。

//销毁
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	if(ps->a!=NULL)
	{
		free(ps->a);
		ps->a = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}

3.增容

void SLCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SeqList* p = (SeqList*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (p == NULL)
		{
			perror("realloc");
			return;
		}
		ps->capacity = newcapacity;
		ps->a = p;
	}
}

在增加元素的每个函数内都放入一个增容函数来扩容。

先利用三木操作符来判断,表里没有内容先开辟4个字节来用,表里又内容开辟原有空间的2倍。realloc开辟空间后先放入一个SeqList指针内,判断这个指针是否为空开辟失败再进行赋值,这样可以防止开辟失败将原来的数据丢失。

4.尾增尾删:

//尾增
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
//尾删
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}

删除元素时要判断size是否为0,是就直接提醒。

5.首增首删:

//首增
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

首增要将所有数据向后移动一位,后末尾开始移动,如果从前开始移动就会覆盖掉接下来将要移动的数据。

//首删
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	assert(ps->size);
	int end = 0;
	while (end<ps->size-1)
	{
		ps->a[end] = ps->a[end + 1];
		end++;
	}
	ps->size--;
}

6.查找

int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}

7.在pos位置插入和删除

//插入
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
//删除
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int end = pos;
	while (end < ps->size-1)
	{
		ps->a[end] = ps->a[end+1];
		end++;
	}
	ps->size--;
}

这里在插删和首增首删的逻辑基本一样,对插入删除后的数据进行移动。

8.打印

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

全部代码:

SeqList.h:

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	int size;
	int capacity;
}SeqList;

// 对数据的管理:增删查改 

//初始化顺序表
void SeqListInit(SeqList* ps);
//顺序表销毁
void SeqListDestroy(SeqList* ps);

//打印顺序表
void SeqListPrint(SeqList* ps);
//尾增元素
void SeqListPushBack(SeqList* ps, SLDateType x);
//首增元素
void SeqListPushFront(SeqList* ps, SLDateType x);
//删除首元素
void SeqListPopFront(SeqList* ps);
//删除尾元素
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

SeqList.c:
 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	if(ps->a!=NULL)
	{
		free(ps->a);
		ps->a = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}

void SLCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SeqList* p = (SeqList*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (p == NULL)
		{
			perror("realloc");
			return;
		}
		ps->capacity = newcapacity;
		ps->a = p;
	}
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	assert(ps->size);
	int end = 0;
	while (end<ps->size-1)
	{
		ps->a[end] = ps->a[end + 1];
		end++;
	}
	ps->size--;
}

int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int end = pos;
	while (end < ps->size-1)
	{
		ps->a[end] = ps->a[end+1];
		end++;
	}
	ps->size--;
}

代码测试:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Seqlist.h"
void test1()
{
	SeqList p;
	SeqListInit(&p);
	int i = 5;
	while(i--)
	{
		SeqListPushFront(&p, 10);
		SeqListPushFront(&p, 20);
		SeqListPushFront(&p, 30);
		SeqListPushFront(&p, 40);
		SeqListPushBack(&p, 1);
		SeqListPushBack(&p, 2);
		SeqListPushBack(&p, 3);
		SeqListPushBack(&p, 4);
		SeqListPrint(&p);
	}
	while (i++ != 3)
	{
		SeqListPopFront(&p);
		SeqListPopFront(&p);
		SeqListPopFront(&p);
		SeqListPopFront(&p);
		SeqListPopBack(&p);
		SeqListPopBack(&p);
		SeqListPopBack(&p);
		SeqListPopBack(&p);
		SeqListPrint(&p);
	}
	printf("%d \n",SeqListFind(&p, 5));
	while (i--)
	{
		SeqListInsert(&p, 4, 88);
		SeqListPrint(&p);
	}
	while (i++ != 3)
	{
		SeqListErase(&p, 4);
		SeqListPrint(&p);
	}
    SeqListDestroy(&p);
}
int main()
{
	test1();
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值