顺序栈基本操作的实现

首先,定义一个结构体,结构体中有一个指针data,顺序栈的大小,还有一个capacuty,即相当于之前顺序链表的Max_Size,表示data这段内存中能容纳的元素个数,用于扩容。

1.初始化:给data开辟内存空间,size设为0,capacity自定义一个值,这里定义为1000;

2.销毁:释放data,并且将size与capacity置为0;

3.入栈:这里与前面学习的顺序表一样,将值直接插入size位置处,在 将size++,唯一的区别是这里如果栈是满了,进行扩容

4.扩容:这里就是按照自己的喜好,重新设置capacity的大小,再将原来data中的值倒进新开辟的这段内存,释放原来的内存,在将新内存赋给data;

5.出栈:直接size--;

6.取栈顶元素:直接将size这个位置的data取出来即可;

代码如下:

seqstack.h

#pragma once

typedef char SeqStackType;


typedef struct SeqStack {
	SeqStackType* data;
	size_t size;
	size_t capacity; //MAX_SIZE的替代品,data这段内存中能容纳的元素个数
}SeqStack;
//初始化
void SeqStackInit(SeqStack* stack);
//销毁
void SeqStackDestroy(SeqStack* stack);
//入栈
void SeqStackPush(SeqStack* stack,SeqStackType value);
//出栈
void SeqStackPop(SeqStack* stack);
//取栈顶元素
int  SeqStackTop(SeqStack* stack ,SeqStackType* value);

seqstack.c

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

#include "seqstack.h"
void SeqStackInit(SeqStack* stack){
	if(stack == NULL){
		return;
	}
	stack->size = 0;
	stack->capacity = 1000;
	stack->data = (SeqStackType*)malloc(stack->capacity*sizeof(SeqStackType));
	return;
}

void SeqStackDestroy(SeqStack* stack){
	free(stack->data);
	stack->size = 0;
	stack->capacity = 0;
}
//扩容
void SeqStackReSize(SeqStack* stack){
	if(stack == NULL){
		return;
	}
	if(stack->size < stack->capacity){
		return;
	}
	//扩容策略可以按照自己的喜好来定
	stack->capacity = stack->capacity * 2 + 1;
	SeqStackType* new_ptr = (SeqStackType*)malloc(stack->capacity*sizeof(SeqStackType));
	
	size_t i = 0;
	for(i = 0; i < stack->size;i++){
		new_ptr[i] = stack->data[i];
	}
	free(stack->data);
	stack->data = new_ptr;
	return;
}

void SeqStackPush(SeqStack* stack , SeqStackType value){
	if(stack == NULL){
		//非法输入
		return;
	}
	if(stack->size >= stack->capacity){
		//扩容
		SeqStackReSize(stack);
	}
	stack->data[stack->size] = value;
	stack->size++;
	return;
}

void SeqStackPop(SeqStack* stack){
	if(stack == NULL){
		//非法输入
		return;
	}
	if(stack->size == 0){
		//空栈
		return;
	}
	stack->size--;
	return;
}

int  SeqStackTop(SeqStack* stack,SeqStackType* value){
	if(stack == NULL){
		return 0;
	}
	if(stack->size == 0){
		return 0;
	}
	*value = stack->data[stack->size-1];
	return 1;
}

test.c

#include <stdio.h>
#define TEST_HEADER printf("\n========================%s=========================\n",__FUNCTION__) 

void SeqStackPrintChar(SeqStack* stack,const char* msg){
	printf("[%s]\n",msg);
	size_t i = 0;
	for(i=0;i<stack->size;i++){
		printf("[%c] ",stack->data[i]);
	}
	printf("\n");
}
void TestInit(){
	TEST_HEADER;
	SeqStack stack;
	SeqStackInit(&stack);
	printf("size expected 0 , actual %d\n",stack.size);
	printf("capacity expected 1000,actual %d\n",stack.capacity);

}

void TestDestroy(){
	TEST_HEADER;
	SeqStack stack;
	SeqStackInit(&stack);
	SeqStackDestroy(&stack);
	printf("capacity expacted 0 ,actual %d\n",stack.capacity);
}
void TestPush(){
	TEST_HEADER;
	SeqStack stack;
	SeqStackInit(&stack);
	SeqStackPush(&stack , 'a');
	SeqStackPush(&stack , 'b');
	SeqStackPush(&stack , 'c');
	SeqStackPush(&stack , 'd');

	SeqStackPrintChar(&stack ,"入栈四个元素");
}

void TestPop(){
	TEST_HEADER;
	SeqStack stack;
	SeqStackInit(&stack);
	SeqStackPush(&stack , 'a');
	SeqStackPush(&stack , 'b');
	SeqStackPush(&stack , 'c');
	SeqStackPush(&stack , 'd');

	SeqStackPop(&stack);
	SeqStackPop(&stack);
	SeqStackPrintChar(&stack,"出栈两个元素");
	SeqStackPop(&stack);
	SeqStackPop(&stack);
	SeqStackPrintChar(&stack,"再出栈两个元素");
	SeqStackPop(&stack);
	SeqStackPrintChar(&stack ,"尝试对空栈出栈");
}
void TestTop(){
	TEST_HEADER;
	SeqStack stack;
	SeqStackInit(&stack);
	SeqStackPush(&stack , 'a');
	SeqStackPush(&stack , 'b');
	SeqStackPush(&stack , 'c');
	SeqStackPush(&stack , 'd');

	SeqStackType value;
	int ret = SeqStackTop(&stack , &value);
	printf("ret expected 1,ctual %d\n",ret);
	printf("value expected d,actual %c\n",value);
}
int main(){
	TestInit();
	TestDestroy();
	TestPush();
	TestPop();
	TestTop();
	return 0;
}



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页