Chinaunix首页 | 论坛 | 博客
  • 博客访问: 926944
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: C/C++

2010-03-17 00:27:53

/*用数组对堆栈的操作*/
#include
#define maxsize 10
int stack[maxsize];
int top=0;

void push(int data)
{
    int i;
    if(top>=maxsize)
        printf("\nThe stack is full!\n");
    else
    {
        stack[top]=data;
        printf("\nThe stack data is:");
        for(i=top;i>=0;i--)
            printf("[%d]",stack[i]);
        top++;
        printf("\n");
     }
}

int take()
{
    int temp;
    int i;
    if(top<=0)
        printf("\nThe stack is empty!\n");
     else
    {
        top--;
        temp=stack[top];
        printf("\nThe pop data is [%d]\n",temp);
        printf("\nThe stack data is");
        for(i=top;i>=0;i--)
            printf("[%d]",stack[i]);

        printf("\n");
    }
}

void main()
{
    int select;
    int stack[5];
    int i,data;
    do
    {
        printf("\n(1) Input a stack data");
        printf("\n(2) Output a stack data");
        printf("\n(3) Exit");
        printf("\nPlease select one:");
        scanf("%d",&select);
        switch(select)
        {
            case 1:printf("\nPlease input the data:");
                   scanf("%d",&data);
                   push(data);
                   break;
            case 2:take();
                   break;
            case 3:break;
        }
    }
    while(select<3);
    getch();
}
 
阅读(810) | 评论(0) | 转发(0) |
0

上一篇:C队列线性存储

下一篇:C堆栈的链式存储

给主人留下些什么吧!~~