顺序表的实现

空间复杂度
  • 对于一般的递归,其实都是深搜,对于深搜,空间复杂度是递归层数而不是递归次数,因为在某一个方向递归结束后,所占的栈区就释放了,相当于对于这几个内存重复使用

顺序表

###静态顺序表

  • typedef int SLDataType;
    #define N 10
    struct SeqList {
    	SLDataType a[N];
    	int size;
    };
    

动态顺序表

  • seqlist.h

  • #pragma once
    
    #include
    #include
    #include
    
    typedef int SLDataType;
    #define INIT_N 4
    #define N 10
    
    typedef struct SeqList {
    	SLDataType* a;
    	int size;//现在已经使用的数量
    	int capacity;//现在的最大容量
    }SL;
    
    void SeqInit(SL* s);
    void SeqDestory(SL* s);
    void print(SL* ps);
    void SLCheckCapacity(SL* ps);
    void SLPushBack(SL* ps, SLDataType data);
    void SLPopBack(SL* ps);
    void SLPushfront(SL* ps, SLDataType data);
    void SLPopfront(SL* ps);
    void SLInsert(SL* ps, int pos, SLDataType data);
    void SLErase(SL* ps, int pos);
    int SLFind(SL* ps, SLDataType x);
    
  • seqlist.c

  • #define  _CRT_SECURE_NO_WARNINGS 1
    
    #include"SeqList.h"
    
    void SLInsert(SL* ps, int pos, SLDataType data) {
    	//传入的pos实际上是待插入位置的下标
    	assert(ps && pos >= 0 && pos <= ps->size);//相当于在pos这个位置让它变成data,其余的数字依次向后移动一个
    	SLCheckCapacity(ps);
    
    	int end = ps->size - 1;
    	while (end >= pos) {
    		ps->a[end + 1] = ps->a[end];
    		end--;
    	}
    	ps->a[pos] = data;
    	ps->size++;
    }
    
    void SLCheckCapacity(SL* ps) {
    	if (ps->size == ps->capacity) {
    		SLDataType* tem = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capacity * 2);
    		if (tem == NULL) {
    			perror("realloc");
    			return;
    		}
    		ps->a = tem;
    		ps->capacity *= 2;
    	}
    }
    
    void print(SL* ps) {
    	for (int i = 0; i < ps->size; i++) {
    		printf("%d ", ps->a[i]);
    	}
    	printf("\n");
    }
    
    void SeqInit(SL* ps) {
    	ps->a =(SLDataType*)malloc(sizeof(SLDataType)* INIT_N);
    	ps->size = 0;
    	ps->capacity = INIT_N;
    }
    
    void SeqDestory(SL* ps){
    	free(ps->a);
    	ps->a = NULL;
    	ps->capacity = ps->size = 0;
    }
    
    void SLPushBack(SL* ps, SLDataType data) {
    
    	//SLCheckCapacity(ps);
    	对于插入操作,统一在一进入函数的时候进行扩容,
    	首先保证一定有空间插入新元素即可
    	//ps->a[ps->size++] = data;
    
    	SLInsert(ps, ps->size, data);
    }
    
    void SLPopBack(SL* ps) {
    	//if (ps->size>0) {
    	//	ps->size--;
    	//}
    	SLErase(ps, ps->size - 1);
    }
    
    void SLPushfront(SL* ps, SLDataType data) {
    	//SLCheckCapacity(ps);
    	//int tem = ps->size;
    	//while (tem>0) {
    	//	ps->a[tem] = ps->a[tem - 1];
    	//	tem--;
    	//}
    	//ps->a[0] = data;
    	//ps->size++;
    	SLInsert(ps,0, data);
    }
    
    void SLPopfront(SL* ps) {
    	//for (int i = 1; i < ps->size; i++) {
    	//	ps->a[i - 1] = ps->a[i];
    	//}
    	//if (ps->size != 0) {
    	//	ps->size--;
    	//}
    	SLErase(ps, 0);
    }
    
    void SLErase(SL* ps, int pos) {
    	assert(ps);
    	assert(pos >= 0 && pos < ps->size);
    	int begin = pos + 1;
    	while (begin < ps->size) {
    		ps->a[begin - 1] = ps->a[begin];
    		begin++;
    	}
    	ps->size--;
    }  
    int SLFind(SL* ps, SLDataType x) {
    	for (int i = 0; i < ps->size; i++) {
    		if (ps->a[i] == x) {
    			return i;
    		}
    	}
    	return -1;
    }
    
  • test.c

  • #include "SeqList.h"
    
    void TestSeqList() {
    	SL s;
    	SeqInit(&s);
    
    	SLPushBack(&s, 1);
    	SLPushBack(&s, 2);
    	SLPushBack(&s, 3);
    	SLPushBack(&s, 4);
    	SLPushBack(&s, 5);
    
    	SLPopBack(&s);
    	SLPopBack(&s);
    
    	SLPushfront(&s, -1);
    	SLPushfront(&s, -2);
    	SLPushfront(&s, -3);
    
    	SLPopfront(&s);
    	SLPopfront(&s);
    
    	SLInsert(&s, 0, 100);
    	SLInsert(&s, 0, 100);
    
    	SLErase(&s, 0);
    
    	print(&s);
    
    	SeqDestory(&s);
    }
    
    int main() {
    	
    	TestSeqList();
    
    	return 0;
    }
    

你可能感兴趣的:(数据结构,顺序表)