一个可以增删改查插的线性表(二)

    美好一天度过了一半,写完这个增加了新功能的线性表我就去收被子了,再晒就热的盖不了了orz

    接着上次的功能继续添加,过段时间我会更新一下,加入测试函数和BubbleSortEx( ),好了开始吧,首先介绍一下各个函数的功能:

给一个顺序表进行初始化 void SeqListInit(SeqList* seq); 
 将一个元素插入到顺序表的末尾  void SeqListPushBack(SeqList* seq, SeqType value); 
将顺序表的最后一个元素删除  void SeqListPopBack(SeqList* seq); 
往顺序表的前面插入一个元素 void SeqListPushFront(SeqList* seq, SeqType value); 
删除顺序表的第一个元素  void SeqListPopFront(SeqList* seq); 
取顺序表中任意位置的一个元素 SeqType SeqListGet(SeqList* seq, size_t pos, SeqType default_value); 
将顺序表中指定位置的值进行设置 void SeqListSet(SeqList* seq, size_t pos, SeqType value); 
查找顺序表中指定元素的下标  size_t SeqListFind(SeqList* seq, SeqType value); 
 在指定位置插入元素  void SeqListInsert(SeqList* seq, size_t pos, SeqType value); 
 删除指定位置的元素 void SeqListErase(SeqList* seq, size_t pos); 
删除顺序表中指定的值, 如果存在重复元素, 只删除第一个 void SeqListRemove(SeqList* seq, SeqType to_delete); 
删除顺序表中所有的指定的值 void SeqListRemoveAll(SeqList* seq, SeqType to_delete); 
获取顺序表元素个数 size_t SeqListSize(SeqList* seq); 
判定顺序表是否为空 int SeqListEmpty(SeqList* seq);
冒泡排序  void SeqListBubbleSort(SeqList* seq); 
冒泡排序  void SeqListBubbleSortEx(SeqList* seq, int (*cmp)(SType , SType)); 

代码如

seqlist.h

#pragma once

#include

#define SeqlistMaxSize 1000
typedef char SeqType;

typedef struct SeqListList{
	SeqListType data[SeqListMaxSize];
	size_t size;
}SeqList;

void SeqListInit(SeqList* seq);

void SeqListPushBack(SeqList* seqlist,SeqListType value);

void SeqListPopBack(SeqList* seqlist);

void SeqListPushFront(SeqList* seqlist,SeqListType value);

void SeqListPopFront(SeqList* seqlist)

size_t SeqListFind(SeqList* seq,SeqType value);

void SeqListInsert(SeqList* seq,size_t pos,SeqListType value);

void SeqListErase(SeqList* seqlist,size_t pos);

SeqType SeqListGet(SeqList *seqlist,size_t pos,SeqListType default_value);

void SeqListSet(SeqList* seqlist,size_t pos,SeqListType default_value);

void SeqListRemove(SeqList* seqlist,SeqListType to_delete);

void SeqListRemoveAll(SeqList* seqlist,SeqListType to_delete);

size_t SeqListSize(SeqList* seqlist);

int SeqListEmpty(SeqList* seqlist);

void SeqListBubbleSort(SeqList* seqlist);

void SeqListBubbleSortEx(SeqList* seqlist);

seqlist.c

#include
#include"seqlist.h"

void SeqListInit(SeqList* seqlist){
	if(seqlist == NULL){
		return;
	}
	seqlist->size = 0;
}

void SeqListPushBack(SeqList* seqlist,SeqType){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size >= SeqListMaxSize){
		return;
	}
	++seqlist->size;
	seqlist->data[seqlist->size] = value;
	return;
}

void SeqListPopBack(SeqList* seqlist){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size == 0){
		return;//kong shunxubiao
	}
	--seqlist->size;
}


void SeqListPushFront(SeqList* seqlist,SeqType value){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size >= SeqlistMaxSize){
		return;
	}
	++seqlist->size;
	size_t i = seqlist->size -1;
	for(;i>0;i--){
		seqlist->data[i] = seqlist->data[i-1];
	}
	seqlist->data[0] = value;
	return;
}

