【数据结构】顺序表的实现


seq_list.h
#pragma once

#include<stdio.h>
#include<stddef.h>

#define LIST_SIZE_INIT 3
#define default_value 1000

typedef char SeqType;

typedef struct Seqlist{
	SeqType* data;
	size_t size;
	size_t capacity;
}Seqlist;

void Seqlist_init(Seqlist *seq);
void Seqlist_pushBack(Seqlist* seq, SeqType value);
void printf_seqlist(Seqlist* seq);
void Seqlist_popBack(Seqlist* seq);
void Seqlist_pushFront(Seqlist* seq, SeqType value);
void Seqlist_popFront(Seqlist* seq);
SeqType Seqlist_get(Seqlist* seq, size_t pos);
void Seqlist_Set(Seqlist* seq, size_t pos, SeqType value);
size_t Seqlist_Find(Seqlist* seq, SeqType value);
void Seqlist_Insert(Seqlist* seq, size_t pos, SeqType value);
void Seqlist_Erase(Seqlist* seq, size_t pos);
void Seqlist_Remove(Seqlist* seq, SeqType value);
void Seqlist_Remove_all(Seqlist* seq, SeqType delete_value);
size_t Seqlist_size(Seqlist* seq);
int SeqlistEmpty(Seqlist* seq);
void SeqListBubbleSort(Seqlist* seq);
void SeqListBubbleSortEx(SeqList* seq, int(*cmp)(SType, SType));


seqlist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"seq_list.h"


//初始化顺序表
void Seqlist_init(Seqlist *seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	seq->capacity = LIST_SIZE_INIT;
	seq->data = (SeqType *)malloc(seq->capacity * sizeof(SeqType));
	if (seq->data != NULL){
		seq->size = 0;
	}
}

//判断顺序表是不是已满,如果满了,则扩充
void Seqlist_if_full(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	if (seq->size == LIST_SIZE_INIT){
		SeqType* new_data = (SeqType*)realloc(seq->data, sizeof(SeqType)*seq->capacity * 2);
		if (new_data != NULL){
			seq->data = new_data;
			seq->capacity *= 2;
		}
	}
}

//尾插一个元素
void Seqlist_pushback(Seqlist* seq, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	Seqlist_if_full(seq);
	size_t i = seq->size++;
	seq->data[i] = value;
}

//删除顺序表尾部的最后一个元素
void Seqlist_Popback(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	seq->size -= 1;
}


//往顺序表的前面插入一个元素 
void Seqlist_pushFront(Seqlist* seq, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	Seqlist_if_full(seq);
	size_t i = seq->size;
	seq->size++;
	for (; i > 0; i--){
		seq->data[i + 1] = seq->data[i];
	}
	seq->data[1] = seq->data[0];
	seq->data[0] = value;
}

//删除顺序表的第一个元素 
void Seqlist_popFront(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t i = 0;
	for (; i < seq->size - 1; i++){
		seq->data[i] = seq->data[i + 1];
	}
	seq->size--;
}
//取顺序表中任意位置的一个元素 
SeqType Seqlist_get(Seqlist* seq, size_t pos){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	if (pos >= seq->size){
		return default_value;
	}else{
		return seq->data[pos];
	}
}

//将顺序表中指定位置的值进行设置 
void Seqlist_Set(Seqlist* seq, size_t pos, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	if (pos >= seq->size){
		return default_value;
	}
	seq->data[pos] = value;
}

//查找顺序表中指定元素的下标 
size_t Seqlist_Find(Seqlist* seq, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t i = 0;
	for (; i < seq->size; i++){
		if (seq->data[i] == value){
			return i;
		}
	}
	return  -1;
}

//在指定位置插入元素 
void Seqlist_Insert(Seqlist* seq, size_t pos, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	Seqlist_if_full(seq);
	size_t i = seq->size;
	seq->size++;
	for (; i > pos; i--){
		seq->data[i + 1] = seq->data[i];
	}
	seq->data[pos + 1] = seq->data[pos];
	seq->data[pos] = value;
}

// 删除指定位置的元素 
void Seqlist_Erase(Seqlist* seq, size_t pos){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t i = 0;
	size_t j = seq->size - pos -1;
	for (; i < j; i++){
		seq->data[pos+i] = seq->data[pos + i+1];
	}
	seq->size--;
}

//删除顺序表中指定的值, 如果存在重复元素, 只删除第一个 
void Seqlist_Remove(Seqlist* seq, SeqType value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t m = 0;
	for (; m < seq->size; m++){
		if (seq->data[m] == value){
			size_t i = 0;
			size_t j = seq->size - m - 1;
			for (; i < j; i++){
				seq->data[m + i] = seq->data[m + i + 1];
			}
			seq->size--;
			break;
		}
	}
}

