整数堆栈程序

时间:2015-01-19 10:30:38

标签: c integer stack

有人可以给我这个程序 整数堆栈

使用数组实现一堆整数。

假设堆栈的最大大小为50个元素。任何增加此大小的尝试都会导致屏幕上出现错误,但程序不会终止。类似地,POP操作会打印错误,但程序不会终止。使用此堆栈模拟连续的命令流入。

输入描述:第一个输入是单个数字N,表示测试用例的数量。新行中的下一个输入是P,表示给定的命令数。然后有完全P行,每行代表一个命令。有三种类型的命令:

推送POP TOP

Push命令后面必须跟一个整数(a),在push和整数之间有一个空格。 POP和TOP没有以下整数。

输出:对于每个测试用例,打印正好是P行。每次PUSH操作后,打印推入的整数或错误“STACK OVERFLOW”。 每次POP操作后,打印出弹出的整数或错误“STACK UNDERFLOW”。每个TOP命令打印堆栈的顶部元素或错误“STACK UNDERFLOW”。

#include<stdio.h>
#define MAX_SIZE 50
int ar[MAX_SIZE];
int top = -1 ;
void push(int n)
{
if(top>=50)
{
printf("STACK OVERFLOW\n");
}
else
ar[++top] = n ;
}
void pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
}
else
printf("%d\n",ar[top--]);
}

void Top()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
}
else
printf("%d\n",top);
}
int main()
{
int t ,tt, n , i , j , k ;
scanf("%d",&t);
while(t--)
{
scanf("%d",&tt);
while(tt--)
{
char s[100];
scanf("%s",&s);
if(strcmp(s,"PUSH")==0)
{
scanf("%d",&n);
push(n);
}
else if(strcmp(s,"POP")==0)
{
pop();
}
else if(strcmp(s,"TOP")==0)
{
Top();
}
}
top = -1 ;
}
return 0 ;
}

当我输入

2
3
PUSH 1
PUSH 2
TOP
4
PUSH 1
POP
POP
TOP

我的预期问题输出是

1
2
2
1
1
STACK UNDERFLOW 
STACK UNDERFLOW

但我的代码却向我显示了这个输出

1
1
STACK UNDERFLOW
STACK UNDERFLOW

这是问题请帮忙!!

2 个答案:

答案 0 :(得分:0)

你可以像这样增强你的编程:

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 50
int ar[MAX_SIZE];
int top = -1 ;

void push(int n)
{
    if(top>=50)
    {
        printf("STACK OVERFLOW\n");
    }
    else
    {
        top++;
        ar[top] = n ;
    }
}

void pop()
{
    if(top==-1)
    {
    printf("STACK UNDERFLOW\n");
    }
    else
    {
    printf("%d\n",ar[top]);
    top--;
    }
}

void Top()
{
    if(top==-1)
    {
    printf("STACK UNDERFLOW\n");
    }
    else
    {
    printf("%d\n",top);
    }
}

int display ( void )
{
    int choice;
    if ( top == -1 )
    {
        printf ( "\n Enter 1 for PUSH \n ");
        printf ( "\n Enter 3 for TOP value \n ");
        printf ( "\n Enter 0 to EXIT \n ");
        scanf ( "%d",&choice );
        if ( choice == 0 )
        {
            printf ( "\n Exited \n");
            exit ( 0 );
        }
    }
    else
    {
        printf ( "\n  Enter 1 for PUSH \n ");
        printf ( "\n Enter 2 for POP value \n ");
        printf ( "\n Enter 3 for TOP value \n ");
        printf ( "\n Enter 0 to EXIT \n ");
        scanf ( "%d",&choice );
        if ( choice == 0 )
        {
            printf ( "\n Exited \n");
            exit ( 0 );
        }
    }
    return ( choice );
}
int main()
{
    int choice, value;
    while ( 1 )
    {

    choice = display ( );

    switch ( choice )
    {
        case 1:
        printf ( "\n enter the value \n ");
        scanf ( "%d", &value );
        push ( value );
        break;

        case 2:
        pop ();
        break;

        case 3:
        Top ();
        break;

        default:
        printf ( "\n Wrong choice \n ");
        break;
    }

    }

    return 0 ;
}

答案 1 :(得分:0)

要修复的示例。

#include <stdio.h>
#include <string.h> //for strcmp

#define MAX_SIZE 50

int ar[MAX_SIZE];
int top = -1 ;

void push(int n){
    if(top>=MAX_SIZE-1){//49 is full
        printf("STACK OVERFLOW\n");
    }else{
        ar[++top] = n ;
        printf("%d\n", n);//display push value
    }
}
void pop(void){
    if(top==-1){
        printf("STACK UNDERFLOW\n");
    } else{
        printf("%d\n",ar[top--]);
    }
}
void Top(void){
    if(top==-1){
        printf("STACK UNDERFLOW\n");
    } else {
        printf("%d\n",ar[top]);//display element
    }
}
int main(void){
    int t ,tt, n;// , i , j , k ;<-unused

    scanf("%d",&t);
    while(t--){
        scanf("%d",&tt);
        while(tt--){
            char s[100];
            scanf("%99s", s);//&s --> s, 99 is safety for buffer overflow
            if(strcmp(s,"PUSH")==0){
                scanf("%d",&n);
                push(n);
            } else if(strcmp(s,"POP")==0){
                pop();
            } else if(strcmp(s,"TOP")==0){
                Top();
            }
        }
        top = -1;//reset
    }
    return 0 ;
} 
相关问题