void SeqListPopFront(SeqList* seqlist){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size == 0){
		return;//kongshunxubiao
	}
	size_t i = 0;
	for(;isize - 1;++i){
		seqlist->data[i] = seqlist->data[i+1];
	}
	--seqlist->size;
	return;
}

void SeqListInsert(SeqList* seqlist,size_t pos,SeqType value){
	if(seqlist == NULL){
		return;
	}
	if(pos > seqlist->size){
		return;//pos yuejie pos->[0,pos]
	}
	if(seqlist->size >= SeqListMaxSize){
		return;//shunxubiao yijing manle
	}
	if(pos == 0 ){
		SeqListPushFront(seqlist,value);
		return;
	}
	++seqlist->size;
	size_t i = seqlist->size - 1;
	for(;i - 1 >= pos;--i){
		seqlist->data[i] = seqlist->data[i - 1];
	}
	seqlist->data[pos] = value;
	return;
}

void SeqListErase(SeqList* seqlist,size_t pos){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size == 0){
		return;
	}
	if(pos >= seqlist->size){
		return;//yuejie
	}
	size_t i = pos;
	for(;isize - 1;++i){
		seqlist->data[i] = seqlist->data[i+1];
	}
	--seqlist->size;
	return;
}

SeqType SeqListGet(SeqList* seqlist,size_t pos,SeqType default_value){
	if(seqlist == NULL){
		return;
	}
	if(SeqList->size == 0){
		return;
	}
	if(pos >= seqlist->size){
		return pos;
	}
	return seqlist->data[pos];
}

void SeqListSet(SeqList* seqlist,size_t pos,SeqType default_value){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->size == 0){
		return;
	}
	if(pos >= seqlist->size){
		return;//yuejie
	}
	seqlist->data[pos] = value;
	return;
}

size_t SeqListFind(SeqList* seqlist,SeqType value){
	if(seqlist == NULL){
		return;
	}
	size_t i = 0;
	for(;i < seqlist->size;++i){
		if(value == seqlist->data[i]){
			return i;
		}
	}
	return (size_t)-1;
}

void SeqListRemove(SeqList* seqlist,SeqType to_delete){
	 if(seqlist == NULL){
		 return;
	 }
	 size_t i = 0;
	 for(;i < seqlist->size;++i){
		 if(seqlist->data[i] == to_delete){
			 for(;i < seqlist->size;++i){
				 seqlist->data[i] == seqlist->data[i+1];
			 }
		 }
		 break;
	 }
	 --seqlist->size;
	 return;
}

void SeqListRemoveAll(SeqList* seqlist,SeqType value){
	if(seqlist == NULL){
		return;
	}
	size_t i = 0;
	size_t j = 0
	for(;i < seqlist->size;++i){
		if(seqlist->data[i] == value){
			for(j = i;j < seqlist->size;++j){
				seqlist->data[j] == seqlist->data[j+1];
			}
		}
		--seqlist->size;
	}
	return;
}

size_t SeqListSize(SeqList* seqlist){
	if(seqlist == NULL){
		return NULL;
	}
	size_t i = 1;
	while(i <= seqlist->size){
		;
		++i;
	}
	return i;
}

int SeqListEmpty(SeqList* seqlist){
	if(seqlist == NULL){
		return;
	}
	if(seqlist->data[0] != NULL){
		return 0;
	}
	else{
		return 1;
	}
}

void SeqListBubbleSort(SeqList* seqlit){
	if(seqlit == NULL){
		return;
	}
	size_t i = 0;
	size_t j = 0;
	for(;i < seqlist->size;++i){
		for(j = i;j < seqlist->size;++j){
			SeqType value = 0;
			if(seqlist->data[i] > seqlist->data[i+1]){
				value = seqlist->data[i];
				seqlist->data[i] = seqlist->data[i+1];
				seqlist->data[i+1] = value;
			}
		}
	}
	return;
}
    喜欢的小伙伴请为我点个赞,或留言评论吧,如果代码中有错误请在评论中指出,我会认真吸取意见,谢谢阅读。

你可能感兴趣的:(数据结构-线性表)