[数据结构]顺序栈实现(C语言)

 栈如何实现,一共有下述几种实现方式:

方式1.定义一个栈底指针用于动态开辟空间,栈顶使用int指示位置

typedef struct Friend {
	int* data;//栈底指针
	int top;//栈顶位置
	int maxsize;
}Friend;

此方式方便进行存储空间realloc扩容,如后续程序所示.


方式2.定义固定大小空间数组,栈顶使用int指示位置

typedef struct Friend {
	int* data[MAXSIZE];//固定大小数组
	int top;//栈顶位置
	int maxsize;
}Friend;

此方式数组大小已经固定,不能进行扩容 ,如后续程序所示.

方式3.栈底使用栈底指针指示位置,栈顶使用指针指示位置

本文栈底对应的位置不存储数据,在栈底下一个位置存储数据,这样在清空栈时(栈顶指针指向栈底)能确保所有数据能够清空.

typedef struct Friend {
	int* data;//栈底指针
	int* top;//栈顶位置
	int maxsize;
}Friend;

此外,方式3扩容时会很麻烦,个人认为不推荐,因为realloc很可能会异地开辟新空间,我们需要栈顶指针指向新空间的位置来存放数据,但栈顶指针迁徙到新空间会比较费劲,如下图所示:

方式1的C语言实现:

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

#define MAXSIZE 10
#define OVERFLOW -1

typedef struct Friend {
	int* data;//栈底指针
	int top;//栈顶位置
	int maxsize;
}Friend;

int Init_Stack(Friend*, int); 
void Push_Stack(Friend*, int); 
void Pop_Stack(Friend*, int*); 
void Print_Stack(Friend*); 
void realloc_Stack(Friend*); 
int Get_TopStack(Friend*); 
void Clear_Stack(Friend*); 
int Length_Stack(Friend*);

int main() {
	
	Friend friend1;	 
	int newsize=20,getvalue=0;
    /*初始栈空间*/
	Init_Stack(&friend1, MAXSIZE);
	for(int k=0;k< MAXSIZE;k++){           //压入数据
		Push_Stack(&friend1, 10 * k + 10);
	}	
    /*输出栈元素*/	
	Print_Stack( &friend1);
    /*开辟新的栈空间*/
	realloc_Stack(&friend1, newsize);
	for(int j= MAXSIZE;j< newsize;j++){
		Push_Stack(&friend1, 10 * j + 10);
	}

	printf("\n"); 
	Print_Stack(&friend1); 
	printf("栈顶元素=%d\n", Get_TopStack(&friend1));  
	printf("current length of Stack=%d\n", Length_Stack(&friend1)); 
	Pop_Stack(&friend1, &getvalue); printf("getvalue=%d\n", getvalue); 
	printf("current length of Stack=%d\n", Length_Stack(&friend1)); 
	Print_Stack(&friend1);
	Clear_Stack(&friend1); 
	Print_Stack(&friend1);

	return 0;
}	

/*初始栈*/
int	Init_Stack(Friend* input_ptr,int maxsize) {
		input_ptr->data = (int*)malloc(sizeof(int) * MAXSIZE);//数组开辟10int大小	
	if(input_ptr->data==NULL){
		return OVERFLOW;}
	input_ptr->top = -1;
	input_ptr->maxsize = maxsize;
	return 0;
}

/*执行压栈操作*/
void Push_Stack(Friend* input_ptr,int value)
{
	input_ptr->top++;//当前栈顶已有数据,栈顶指向下一位
	input_ptr->data[input_ptr->top] = value;
}

/*执行出栈操作*/
void Pop_Stack(Friend* input_ptr,int* e)//出栈:栈顶元素用e返回,栈顶位标减1;
{
	e= input_ptr->data[input_ptr->top];
	(input_ptr->top)--;
}

/*打印栈存储元素*/
void Print_Stack(Friend* input_ptr)
{
	if(input_ptr->top<0){
		printf("Stack is empty");
	}
	for(int i=0;i<=(input_ptr->top);i++){
		printf("ptr->start[%d]=%d\n", i, input_ptr->data[i]);
	}
}

/*重新分配栈空间*/
void realloc_Stack(Friend* input_ptr,int newsize)
{
	int* str = (int*)realloc(input_ptr->data, sizeof(int) * newsize);
	if(str==NULL){
		printf("realloc overflow");
		return -1;
	}
	input_ptr->data = str;
	return 0;
}

