栈的基本操作

栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。

栈的基本操作有:

1:构造一个空栈        status InitStack(SqStack& S)

//构造一个空栈
status InitStack(SqStack& S) {
	S.base = (Selemtype*)malloc(STACK_INIT_SIZE * sizeof(Selemtype));
	if (!S.base)
		return Error;
	S.top = S.base;
	S.stasksise = STACK_INIT_SIZE;
	return OK;
}

2:若栈不空,用元素返回栈的栈顶元素        status Getelem(SqStack S, Selemtype& e)

//若栈不空,用元素返回栈的栈顶元素
status Getelem(SqStack S, Selemtype& e) {
	if (S.top == S.base)
		return Error;
	e = *(S.top - 1);
	return OK;
}

3:插入新的栈顶元素        status Push(SqStack& S, Selemtype e)

插入新的栈顶元素
status Push(SqStack& S, Selemtype e) {
	if (S.top - S.base >= S.stasksise)
	{
		S.base = (Selemtype*)realloc(S.base, (STACKINCREMENT + STACK_INIT_SIZE) * sizeof(Selemtype));
		if (!S.base)
			return Error;
		S.top = S.base + S.stasksise;
		S.stasksise += STACKINCREMENT;
	}
	*S.top=e;
	S.top++;
	return OK;
}

 4:删除栈顶元素        status Pop(SqStack& S, Selemtype& e)

//删除栈顶元素
status Pop(SqStack& S, Selemtype& e) {
	if (S.top == S.base)
		return Error;
	e = *--S.top;
	return OK;
}

5:向栈中输入n个元素        status CreatStack(SqStack& S, int n)

//向栈中输入n个元素
status CreatStack(SqStack& S, int n) {
	while (S.top - S.base < S.stasksise&&n>0) {
		Selemtype e;
		scanf_s("%d", &e);
		*S.top++ = e;
		n--;
	}
	return OK;
}

6:输出栈中的元素        status PrintfStack(SqStack S)

//输出栈中的元素
status PrintfStack(SqStack S) {
	Selemtype* p;
	p = --S.top;
	while (p>=S.base) {
		printf("%d ", *p);
		 p--;
	}
	return OK;
}

 7:返回栈的长度        status StackLength(SqStack S)

//返回栈的长度
status StackLength(SqStack S) {
	int Length;
	Length = S.top - S.base;
	printf("%d", Length);
	return Length;
}

8:销毁栈        status DestroyStack(SqStack& S)

//销毁栈
status DestroyStack(SqStack& S) {
	S.top = S.base;
	return OK;
}

具体代码如下

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define Error 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Selemtype;
typedef int status;

//栈的顺序储存表示
typedef struct {
	Selemtype* top;
	Selemtype* base;
	int stasksise;
}SqStack;

//构造一个空栈
status InitStack(SqStack& S) {
	S.base = (Selemtype*)malloc(STACK_INIT_SIZE * sizeof(Selemtype));
	if (!S.base)
		return Error;
	S.top = S.base;
	S.stasksise = STACK_INIT_SIZE;
	return OK;
}

//向栈中输入n个元素
status CreatStack(SqStack& S, int n) {
	while (S.top - S.base < S.stasksise && n>0) {
		Selemtype e;
		scanf_s("%d", &e);
		*S.top++ = e;
		n--;
	}
	return OK;
}

//若栈不空,用元素返回栈的栈顶元素
status Getelem(SqStack S, Selemtype& e) {
	if (S.top == S.base)
		return Error;
	e = *(S.top - 1);
	return OK;
}

//插入新的栈顶元素
status Push(SqStack& S, Selemtype e) {
	if (S.top - S.base >= S.stasksise)
	{
		S.base = (Selemtype*)realloc(S.base, (STACKINCREMENT + STACK_INIT_SIZE) * sizeof(Selemtype));
		if (!S.base)
			return Error;
		S.top = S.base + S.stasksise;
		S.stasksise += STACKINCREMENT;
	}
	*S.top=e;
	S.top++;
	return OK;
}

//删除栈顶元素
status Pop(SqStack& S, Selemtype& e) {
	if (S.top == S.base)
		return Error;
	e = *--S.top;
	return OK;
}

//输出栈中的元素
status PrintfStack(SqStack S) {
	Selemtype* p;
	p = --S.top;
	while (p>=S.base) {
		printf("%d ", *p);
		 p--;
	}
	return OK;
}

//返回栈的长度
status StackLength(SqStack S) {
	int Length;
	Length = S.top - S.base;
	printf("%d", Length);
	return Length;
}

//销毁栈
status DestroyStack(SqStack& S) {
	S.top = S.base;
	return OK;
}

//主函数
int main() {
	SqStack S;
	InitStack(S);
	int n,e;

	printf("输入栈的长度\n");
	scanf_s("%d", &n);

	printf("输入栈中的元素\n");
	CreatStack(S, n);

	printf("删除栈顶元素\n");
	Pop(S, e);
	printf("删除的栈顶元素为%d\n",e);

	printf("插入栈顶元素3\n");
	Push(S, 3);

	printf("返回栈的长度\n");
	StackLength(S);

	printf("返回栈的元素");
	PrintfStack(S);

	printf("销毁栈\n");
	DestroyStack(S);

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值