栈(Stack)

1、基本概念

        栈(Stack)是一种遵循后进先出(LIFO, Last In First Out)原则的有序集合。这种数据结构只允许在栈顶进行添加(push)或删除(pop)元素的操作。栈的底部称为栈底,栈顶则是指允许添加或删除元素的那一端。

2、实现方式

栈可以通过数组或链表来实现。

  • 基于数组的栈:使用数组来存储栈中的元素,通常需要一个变量来记录栈顶的位置。当执行push操作时,将新元素添加到数组的末尾,并更新栈顶位置;pop操作则是移除数组末尾的元素,并相应地调整栈顶位置。如果栈的大小超过了数组的容量,则可能需要动态地调整数组的大小。

  • 基于链表的栈:使用链表来存储栈中的元素,链表的头部即为栈顶。push操作相当于在链表头部插入新节点;pop操作则是移除链表头部的节点。链表实现的栈在动态调整大小方面通常比数组实现更为灵活,但访问链表元素的时间复杂度可能较高。

3、数组栈实现

(1)定义

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;//数组 
	int top;//栈顶位置 
	int capacity;//数组容量 
}ST;

(2)初始化

//初始化
void StackInit(ST* ps)
{
	assert(ps);
	
	ps->a = NULL;
	ps->top = 0;//先给值,再++    ps->top = -1;先++,再给值 
	ps->capacity = 0;	
}

(3)销毁

//销毁
void StackDestroy(ST* ps)
{
	assert(ps);	
	
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

(4)插入(栈顶)

//栈顶插入
void StackPush(ST* ps,STDataType x)
{
	assert(ps);
	
	if(ps->top == ps->capacity)
	{
		//扩容 
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType)*newCapacity);//realloc对已有空间进行增容 
		
		if(tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		
		//更新数据 
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	
	//插入数据 
	ps->a[ps->top] = x;
	(ps->top)++; 
}

(5)删除(栈顶) 

//栈顶删除 
void StackPop(ST* ps)
{
	assert(ps);
	//assert(ps->top > 0);栈不为空 
	assert(!StackEmpty(ps)); 
	
	ps->top--;
}

(6)取栈顶数据

//取栈顶数据  
STDataType StackTop(ST* ps)
{
	assert(ps);
	//assert(ps->top > 0);栈不为空,栈为空所取值为a[-1],越界访问,是个随机值 
	assert(!StackEmpty(ps));
	 
	return ps->a[ps->top - 1];
}

(7)栈存放数据多少

//栈存放数据多少
int StackSize(ST* ps)
{
	assert(ps);
	
	return ps->top;
}

(8)栈是否为空

//判断栈是否为空 
bool StackEmpty(ST* ps)
{
	assert(ps);
	
	/*
	if(tps->top == 0)//为空 
	{
		return ture;
	}
	else//不为空 
	{
		return false;
	}
	*/
	
	return ps->top == 0;
}

 4、测试用例

main.c

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

void TestStack()
{
	ST st;
	StackInit(&st);
	
	//栈顶插入 
	StackPush(&st,5);
	StackPush(&st,7);
	StackPush(&st,8);
	StackPush(&st,11);
	
	printf("%d,%d ",StackSize(&st),StackTop(&st));
	
	//栈顶删除 
	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	//StackPop(&st);
	
	StackDestroy(&st);	
}


void Test1Stack()
{
	ST st;
	StackInit(&st);
	
	//栈顶插入 
	StackPush(&st,1);
	StackPush(&st,2);
	StackPush(&st,3);
	StackPush(&st,4);
	
	//出栈 
	printf("%d ",StackTop(&st));
	StackPop(&st);	
	printf("%d ",StackTop(&st));
	StackPop(&st);
	
	//栈顶插入
	StackPush(&st,5);
	StackPush(&st,6);
	
	//出栈,只能从栈顶出 
	while(!StackEmpty(&st))
	{
		printf("%d ",StackTop(&st));//取出栈顶数据 
		StackPop(&st);//删除当前栈顶数据,取下一个数据	
	}
	
	//坚持一个原则:后进先出(LIFO) 
	
	StackDestroy(&st);
}

int main(int argc, char *argv[]) 
{
	//TestStack();
	Test1Stack();
	return 0;
}

 

 

 

 

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题非常适合用程序解决。以下是用stack实现简单计算器的c代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define STACKLEN 1000 /* 的最大容量 */ typedef struct { int top; /* 顶指针 */ double data[STACKLEN]; /* 元素数组 */ } Stack; void push(Stack *pstack, double value) { if (pstack->top == STACKLEN - 1) { printf("Error: stack overflow!\n"); exit(EXIT_FAILURE); } else { pstack->data[++pstack->top] = value; } } double pop(Stack *pstack) { if (pstack->top == -1) { printf("Error: stack underflow!\n"); exit(EXIT_FAILURE); } else { return pstack->data[pstack->top--]; } } bool is_digit(char c) { return (c >= '0' && c <= '9'); } int precedence(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else return 0; } double calculate(double left, char op, double right) { switch (op) { case '+': return left + right; case '-': return left - right; case '*': return left * right; case '/': return left / right; default: printf("Error: invalid operator!\n"); exit(EXIT_FAILURE); } } double eval(char *expr) { Stack operandStack; operandStack.top = -1; Stack operatorStack; operatorStack.top = -1; int len = strlen(expr); int i = 0; while (i < len) { char c = expr[i]; if (is_digit(c)) { double value = 0.0; while (i < len && is_digit(expr[i])) { value = value * 10.0 + (double)(expr[i] - '0'); i++; } push(&operandStack, value); } else { while (operatorStack.top != -1 && precedence(operatorStack.data[operatorStack.top]) >= precedence(c)) { char op = operatorStack.data[operatorStack.top--]; double right = pop(&operandStack); double left = pop(&operandStack); push(&operandStack, calculate(left, op, right)); } push(&operatorStack, c); i++; } } while (operatorStack.top != -1) { char op = operatorStack.data[operatorStack.top--]; double right = pop(&operandStack); double left = pop(&operandStack); push(&operandStack, calculate(left, op, right)); } return pop(&operandStack); } int main() { char s[1000]; printf("Please enter an expression: "); scanf("%s", s); double result = eval(s); printf("Result: %f\n", result); return 0; } ``` 这段代码定义了两个:一个操作数(operandStack)和一个操作符(operatorStack),通过不断入和出的操作,实现对表达式进行求值。其中,is_digit函数用于判断一个字符是否是数字;precedence函数用于比较两个运算符的优先级;calculate函数用于计算两个操作数和一个操作符的运算结果;eval函数是主函数,用于将输入的表达式转化为数字计算结果。 希望这个回答能够帮助您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值