/*取出栈顶元素*/
int Get_TopStack(Friend* input_ptr) //只取出栈顶元素,栈顶位不需要减1
{
	if((input_ptr->top)<0){
		printf("Stack is empty\n");
	return 0;
    }
	return input_ptr->data[input_ptr->top];
}

/*清空栈*/
void Clear_Stack(Friend* input_ptr){//栈顶位指向-1
	printf("Stack is cleared\n");
	input_ptr->top = -1;
}

/*栈长度*/
int Length_Stack(Friend* input_ptr) 
{
	if (input_ptr < 0) {
		printf("Stack is empty\n");
		return -1;
	}
	return (input_ptr->top) + 1;
}

出栈:当栈顶左移时,原来栈顶指向的位置依然有数据,不过已经不属于我们所定义的栈了,当新数据存入时,会覆盖原来位置上的数据.

方式2的C语言实现:

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

#define MAXSIZE 10
#define OVERFLOW -1

typedef struct Friend {

	int data[MAXSIZE];//定义数组存储数据
	int top;//栈顶位置
	int maxsize;
}Friend;
int Init_Stack(Friend*, int); //初始栈
void Push_Stack(Friend*, int); //压栈
void Pop_Stack(Friend*, int*); //出栈
void Print_Stack(Friend*); //输出栈元素

int Get_TopStack(Friend*); //获取栈顶元素
void Clear_Stack(Friend*); //清空栈
int Length_Stack(Friend*);//栈长度

int main() {
	Friend friend1;	 
	
	int newsize=20,getvalue=0;//newsize为新的空间大小,getvalue用于取走栈顶元素
    /*初始栈空间*/
	Init_Stack(&friend1, MAXSIZE);
	for(int k=0;k< MAXSIZE;k++){           //压入数据
		Push_Stack(&friend1, 10 * k + 10);
	}	
    /*输出栈元素*/	
	Print_Stack( &friend1);
    /*开辟新的栈空间
	realloc_Stack(&friend1, newsize);
	for(int j= MAXSIZE;j< newsize;j++){//
		Push_Stack(&friend1, 10 * j + 10);
	}*/

	printf("\n"); 
	Print_Stack(&friend1); 
	printf("栈顶元素=%d\n", Get_TopStack(&friend1));  
	printf("current length of Stack=%d\n", Length_Stack(&friend1)); 
	Pop_Stack(&friend1, &getvalue); printf("getvalue=%d\n", getvalue); 
	printf("current length of Stack=%d\n", Length_Stack(&friend1)); 
	Print_Stack(&friend1);
	Clear_Stack(&friend1); 
	Print_Stack(&friend1);

	return 0;
}	

/*初始栈*/
int	Init_Stack(Friend* input_ptr,int maxsize) {
	input_ptr->top = -1;	//栈顶指向数组首元素之前
    input_ptr->maxsize = maxsize;//保存当前存储空间大小
	
	return 0;
}
/*执行压栈操作*/
void Push_Stack(Friend* input_ptr,int value)
{
	input_ptr->top++;//当前栈顶指针指向位置已有数据,栈顶指向下一位
	input_ptr->data[input_ptr->top] = value;
}

/*执行出栈操作*/
void Pop_Stack(Friend* input_ptr,int* e)//出栈:栈顶元素用e返回,栈顶位标减1;
{
	*e= input_ptr->data[input_ptr->top];
	(input_ptr->top)--;
}

/*打印栈存储元素*/
void Print_Stack(Friend* input_ptr)
{
	if(input_ptr->top<0){
		printf("Stack is empty");
	}
	for(int i=0;i<=(input_ptr->top);i++){
		printf("ptr->start[%d]=%d\n", i, input_ptr->data[i]);
	}
}

/*取出栈顶元素*/
int Get_TopStack(Friend* input_ptr) //只取出栈顶元素,栈顶位不需要减1
{
	if((input_ptr->top)<0){
		printf("Stack is empty\n");
	return 0;
    }
	return input_ptr->data[input_ptr->top];
}

/*清空栈*/
void Clear_Stack(Friend* input_ptr){//栈顶位指向-1
	printf("Stack is cleared\n");
	input_ptr->top = -1;
}

/*栈长度*/
int Length_Stack(Friend* input_ptr) 
{
	if (input_ptr < 0) {
		printf("Stack is empty\n");
		return -1;
	}
	return (input_ptr->top) + 1;
}

方式3的C语言实现:

