Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289179
  • 博文数量: 148
  • 博客积分: 4365
  • 博客等级: 上校
  • 技术积分: 1566
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 21:38
文章分类
文章存档

2014年(2)

2013年(45)

2012年(18)

2011年(1)

2009年(54)

2008年(28)

我的朋友

分类: C/C++

2009-09-08 21:50:48


#define    MAXSIZE 20            /* */
#include    <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef struct {
int ary[MAXSIZE];
int top;
}stack;

void init(stack* stack_para){
    stack_para->top=0;

}


void push(stack *stack_para,int ele){
    if(stack_para->top>MAXSIZE)
    {
        cout<<"stack is full\n";
        exit(1);
    }
    stack_para->ary[stack_para->top]=ele;
    stack_para->top++;

}

int pop(stack * stack_para){
    stack_para->top--;
    int i=stack_para->ary[stack_para->top];
    return(i);
}

int visit(stack * stack_para){
    stack_para->top--;
    return(stack_para->ary[stack_para->top++]);
}

void traverse(stack * stack_para){
    
    for (int i=0 ;i< stack_para->top;i++)
    {
        cout<<" "<<stack_para->ary[i];
    }
    cout<<endl;
}

int
main ( int argc, char *argv[] )
{
    stack mystack;
    init( &mystack);
    for(int i=0;i<10;i++)
    {
        push(&mystack,i+1);
    }
    traverse(&mystack);
    for(int i=0;i<10;i++)
    {
        cout<<" "<<visit(&mystack);
    }

    return EXIT_SUCCESS;
}                /* ---------- end of function main ---------- */

阅读(376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~