顺序栈

本文详细介绍了顺序栈的基本概念,包括其数据结构定义、初始化、元素的插入与删除等核心操作,并通过具体代码实例展示了顺序栈的操作流程。


顺序栈:利用一组连续的存储单元依次存放自栈底到栈顶的数据元素;
由于栈顶元素是经常变动的,所以附设top指示栈顶元素在顺序表中
的位置,同时也需要知道顺序栈存储空间的起始位置,因此还需设定
一个base指针用来指示栈空间的起始位置。

一般约定top指针指向栈顶元素的下一个位置,即新数据元素将要插
入得位置。

 

顺序栈的类型定义:
 tyedef struct
 {
  SElemType *base;
  SElenType *top;
  int stacksize; //栈当前可使用的最大容量
 }SqStack;

初始化顺序表
int InitStack(SqStack &S)
{
 S.base = (SElemType *)malloc(StackInitSize*sizeof(SElemType));
 if (!S.base)
  exit(OVREFLOW);
 S.top = S.base;
 S.stacksize = StackIniSize;
 return OK;
}

判断栈是否为空栈
int StackEmpty(SqStack S)

 if (S.top == S.base)
  return TRUE;
 else
  return FALSE;
}

求顺序栈中的元素的个数
int StackLength(SqStack S)
{
 int i = 0;
 i = S.top - S.base;
 return i;
}

取顺序栈栈顶元素
int GetTop(SqStack S, SElemType &e)
{
 if (S.top == S.base)
  return ERROR;
 e = *(S.top - 1);
 return OK;
}

插入栈顶元素
int Push(SqStack &S, SElemType e)
{
 if (S.top-S.base >= S.stacksize)
 {
  S.base = (SElemeType *)ealloc(S.base,\
  (S.stacksize+StackIncrement)*sizeof(SElemType));
  if (!S.base)
   exit(OVERFLOW);
  S.top = S.base + S.stacksize;
  S.sacksize += StackIncrement;
 }
 *(S.top++) = e;
}

删除栈顶元素
int Pop(SqStack &S, SElemType &e)
{
 if (S.top == S.base)
  exit(UNDERFLOW);
 e = *--S.top;
 return OK;
}

将顺序栈清空
int ClearStack(SqStack &S)
{
 S.top = S.base; //注意是将top指针指向base指针
}

总结:顺序栈就是两个指针,base负责定位,top负责元素操作。

 

 

//实现顺序栈的简单操作
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define StackInitSize 10

typedef struct
{
	int *base;
	int *top;
	int stacksize;
}SqStack;

int InitStack(SqStack &S);
int StackEmpty(SqStack S);
int StackLength(SqStack S);
int GetTop(SqStack S, int &e);
int Push(SqStack &S, int e);
int Pop(SqStack &S, int &e);
int ClearStack(SqStack &S);

int main(void)
{
	SqStack *S = (SqStack*)malloc(sizeof(SqStack));
	int len, e;
	int i;
	InitStack(*S);
	len = StackLength(*S);
	for (i=0; i<StackInitSize+5; i++)
		Push(*S, i);
	GetTop(*S, e);
	printf("栈顶元素是:%d\n", e);
	for (i=0; i<StackInitSize; i++)
	{
		Pop(*S, e);
		printf("%d ", e);
	}
	printf("\n");
	ClearStack(*S);
	return 0;
}

int InitStack(SqStack &S)
{
	S.base = (int *)malloc(StackInitSize*sizeof(int));
	if (S.base == NULL)
		exit(1);
	S.top = S.base;
	S.stacksize = StackInitSize;
	return 1;
}

int StackEmpty(SqStack S)
{
	if (S.top = S.base)
		return 1;
	else
		return 0;
}

int StackLength(SqStack S)
{
	int len;
	len = S.top - S.base;
	return len;
}

int GetTop(SqStack S, int &e)
{
	if (S.base == S.top)
	{
		printf("栈中无元素\n");
		exit(2);
	}
	e = *(S.top - 1);
	return 0;
}

int Push(SqStack &S, int e)
{
	if (S.top - S.base >= S.stacksize)
	{
		S.base = (int *)realloc(S.base, \
			(S.stacksize+StackInitSize)*sizeof(int));
		if (S.base == NULL)
		{
			exit(3);
		}
		S.top = S.base + S.stacksize;
		S.stacksize += StackInitSize;
	}
	*(S.top++) = e;
	return 0;
}

int Pop(SqStack &S, int &e)
{
	if (S.top == S.base)
		exit(4);
	e = *--S.top;
	return 0;
}

int ClearStack(SqStack &S)
{
	S.top = S.base;
	return 0;
}

转载于:https://www.cnblogs.com/zm001/archive/2012/11/28/2875490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值