0
点赞
收藏
分享

微信扫一扫

SRRC认证的必要性:保障电子产品质量安全的重要措施

炽凤亮尧 2023-11-10 阅读 46
数据结构

 初始化

void StackInit(ST* ps)
{
//不初始化空间
	/*ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;*/
	//初始化空间
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	ps->top = 0;
	ps->capacity = 4;
}

Push

检查空间

static void check_capacity(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity == newCapacity;
		}
	}
}
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);
	check_capacity(ps);
	ps->a[ps->top] = x;
	ps->top++;//指向下一个
}

销毁

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

 判空

bool StackEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top == 0;
}

Pop

void StackPop(ST* ps)//出栈
{
	assert(ps);
	//if(ps->top>0)
	assert(!StackEmpty);
		ps->top--;
}

栈顶元素 

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty);
	return ps->a[ps->top-1];//top为最后一个元素的下一个
}

Size

int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
	StackSize(&ps);
	//ps.top

完整代码 

//stack.cpp
#include "Stack.h"
void StackInit(ST* ps)
{
	/*ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;*/
	//初始化空间
	ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
	if (ps->a == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	ps->top = 0;
	ps->capacity = 4;
}

static void check_capacity(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity == newCapacity;
		}
	}
}
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);
	check_capacity(ps);
	ps->a[ps->top] = x;
	ps->top++;//指向下一个
}
void StackPop(ST* ps)//出栈
{
	assert(ps);
	//if(ps->top>0)
	assert(!StackEmpty(ps));
		ps->top--;
}

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top-1];//top为最后一个元素的下一个
}
void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
bool StackEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top == 0;
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
//stack.h
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int capacity;
	int top;
}ST;

void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDatatype x);
void StackPop(ST* ps);//出栈
STDatatype StackTop(ST* ps);

bool StackEmpty(ST* ps);
int StackSize(ST* ps);
//test.cpp
#include "Stack.h"
void TestStack1()
{
	ST ps;
	StackInit(&ps);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackPush(&ps, 1);
	StackDestroy(&ps);
}
void TestStack2()
{
	ST ps;
	StackInit(&ps);
	StackPush(&ps, 2);
	printf("%d",StackTop(&ps));
	StackPop(&ps);
	StackSize(&ps);
	//ps.top
	StackDestroy(&ps);

}

int main()
{
	//TestStack1();
	TestStack2();

	return 0;
}
举报

相关推荐

0 条评论