《数据结构初阶》之顺序表

主要功能实现部分

#include"SeqList.h"

//初始化顺序表
void SeqListInit(SL* ps)
{
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}


//打印顺序表
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d  ", ps->a[i]);
	}
	printf("\n");
}


//销毁顺序表
void SeqListDestory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}


//检查顺序表容量,若为0则开辟一块为4个单位大小的空间,若满则空间大小*2
void SeqListCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;

		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		
		if (tmp == NULL)
		{
			printf("relloc fail\n");
			exit(-1);
		}

		ps->capacity = newcapacity;

		ps->a = tmp;
	}
}


//头插
void SeqListPushFront(SL* ps, SLDataType x)
{
	SeqListCheckCapacity(ps);

	//int end = ps->size - 1;
	//while (end>=0)
	//{
	//	ps->a[end+1] = ps->a[end ];
	//	end--;
	//}
	//ps->a[0] = x;
	//ps->size++;

	使用指定位置插入实现头插
	SeqListInsert(ps,0,x);

}


//尾插
void SeqListPushBack(SL* ps, SLDataType x)
{
	SeqListCheckCapacity(ps);

	ps->a[ps->size] = x;
	ps->size++;

}


//头删
void SeqListPopFront(SL* ps)
{
	assert(ps->size > 0);

	/*int i = 1;
	while (i < ps->size)
	{
		ps->a[i - 1] = ps->a[i];
		i++;
	}
    ps->size--;*/

	//使用指定位置删除实现头删
	SeqListErase(ps, 0);
}


//尾删
void SeqListPopBack(SL* ps)
{
	assert(ps->size > 0);

	ps->size--;
}


//在顺序表中寻找这个数并返回下标,若没找到则返回-1
int SeqListFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}

	return -1;
}


//在顺序表指定下标处插入数据
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
	assert(pos >= 0 && pos <= ps->size);

	SeqListCheckCapacity(ps);
	int end = ps->size;
	while (end > pos)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}


//在顺序表指定位置删除数据
void SeqListErase(SL* ps, int pos)
{
	assert(pos >= 0 && pos < ps->size);

	int i = pos;
	while (i < ps->size-1)
	{
		ps->a[i] = ps->a[i + 1];
		i++;
	}
	ps->size--;

}

 测试部分

#include"SeqList.h"

void TestSeqList()
{
	SL sl;
	SeqListInit(&sl);
	SeqListPushFront(&sl, 5);
	SeqListPushFront(&sl, 4);
	SeqListPushFront(&sl, 3);
	SeqListPushFront(&sl, 2);
	SeqListPushFront(&sl, 1);
	SeqListPushBack(&sl, 6);
	SeqListPushBack(&sl, 7);
	SeqListPushBack(&sl, 8);
	SeqListPushBack(&sl, 9);
	SeqListPushBack(&sl, 10);

	SeqListPrint(&sl);

	SeqListPopFront(&sl);
	SeqListPrint(&sl);
	SeqListPopBack(&sl);
	SeqListPrint(&sl);

	SeqListInsert(&sl, 3,10);
	SeqListInsert(&sl, 3, 10);
	SeqListInsert(&sl, 3, 10);

	SeqListPrint(&sl);

	SeqListErase(&sl, 3);
	SeqListErase(&sl, 3);
	SeqListErase(&sl, 3);

	SeqListPrint(&sl);

	SeqListDestory(&sl);
}

int main()
{
	TestSeqList();
}

 头文件部分

#pragma once

#include
#include
#include


typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* a;
	int size;
	int capacity;
}SL;

void SeqListInit(SL* ps);
void SeqListPrint(SL* ps);
void SeqListDestory(SL* ps);


void SeqListCheckCapacity(SL* ps);
void SeqListPopFront(SL* ps);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPushBack(SL* ps, SLDataType x);
int SeqListFind(SL* ps, SLDataType x);
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);

 本文到此结束,欢迎大家点赞评论互关,感谢大家的阅读,祝大家万事如意。

你可能感兴趣的:(《数据结构初阶》,数据结构,链表,c语言)