SeqList.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>typedef int SLDataType;
//#define NULL 0typedef struct SeqList {SLDataType* a;int size;//顺序表中存储的有效元素的个数int capacity;//空间的大小
}SL;void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SPrint(SL* ps);void SLPopBack(SL* ps);
void SLPushBack(SL* ps,SLDataType x);
void SLCheckCapacity(SL* ps);void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);void SLInsert(SL* ps, int pos, SLDataType x);
int SLFind(SL* ps, SLDataType x);
void SLErase(SL* ps, int pos);
void SLModify(SL* ps, int pos, SLDataType x);
SeqList.c
#include"SeqList.h"void SLInit(SL* ps) {assert(ps);ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);if (ps->a == NULL) {perror("malloc failed");exit(-1);}ps->size = 0;ps->capacity = 4;
}void SLDestroy(SL* ps){assert(ps);free(ps->a);ps->a = NULL;ps->size = 0;ps->capacity = 0;
}void SPrint(SL* ps) {assert(ps);for (int i = 0; i < ps->size; i++) {printf("%d ", ps->a[i]);}printf("\n");
}void SLPopBack(SL* ps) {assert(ps);//assert(ps->size > 0);//ps->size--;SLErase(ps, ps->size-1);
}void SLPushBack(SL* ps, SLDataType x) {assert(ps);//SLCheckCapacity(ps);//ps->a[ps->size] = x;//ps->size++;SLInsert(ps, ps->size-1, x);
}void SLCheckCapacity(SL* ps) {assert(ps);if (ps->size == ps->capacity) {SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(SLDataType));if (tmp == NULL) {perror("realloc failed");exit(-1);}ps->a = tmp;ps->capacity = ps->capacity * 2;}
}void SLPushFront(SL* ps, SLDataType x) {assert(ps);//SLCheckCapacity(ps);//int end = ps->size - 1;//while (end >= 0) {// ps->a[end + 1] = ps->a[end];// --end;//}//ps->a[0] = x;//ps->size++;SLInsert(ps, 0, x);
}void SLPopFront(SL* ps) {assert(ps);//assert(ps->size > 0);//int begin = 1;//while (begin < ps->size) {// ps->a[begin - 1] = ps->a[begin];// ++begin;//}//ps->size--;SLErase(ps, 0);
}void SLInsert(SL* ps, int pos, SLDataType x) {assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);int end = ps->size - 1;while (end >= pos) {ps->a[end + 1] = ps->a[end];--end;}ps->a[pos] = x;ps->size++;
}int SLFind(SL* ps, SLDataType x) {assert(ps);for (int i = 0; i < ps->size; i++) {if (ps->a[i] == x) {return i;}}return -1;
}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--;
}void SLModify(SL* ps, int pos, SLDataType x) {assert(ps);assert(pos >= 0 && pos < ps->size);ps->a[pos] = x;
}
移除元素
int removeElement(int* nums, int numsSize, int val){int n = numsSize;int begin = 0;int end = 0;while(begin<n){if(nums[begin]!=val){nums[end] = nums[begin];end++;begin++;}else{begin++;}}return end;
}
合并两个有序数组
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){int end1 = m-1;int end2 = n-1;int end = n+m-1;while(end1>=0 && end2>=0){if(nums1[end1]>=nums2[end2]){nums1[end] = nums1[end1];end--;end1--;}else{nums1[end] = nums2[end2];end--;end2--;}}while(end2>=0){nums1[end--] = nums2[end2--];}
}