顺序表的实现(1)

实现基于静态数组的顺序表的以下基本操作:

1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 修改任意位置元素

7. 在任意位置插入元素

 

seqlist.h
#pragma once
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>

#define SeqlistMaxSize 20
#define Header_Print printf("\n==========%s==========\n",__FUNCTION__);
typedef char SeqlistType;

typedef struct Seqlist{
	SeqlistType SeqlistArr[SeqlistMaxSize];
	size_t size;
}Seqlist;


void SeqlistInit(Seqlist* seqlist);
void SeqlistPrint(Seqlist* seqlist,SeqlistType* ch);
void SeqlistPushBack(Seqlist* seqlist,SeqlistType value);
void SeqlistPopBack(Seqlist* seqlist);
void SeqlistPushFront(Seqlist* seqlist,SeqlistType value);
void SeqlistPopFront(Seqlist* seqlist);
void SeqlistInsert(Seqlist* seqlist,size_t pos,SeqlistType value);
void SeqlistErase(Seqlist* seqlist,size_t pos);
seqlist.c
#include"seqlist.h"
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>

void SeqlistInit(Seqlist* seqlist){            //初始化      
	if(seqlist == NULL){
		return;
	}
	seqlist->size=0;
}

void SeqlistPrint(Seqlist* seqlist,SeqlistType *ch){            //顺序表的打印
	if(seqlist == NULL){
		return;
	}
	size_t i=0;
	printf("%s\n",ch);
	for(;i < seqlist->size;i++){                         //将顺序表中的元素逐个打印
		printf("%c  ",seqlist->SeqlistArr[i]);
	}
	printf("\n");
}

void SeqlistPushBack(Seqlist* seqlist,SeqlistType value){        //尾插
	if(seqlist == NULL){                                     //指针判空
		return;
	}
	if(seqlist->size >= SeqlistMaxSize){                    //顺序表已满,不能再继续插入
		return;
	}
	seqlist->SeqlistArr[seqlist->size]=value;
	++seqlist->size;
}


void SeqlistPopBack(Seqlist* seqlist){                         //尾删
	if(seqlist == NULL){                                   //指针判空
		return;
	}
	if(seqlist->size == 0){                                //顺序表为空,无法删除
		return;
	}
	seqlist->size--;
}

void SeqlistPushFront(Seqlist* seqlist,SeqlistType value){    //头插
	if(seqlist == NULL){                                  //指针判空
		return;
	}
	if(seqlist->size >= SeqlistMaxSize){                 //顺序表已满,不能再继续插入
		return;
	}
	int i = seqlist->size-1;
	for(;i>=0;i--){
		seqlist->SeqlistArr[i+1] = seqlist->SeqlistArr[i];
	}
	seqlist->SeqlistArr[0]=value;
	seqlist->size ++;
}


void SeqlistPopFront(Seqlist* seqlist){                    //头删
	if(seqlist == NULL){                               //指针判空
		return;
	}
	if(seqlist->size == 0){                            //顺序表为空,无法删除
		return;
	}
	size_t i=0;
	for(;i < seqlist->size-1;i++){
		seqlist->SeqlistArr[i]=seqlist->SeqlistArr[i+1];
	}
	--seqlist->size;
}


void SeqlistInsert(Seqlist* seqlist,size_t pos,SeqlistType value){        //在任意位置插入元素
	if(seqlist == NULL){                                //指针判空
		return;
	}
	if(seqlist->size >= SeqlistMaxSize){                //顺序表已满,不能再继续插入
		return;
	}
	if(pos > seqlist->size-1){                         //所要插入的元素位置超出范围
		return;
	}
	++seqlist->size;
	size_t i = seqlist->size-1;
	for(;i >= pos;i--){
		seqlist->SeqlistArr[i+1]=seqlist->SeqlistArr[i];
	}
	seqlist->SeqlistArr[pos]=value;
}
 

void SeqlistErase(Seqlist* seqlist,size_t pos){            //在任意位置删除元素
	if(seqlist == NULL){                               //指针判空
		return;
	}
	if(pos > seqlist->size){                           //所要删除的元素位置超出范围
		return;
	}
    for(;pos < seqlist->size;pos++){
		seqlist->SeqlistArr[pos] = seqlist->SeqlistArr[pos+1];
	}
	--seqlist->size;
}
test.c
#include"seqlist.h"
#include<stdio.h>

void TestSeqlistInit(){                               //测试初始化函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPrint(&seqlist,"Seqlist Init");
}


void TestSeqlistPushBack(){                         //测试尾插函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'b');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');            //插入四个元素a,b,c,d

	SeqlistPrint(&seqlist,"insert four elements to the tail");
}


void TestSeqlistPopBack(){                      //测试尾删函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);
	
	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'b');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');        //先尾插四个元素a,b,c,d
	SeqlistPopBack(&seqlist);
	SeqlistPopBack(&seqlist);            //再尾删两个元素

	SeqlistPrint(&seqlist,"delete two elements from the tail");
}


void TestSeqlistPushFront(){                //测试头插函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'b');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');        //先尾插四个元素a,b,c,d
	SeqlistPushFront(&seqlist,'x');
	SeqlistPushFront(&seqlist,'y');       //再头插两个元素x,y

	SeqlistPrint(&seqlist,"insert two elements to the head");
}


void TestSeqlistPopFront(){                  //测试头删函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'b');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');        //先尾插四个元素a,b,c,d
	SeqlistPopFront(&seqlist);
	SeqlistPopFront(&seqlist);            //再头删两个元素
	
	SeqlistPrint(&seqlist,"delete two elements from the head");
}


void TestSeqlistInsert(){                    //测试插入函数
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');        //先尾插三个元素a,c,d
	SeqlistInsert(&seqlist,1,'b');        //再在下标为1的地方插入元素b

	SeqlistPrint(&seqlist,"insert an element to the specific position");
}


void TestSeqlistErase(){
	Header_Print;
	Seqlist seqlist;
	SeqlistInit(&seqlist);

	SeqlistPushBack(&seqlist,'a');
	SeqlistPushBack(&seqlist,'b');
	SeqlistPushBack(&seqlist,'c');
	SeqlistPushBack(&seqlist,'d');        //先尾插四个元素a,b,c,d
	SeqlistErase(&seqlist,3);             //再删除下标为3的元素

	SeqlistPrint(&seqlist,"delete an element from the specific position");
}


int main(){
	TestSeqlistInit();
	TestSeqlistPushBack();
	TestSeqlistPopBack();
	TestSeqlistPushFront();
	TestSeqlistPopFront();
	TestSeqlistInsert();
	TestSeqlistErase();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值