C语言实现顺序表代码

2 阅读2分钟

多文件书写顺序表,建议:写一部分测一部分

SeqList.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>

#define MAXSIZE 32

typedef int SeqListDataType;
//typedef struct SeqList
//{
//	SeqListDataType arr[MAXSIZE];
//	int capacity;
//} SeqList;

typedef struct SeqList
{
	SeqListDataType *arr;
	int capacity;
	int size;
} SeqList;

#define DEFAULTSIZE 4

void init(SeqList *sl);
void destroy(SeqList *sl);
void push_back(SeqList *sl, SeqListDataType x);
void push_front(SeqList *sl, SeqListDataType x);
void pop_back(SeqList *sl);
void pop_front(SeqList *sl);
void Print(SeqList *sl);
void insert(SeqList *sl, int index, SeqListDataType x);
void erase(SeqList *sl, int index);

SeqList.c

#include "SeqList.h"

//typedef struct SeqList
//{
//	SeqListDataType *arr;
//	int capacity;
//	int size;
//} SeqList;

void init(SeqList *sl)
{
	assert(sl);
	sl->arr = (SeqListDataType *)malloc(sizeof(SeqListDataType) * DEFAULTSIZE);
	if (sl->arr == NULL)
	{
		perror("malloc:");
		return ;
	}
	sl->capacity = 0;
	sl->size = 4;
}
void destroy(SeqList *sl)
{
	assert(sl);
	if (sl->arr != NULL)
		free(sl->arr);
	sl->capacity = sl->size = 0;
}

static void checkSize(SeqList *sl)
{
	if (sl->capacity == sl->size)
	{
		int newCapacity = sl->capacity * 2;
		SeqListDataType *tmp = (SeqListDataType *)realloc(sl->arr, sizeof(SeqListDataType) * newCapacity);
		if (tmp == NULL)
		{
			perror("realloc");
		}
		printf("扩容成功\n");
		sl->arr = tmp;
		sl->size = newCapacity;
	}
}


void push_back(SeqList *sl, SeqListDataType x)
{
	assert(sl);
	// 判满
	checkSize(sl);
	// 插入元素
	sl->arr[sl->capacity ++] = x;
}
void push_front(SeqList *sl, SeqListDataType x)
{
	assert(sl);
	// 判满
	checkSize(sl);
	// 插入元素
	int index = sl->capacity - 1;
	while (index >= 0)
	{
		sl->arr[index + 1] = sl->arr[index];
		-- index;
	}
	sl->arr[0] = x;
	++ sl->capacity;
}
void pop_back(SeqList *sl)
{
	assert(sl);
	-- sl->capacity;
}
void pop_front(SeqList *sl)
{
	assert(sl);
	if (0 == sl->capacity)
		return ;
	int index = 1;
	while (index < sl->capacity)
	{
		sl->arr[index - 1] = sl->arr[index];
		++ index;
	}
	-- sl->capacity;
}
void Print(SeqList *sl)
{
	assert(sl);
	int i = 0;
	for (; i < sl->capacity; ++ i)
	{
		printf("%d ", sl->arr[i]);
	}
	printf("\n");
}

void insert(SeqList *sl, int index, SeqListDataType x)
{
	assert(sl);
	checkSize(sl);
	if (index < 0 || index > sl->capacity)
		return ;
	int i = sl->capacity - 1;
	while (i >= index)
	{
		sl->arr[i + 1] = sl->arr[i];
		-- i;
	}
	sl->arr[index] = x;
	++ sl->capacity;
}
void erase(SeqList *sl, int index)
{
	assert(sl);
	if (index < 0 || index > sl->capacity)
		return ;
	int i = index + 1;
	while (i < sl->capacity)
	{
		sl->arr[i - 1] = sl->arr[i];
		++ i;
	}
	-- sl->capacity;
}

main.c

#include "SeqList.h"

void testSeqlist1()
{
	SeqList sl1;
	init(&sl1);
	push_back(&sl1, 1);
	Print(&sl1);
	push_back(&sl1, 2);
	Print(&sl1);
	push_back(&sl1, 3);
	Print(&sl1);
	push_back(&sl1, 4);
	Print(&sl1);
	push_back(&sl1, 5);
	Print(&sl1);
	push_front(&sl1, 1);
	Print(&sl1);
	push_front(&sl1, 2);
	Print(&sl1);
	push_front(&sl1, 3);
	Print(&sl1);
	push_front(&sl1, 4);
	Print(&sl1);

	pop_back(&sl1);
	pop_back(&sl1);
	pop_back(&sl1);
	Print(&sl1);
	pop_front(&sl1);
	pop_front(&sl1);
	pop_front(&sl1);
	Print(&sl1);

	insert(&sl1, 1, 9);
	Print(&sl1);
	insert(&sl1, 1, 10);
	Print(&sl1);
	erase(&sl1, 1);
	Print(&sl1);
	erase(&sl1, 1);
	Print(&sl1);
	destroy(&sl1);
}


int main()
{
	testSeqlist1();
	return 0;
}

扩展

还可以实现在顺序表中查找一个元素是否存在等等的接口