码迷,mamicode.com
首页 > 其他好文 > 详细

练习2014092501

时间:2014-09-25 14:40:29      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   数据   div   

/********************************************************************
@file     Main_practise.cpp
@date     2014-9-25
@author   Tiger
@brief    数据结构
@details  堆栈
********************************************************************/
#include <iostream>
#define STACK_SIZE 5

typedef int ElementType;
typedef struct
{
    ElementType m_Data[STACK_SIZE];
    int         m_nTop;
}STACK, *PSTACK;

void initStack(PSTACK pMyStack)
{
    pMyStack->m_nTop = -1;
}

bool isEmpty(PSTACK pMyStack)
{
    return pMyStack->m_nTop == -1;
}

bool isFull(PSTACK pMyStack)
{
    return pMyStack->m_nTop == STACK_SIZE-1;
}

bool push(PSTACK pMyStack, ElementType elem)
{
    if (isFull(pMyStack)) return false;
    pMyStack->m_Data[++pMyStack->m_nTop] = elem;
    return true;
}

bool pop(PSTACK pMyStack, ElementType& elem)
{
    if (isEmpty(pMyStack)) return false;
    elem = pMyStack->m_Data[pMyStack->m_nTop--];
    return true;
}

bool getTop(PSTACK pMyStack, ElementType& elem)
{
    if (isEmpty(pMyStack)) return false;
    elem = pMyStack->m_Data[pMyStack->m_nTop];
    return true;
}

int main(int argc, const char* argv[])
{
    PSTACK pMyStack = new STACK;

    initStack(pMyStack);
    for (int i=0; i<STACK_SIZE; ++i)
    {
        push(pMyStack, i);
    }

    while (!isEmpty(pMyStack))
    {
        ElementType elem = 0;
        pop(pMyStack, elem);
        std::cout << elem << std::endl;
    }

    delete pMyStack;

    return 0;
}

 

练习2014092501

标签:style   blog   color   io   os   ar   for   数据   div   

原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3992579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!