堆栈顺序存储C++实现

时间:2019-12-07
本文章向大家介绍堆栈顺序存储C++实现,主要包括堆栈顺序存储C++实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
// ConsoleApplication14.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"stdlib.h"
#include"iostream"
using namespace std;

#define MAX  100
typedef int data;

typedef struct
{
    int top;
    data a[MAX];
}st;

void init(st *p);
void push(st *p, data x);
void pop(st *p);
void dis(st *p);

int _tmain(int argc, _TCHAR* argv[])
{
    st ss, *p;
    p = &ss;

    init(p);

    for (int i = 0; i < 10; i++)
        push(p, rand());
    
    dis(p);

    cout << "*******" << endl;

    for (int i = 0; i < 10; i++)
        pop(p);

    dis(p);

    return 0;
}

void init(st *p)
{
    p->top = 0;
}

void push(st *p, data x)
{
    if (p->top >= MAX)
        cout << "It is full!" << endl;
    else
    {
        p->a[p->top] = x;
        p->top++;
    }
}

void pop(st *p)
{
    if (p->top == 0)
        cout << "It is empty!" << endl;
    else
        p->top--;
}

void dis(st *p)
{
    for (int i = 0; i < p->top; i++)
        cout << p->a[i] << endl;

}

周六下午晒太阳,喝茶,撸代码

原文地址:https://www.cnblogs.com/butchert/p/12001461.html