计算C中堆栈中的元素数

问题描述

我尝试使用for循环来获取总数。它的工作还不错,但这只是在第一个输出中。在下面的代码中,如果您以选项4为指令,那么您将在第一个输出中得到正确的响应,但是如果再次单击选项4,它将保持递增。我的实际问题是如何存储循环的第一个输出

    #include<stdio.h>
    int top=-1,m=0;
    int A[200],k,n,a,i;
    void push(){
            if(top>=n-1)
            {
                 printf("\n\tSTACK is FULL!!\n");  
             }
            else
            {
                printf("Enter the value to be pushed:");
                scanf("%d",&a);
                top++;
                A[top]=a;
            }
    }
    void pop(){
        if(top<=-1)
        {
               printf("\n\t Stack is EMPTY!!\n");
        }
        else
        {
               printf("\n\t The popped elements is %d\n",A[top]);
               top--;
        }
    }

    int main()
    {
        printf("***********\n");
        printf("Set the size of your stack(max 200).\n");
        scanf("%d",&n);
        printf("You've set the size of the Stack to %d\n",n);
        printf("\n\t ........................\n");
        printf("\n\t 1- Push into the stack.\n");
        printf("\n\t 2- Pop the stack.\n");
        printf("\n\t 3- display the elements in the stack.\n");
        printf("\n\t 4- Get the number of elements in the stack.\n");
        printf("\n\t 5- EXIT.\n");
        printf("\n\t ........................\n");
        printf("\n");
        do{
            printf("Enter your instruction: ");
            scanf("%d",&k);
            
            switch(k){
                case 1:
                    push();
                    printf("\n");
                    break;
                case 2:
                    pop();
                    printf("\n");
                    break;
                case 3:
                    if(top>=0){
                        printf("\n\t Your Stack \n");
                        for(i=top;i>=0;i--){
                            printf("\n\t %d\n",A[i]);
                        }
                        printf("\n");
                    }
                    else{
                        printf("Stack is EMPTY!!");
                    }
                    break;
                case 4:
                    for(i=0;i<=top;i++){
                        if(A[i] != top)
                        m++;
                    }
                    printf("%d",m);
                    printf("\n");
                    break;
                case 5:
                    printf("\n\t EXITTED...");
                    break;
                default:
                    printf("\n\t Not in the above instruction.\n\n");
            }
        }
        while(k!=5);
        return 0;
    }

解决方法

要显示给定时间的元素计数,只需显示top+1的值即可。

#include<stdio.h>
    int top=-1;
    int A[200],k,n,a,i;
    void push(){
            if(top>=n-1)
            {
                 printf("\n\tSTACK is FULL!!\n");  
             }
            else
            {
                printf("Enter the value to be pushed:");
                scanf("%d",&a);
                top++;
                A[top]=a;
            }
    }
    void pop(){
        if(top<=-1)
        {
               printf("\n\t Stack is EMPTY!!\n");
        }
        else
        {
               printf("\n\t The popped elements is %d\n",A[top]);
               top--;
        }
    }

    int main()
    {
        printf("***********\n");
        printf("Set the size of your stack(max 200).\n");
        scanf("%d",&n);
        printf("You've set the size of the Stack to %d\n",n);
        printf("\n\t ........................\n");
        printf("\n\t 1- Push into the stack.\n");
        printf("\n\t 2- Pop the stack.\n");
        printf("\n\t 3- Display the elements in the stack.\n");
        printf("\n\t 4- Get the number of elements in the stack.\n");
        printf("\n\t 5- EXIT.\n");
        printf("\n\t ........................\n");
        printf("\n");
        do{
            printf("Enter your instruction: ");
            scanf("%d",&k);
            
            switch(k){
                case 1:
                    push();
                    printf("\n");
                    break;
                case 2:
                    pop();
                    printf("\n");
                    break;
                case 3:
                    if(top>=0){
                        printf("\n\t Your Stack \n");
                        for(i=top;i>=0;i--){
                            printf("\n\t %d\n",A[i]);
                        }
                        printf("\n");
                    }
                    else{
                        printf("Stack is EMPTY!!");
                    }
                    break;
                case 4:
                    printf("%d",(top+1));
                    printf("\n");
                    break;
                case 5:
                    printf("\n\t EXITTED...");
                    break;
                default:
                    printf("\n\t Not in the above instruction.\n\n");
            }
        }
        while(k!=5);
        return 0;
    }

要修改您的代码,请执行以下操作:

for(i=0,m=0;i<=top;i++)
{
   if(A[i] != top)
      m++;
}