其他分享
首页 > 其他分享> > 顺序表基本操作练习

顺序表基本操作练习

作者:互联网

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

typedef struct SeqList{
	int* array;
	int capacity;
	int size;
}SeqList;

extern void CheckCapacity(SeqList* ps);//检查容量
extern int SeqListEmpty(SeqList* ps);//检查内容是否为空
extern void	SeqListInit(SeqList* ps, int initCapacity);//初始化
extern void SeqListDestory(SeqList* ps);//销毁
extern void SeqListPushFront(SeqList* ps,int data);//头插
extern void SeqListPopFront(SeqList* ps);//头删
extern void SeqListPushBack(SeqList* ps,int data);//尾插
extern void SeqListPopBack(SeqList* ps);//尾删
extern void SeqListInsert(SeqList* ps,int pos,int data);//任意位置插入
extern void SeqListErase(SeqList* ps,int pos);//任意位置删除
extern int SeqListFind(SeqList* ps,int data);//查找
extern void PrintSeqList(SeqList* ps);//打印

#include "mySeqList.h"

void CheckCapacity(SeqList* ps) {//检查容量
	assert(ps);
	if (ps->size == ps->capacity) {
		//新空间容量是原有的二倍
		int newCapacity = ps->capacity * 2;
		//扩容
		int* temp = (int*)malloc(sizeof(int) * newCapacity);
		if (NULL == temp) {
			assert(0);
			return;
		}
		memcpy(temp, ps->array, ps->size * sizeof(int));
		free(ps->array);
		ps->array = temp;
		ps->capacity = newCapacity;
	}
}

int SeqListEmpty(SeqList* ps) {//检查内容是否为空
	return 0 == ps->size ;
}

void SeqListInit(SeqList* ps, int initCapacity) {//初始化
	assert(ps);
	initCapacity = initCapacity <= 0 ? 2 : initCapacity;
	ps->array=(int*)malloc(initCapacity*sizeof(int));
	if (NULL == ps->array ) {
		assert(0);
		return;
	}
	ps->capacity = initCapacity;
	ps->size = 0;
}

void SeqListDestory(SeqList* ps) {//销毁
	assert(ps);
	if (ps->array) {
		free(ps->array);
		ps->array = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}

void SeqListPushFront(SeqList* ps,int data) {//头插
	assert(ps);
	CheckCapacity(ps);
	for (int pos = ps->size-1; pos >= 0; pos--) {
		ps->array[pos + 1] = ps->array[pos];
	}
	ps->array[0] = data;
	ps->size++;
}

void SeqListPopFront(SeqList* ps) {//头删
	if (ps->size == 0) {
		return;
	}
	for (int pos = 1; pos < ps->size; pos++) {
		ps->array[pos - 1] = ps->array[pos];
	}
	ps->size--;
}

void SeqListPushBack(SeqList* ps,int data) {//尾插
	assert(ps);
	CheckCapacity(ps);
	ps->array[ps->size] = data;
	ps->size++;
}

void SeqListPopBack(SeqList* ps) {//尾删
	if (SeqListEmpty(ps)) {
		return;
	}
	ps->size--;
}


void SeqListInsert(SeqList* ps, int pos,int data) {//任意位置插入
	assert(ps);
	if (pos<0 || pos>ps->size) {
		printf("pos位置非法");
		return;
	}
	CheckCapacity(ps);
	for (int i = ps->size; i >= pos; i--) {
		ps->array[i + 1] = ps->array[i];
	}
	ps->array[pos] = data;
	ps->size++;
}


void SeqListErase(SeqList* ps,int pos) {//任意位置删除
	assert(ps);
	if (pos<0 || pos>ps->size) {
		printf("pos位置非法");
		return;
	}
	for (int i = pos; i < ps->size-1; i++) {
		ps->array[i] = ps->array[i + 1];
	}
	ps->size--;
}



int SeqListFind(SeqList* ps,int data) {//查找
	assert(ps);
	for (int i = 0; i < ps->size; i++) {
		if (ps->array[i] == data) {
			return i;
		}
	}
	return -1;
}

void PrintSeqList(SeqList* ps) {//打印
	assert(ps);
	for (int i = 0; i < ps->size; i++) {
		printf("%d ", ps->array[i]);
	}
	printf(" size:%d ", ps->size);
	printf(" capacity:%d ", ps->capacity);
	printf("\n");
}


#include "mySeqList.h"

int main() {
	SeqList s;
	SeqListInit(&s, 5);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 4);
	SeqListPushBack(&s, 5);
	PrintSeqList(&s);

	SeqListPushFront(&s, 1);
	SeqListPushFront(&s, 0);
	PrintSeqList(&s);//0 1 2 3 4 5
	SeqListPopFront(&s);
	PrintSeqList(&s);// 1 2 3 4 5

	SeqListPushBack(&s, 6);
	PrintSeqList(&s);//1 2 3 4 5 6
	SeqListPopBack(&s);
	PrintSeqList(&s);//1 2 3 4 5

	SeqListInsert(&s, 2, 9);
	PrintSeqList(&s);//1 2 9 3 4 5 
	SeqListErase(&s, 2);
	PrintSeqList(&s);//1 2 3 4 5

	printf("\n数字3的查找位置为%d\n",SeqListFind(&s, 3));


	return 0;
}

测试结果如下

 

标签:ps,顺序,int,SeqList,练习,基本操作,array,void,size
来源: https://blog.csdn.net/qq_59513256/article/details/123605379