1. 程式人生 > >順序棧基本操作的實現

順序棧基本操作的實現

首先,定義一個結構體,結構體中有一個指標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;
}