顺序表基本操作练习

#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;
}

测试结果如下

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值