堆栈——数组实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime> 

using namespace std;

using ElemType = int;
const int MAXSIZE = 20;

// 堆栈结构
class Stack {
public:
	ElemType data[MAXSIZE];
	int top;
};

// 初始化堆栈
void initStack(Stack &stack, int n)
{
	stack.top = -1;
	srand(time(NULL));
	while (n--) {
		stack.top++;
		stack.data[stack.top] = rand()%100 + 1;
	}
}

// 判空
bool isEmpty(const Stack &stack)
{
	if (stack.top == -1)
		return true;
	return false;
}

// 压栈
void push(Stack &stack, ElemType val)
{
	if (stack.top == MAXSIZE - 1) {
		cout << "stack is full...\n";
		return;
	}
	stack.top++;
	stack.data[stack.top] = val;
}

// 出栈
void pop(Stack &stack)
{
	if (isEmpty(stack)) {
		cout << "stack is empty...\n";
		return;
	}
	stack.top--;
}

// 返回栈顶元素
const ElemType& getTop(const Stack &stack)
{
	if (isEmpty(stack)) {
		cout << "stack is empty...\n";
	}
	return stack.data[stack.top];
}

// 打印
void print(const Stack &stack)
{
	if (isEmpty(stack)) {
		cout << "Empty stack...\n";
	}
	int n = stack.top;
	cout << "从栈顶到栈底的元素依次为:";
	while (n != -1) {
		cout << stack.data[n--] << " ";
	}
	cout << endl;
}

int main()
{
	Stack stack;
	initStack(stack, 10);
	print(stack);
	pop(stack);
	print(stack);
	push(stack, 100);
	print(stack);
	cout << "栈顶元素为:" << getTop(stack) << endl;
}

  

原文地址:https://www.cnblogs.com/xzxl/p/8643119.html

时间: 2024-08-10 18:46:13

堆栈——数组实现的相关文章

堆栈数组实现

//堆栈,数组实现#include<iostream>using namespace std;#define Maxsize 100class stack{public: int data[Maxsize]; int top = -1; //保存栈顶下标值};int pop(stack*ptrl); //出栈void push(stack*ptrl,int x);//进栈int main(){ stack l; stack*ptrl = &l; int i,m; for (i = 0;

堆栈-数组实现

#include <stdio.h> #include <malloc.h> #define DataType int #define MAX 1024 typedef struct { DataType data[MAX]; int top; }stack, *pstack; pstack init_stack() { pstack ps; ps=(pstack)malloc(sizeof(stack)); if(!ps) { printf("Error. fail m

4-7 在一个数组中实现两个堆栈

本题要求在一个数组中实现两个堆栈. 函数接口定义: Stack CreateStack( int MaxSize ); bool Push( Stack S, ElementType X, int Tag ); ElementType Pop( Stack S, int Tag ); 其中Tag是堆栈编号,取1或2:MaxSize堆栈数组的规模:Stack结构定义如下: typedef int Position; struct SNode { ElementType *Data; Positio

6-7 在一个数组中实现两个堆栈

6-7 在一个数组中实现两个堆栈(20 分) 本题要求在一个数组中实现两个堆栈. 函数接口定义: Stack CreateStack( int MaxSize ); bool Push( Stack S, ElementType X, int Tag ); ElementType Pop( Stack S, int Tag ); 其中Tag是堆栈编号,取1或2:MaxSize堆栈数组的规模:Stack结构定义如下: typedef int Position; struct SNode { Ele

使用线程执行堆栈StackTraceElement设计Android日志模块

假设你想在你的Android自己主动打印程序MainActivity.onCreate(line:37)这样的类名.方法名称(行)登录如何实现? 1.介绍Java线程执行堆栈 Java.lang包中提供了StackTraceElement,能够用来获取方法的调用栈信息. 通过调用线程函数Thread.currentThread().getStackTrace()能够获得StackTraceElement[]的堆栈数组.数组中保存了线程中的运行调用的方法.观察以下的代码: @Override pr

堆栈的三种实现方式

传统的堆栈操作只有 入栈push 和 出栈pop 两种,没有单独的访问栈顶元素的操作,访问栈顶元素的唯一方式就是出栈(pop会把堆栈顶部的值移出堆栈并返回这个值).这样的pop存在副作用. 所以,我们在这里实现提供push.pop.top三种基本操作的堆栈. 实现堆栈这一抽象数据类型(ADT),即要实现:入栈(push).出栈(pop).访问栈顶元素(top)的操作,另外加上两个判断 栈满.栈空与否的函数. 通常,我们可以使用 静态数组.动态数组.动态链表 这三种方式来实现堆栈. 首先,在sta

java开始学习,数据类型和堆栈

一篇简单的文章作为自己转型的开始,以后由嵌入式开发转向安卓app开发 javaee企业版 javase 标准板,做桌面应用程序 javame小型版,针对小产品,比如手机(这个已经不用了,现在用的是安卓) java输出 System.out.print(a); print只是输出'字符'和"字符串"和 变量 print('a'+1) 变为输出 98 'a'的ASCII为97.print('你'+1):因为汉字也是存放表中 java不同数据类型运算注意事项 java数据类型分为byte 1

数据分析(1) Numpy数组

Numpy的数组类被称作ndarray,通常被称作数组. 特点:元素类型相同,数量事先给定. 1 创建方式 (1)array( ):接收一个普通的Python序列(元组与列表并无区别),并将其转换为ndarray: # 使用列表创建 list1 = [1, 2, 3, 4, 5] print('使用一维列表创建:\n', np.array(list1)) list2 = [[1, 2, 3], [4, 5, 6]] print('使用二维列表创建:\n', np.array(list2)) #

栈的应用-判断括号匹配

栈的一个典型应用就是可以用来协助分析表达式的括号是否匹配.括号可以延伸到任何成对出现的界定符,例如引号,书名号等. 接下来给出程序实现: 第一部分给出的是堆栈的插入,删除等操作对应的实现: 1 public class StackChar 2 { 3 private int maxSize;//堆栈数组大小 4 private char [] stackArray; 5 private int top;//堆栈顶 6 public StackChar(int maxSize) 7 { 8 thi