为了方便清空时(input_ptr->top==input_ptr->data)清除全部数据,本文设置开辟空间首位存栈长度,栈实际从开辟空间第二个int为开始

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

#define MAXSIZE 10
#define OVERFLOW -1

typedef struct Friend {
	int* data;//栈底指针
	int* top;//栈顶位置
	int maxsize;
}Friend;

int Init_Stack(Friend*, int);
void Push_Stack(Friend*, int);
void Pop_Stack(Friend*, int*);
void Print_Stack(Friend*);
int realloc_Stack(Friend*, int);
int Get_TopStack(Friend*);
void Clear_Stack(Friend*);
int Length_Stack(Friend*);

int main() {

	Friend friend1;
	int newsize = 20, getvalue = 0;
	/*初始栈空间*/
	Init_Stack(&friend1, MAXSIZE);
	/*压入10个数据*/
	for (int k = 0; k < MAXSIZE; k++) {
		Push_Stack(&friend1, 10 * k + 10);
	}

	/*输出栈元素*/
	Print_Stack(&friend1);	//第一次打印栈
	printf("栈顶元素=%d\n", Get_TopStack(&friend1));
	printf("current length of Stack=%d\n", Length_Stack(&friend1));
	Pop_Stack(&friend1, &getvalue); printf("出栈,getvalue=%d\n", getvalue);//出栈一个元素
	printf("current length of Stack=%d\n", Length_Stack(&friend1));
	printf("栈顶元素=%d\n", Get_TopStack(&friend1));
	printf("\n");

	Print_Stack(&friend1);//第二次打印栈
	Clear_Stack(&friend1);
	Print_Stack(&friend1);

	return 0;
}

/*初始栈*/
int Init_Stack(Friend* input_ptr, int maxsize) {
	input_ptr->data = (int*)malloc(sizeof(int) * (MAXSIZE+1));//数组开辟10int+int大小,+1为最前面存储栈长度的空间
	if (input_ptr->data == NULL) {
		return OVERFLOW;
	}
	printf("first address=%p\n", input_ptr->data);
	input_ptr->top = input_ptr->data;//初始栈顶指针与栈底指针重合
	input_ptr->maxsize = maxsize;
	*(input_ptr->top) = input_ptr->maxsize;
	return 0;
}
/*执行压栈操作*/
void Push_Stack(Friend* input_ptr, int value) {
	if(((input_ptr->top)-(input_ptr->data))>=(input_ptr->maxsize)){
		printf("栈满\n");
		return ;
	}

	input_ptr->top++;//栈顶指向下一位
	*(input_ptr->top) = value;
	return ;
}
/*执行出栈操作*/
void Pop_Stack(Friend* input_ptr, int* e) {//出栈:栈顶元素用e返回,栈顶位标减1;
	*e = *(input_ptr->top - 1);
	(input_ptr->top)--;
	return;
}

/*打印栈存储元素*/
void Print_Stack(Friend* input_ptr) {
	int* search = input_ptr->data+1;
	if ((input_ptr->top) == (input_ptr->data)) {
		printf("Stack is empty\n");
		return;
	}
	printf("栈长度=%d\n", (input_ptr->top) - (input_ptr->data));

	for (int j = 1; j < ((input_ptr->top) - (input_ptr->data))+1; j++) {//从栈的第一个元素开始打印,不打印空间首元素(栈长度)
		printf("ptr->start[ ]=%d\n", *(search));
		search++;
	}
}

/*重新分配栈空间*/
int realloc_Stack(Friend* input_ptr, int newsize) {
	int* str = (int*)realloc(input_ptr->data, sizeof(int) * newsize);
	if (str == NULL) {
		printf("realloc overflow");
		return -1;
	}
	input_ptr->data = str; printf("realloc  address:%p\n", str);

	return 0;
}

/*取出栈顶元素*/
int Get_TopStack(Friend* input_ptr) {//只取出栈顶元素,栈顶位不需要减1
	if (input_ptr->top==input_ptr->data ) {
		printf("Stack is empty\n");
		return 0;
	}
	return *((input_ptr->top));
}

/*清空栈*/
void Clear_Stack(Friend* input_ptr)
{                                    //
	printf("Stack is cleared\n");
	input_ptr->top = input_ptr->data;

}

/*栈长度*/
int Length_Stack(Friend* input_ptr) {
	if (input_ptr->top ==input_ptr->data) {
		printf("Stack is empty\n");
		return -1;
	}
	return (input_ptr->top) - (input_ptr->data);
}

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值