//删除顺序表中所有指定的值O(n^2)
void Seqlist_Remove_all(Seqlist* seq, SeqType delete_value){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t m = 0;
	for (; m < seq->size; m++){
		if (seq->data[m] == delete_value){
			size_t i = 0;
			size_t j = seq->size - m - 1;
			for (; i < j; i++){
				seq->data[m + i] = seq->data[m + i + 1];
			}
			seq->size--;
			m = 0;
		}
	}
}

//获取顺序表元素个数
size_t Seqlist_size(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	return seq->size;
}
//判断顺序表是否为空
int SeqlistEmpty(Seqlist* seq){
	if(seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	if (seq->size == 0){
		return 1;
	}
	else{
		return 0;
	}
}

//冒泡排序
void SeqListBubbleSort(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t i = 0;
	size_t j = 0;
	for ( i = 0; i < seq->size; i++){
		for (j = 0; j < seq->size - 1 - i; j++){
			if (seq->data[j + 1]<seq->data[j]){
				SeqType tmp = seq->data[j];
				seq->data[j] = seq->data[j + 1];
				seq->data[j + 1] = tmp;
			}
		}
	}
}

//打印顺序表
void printf_seqlist(Seqlist* seq){
	if (seq == NULL){
		printf("seq指针为空指针");
		return;
	}
	size_t i = 0;
	for (i = 0; i < seq->size; i++){
		printf("%c  ",seq->data[i]);
	}
	printf("\n");
}

test.c
#define _CRT_SECURE_NO_WARNINGS 1


#include"seq_list.h"
#include<stdio.h>
#include<stdlib.h>


void my_title(){
	printf("*******************TEST******************\n");
}


void testPushback(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	printf_seqlist(&seq);
}


void test_popback(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_Popback(&seq);
	printf_seqlist(&seq);
	Seqlist_Popback(&seq);
	printf_seqlist(&seq);
	Seqlist_Popback(&seq);
	printf_seqlist(&seq);
}


void test_push_front(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushFront(&seq, 'c');
	Seqlist_pushFront(&seq, 'd');
	printf_seqlist(&seq);
}
void test_popfront(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_popFront(&seq);
	Seqlist_popFront(&seq);
	printf_seqlist(&seq);
}


void test_getvalue(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	printf("%c\n",Seqlist_get(&seq, 0));
	printf("%c\n", Seqlist_get(&seq, 1));
	printf("%c\n", Seqlist_get(&seq, 2));
	printf("%c\n", Seqlist_get(&seq, 4));
}
void test_setvalue(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_Set(&seq, 0, 'z');
	Seqlist_Set(&seq, 3, 'm');
	printf_seqlist(&seq);
}


void test_seqlist_find(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	printf("您要查找的元素下标为:%d\n", Seqlist_Find(&seq, 'a'));
	printf("您要查找的元素下标为:%d\n", Seqlist_Find(&seq, 'b'));
	printf("您要查找的元素下标为:%d\n", Seqlist_Find(&seq, 'c'));
	printf("您要查找的元素下标为:%d\n", Seqlist_Find(&seq, 'e'));
}


void test_seqlist_insert(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_Insert(&seq, 0, 'z');
	Seqlist_Insert(&seq, 1, 'a');
	Seqlist_Insert(&seq, 2, 'z');
	printf_seqlist(&seq);
}
void test_Seqlist_erase(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_pushback(&seq, 'e');
	Seqlist_pushback(&seq, 'f');
	Seqlist_Erase(&seq, 1);
	printf_seqlist(&seq);
	Seqlist_Erase(&seq, 2);
	printf_seqlist(&seq);
}


void test_seqlist_remove(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_pushback(&seq, 'a');
	Seqlist_Remove(&seq, 'a');
	printf_seqlist(&seq);
}


void test_empty_size(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'd');
	Seqlist_pushback(&seq, 'a');
	printf("顺序表长度:%d\n", Seqlist_size(&seq));
	int ret = SeqlistEmpty(&seq);
	if (ret == 1){
		printf("顺序表为空\n");
	}
	else{
		printf("顺序表bu为空\n");
	}
}


void test_seqlist_bubbleSort(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'b');
	Seqlist_pushback(&seq, 'd');
	Seqlist_pushback(&seq, 'f');
	Seqlist_pushback(&seq, 'e');
	SeqListBubbleSort(&seq);
	printf_seqlist(&seq);
}
void test_pop_all(){
	Seqlist seq;
	my_title();
	Seqlist_init(&seq);
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'c');
	Seqlist_pushback(&seq, 'a');
	Seqlist_pushback(&seq, 'd');
	Seqlist_pushback(&seq, 's');
	Seqlist_pushback(&seq, 'e');
	Seqlist_Remove_all(&seq, 'a');
	printf_seqlist(&seq);
}


void test(){
	testPushback();
	test_popback();
	test_push_front();
	test_popfront();
	test_getvalue();
	test_setvalue();
	test_seqlist_find();
	test_seqlist_insert();
	test_Seqlist_erase();
	test_seqlist_remove();
	test_empty_size();
	test_seqlist_bubbleSort();
	test_pop_all();
}


int main(){
	test();
	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值