C-Stack and Queue 8 of Data Structure Experiment: Basic Stack Operation

C-Data Structure Experiment: Stack and Queue 8: Basic Stack Operation
Description

The stack is a basic data structure. The stack has two basic operation modes, push and pop. Pushing a value will push it onto the top of the stack, and pop will pop the value at the top of the stack. Now we will verify the use of the stack.
Input

First enter the integer t (1 <= t <= 10), which represents the number of test groups, and then enter the t group.
For each set of test data, enter two positive integers m (1 <= m <= 100) and n (1 <= n <= 1000) in the first line, where m represents the maximum length of the current stack, and n represents this group of tests The operand to be entered below. In the next n lines, the first character of each line may be'P' or'O' or'A'; if it is'P', an integer will follow, which means that the data is pushed onto the stack; if it is' O'means popping the top element of the stack; if it is'A', it means asking for the value of the current top of the stack'.
Output

For each group of test data, the stack is processed according to the command characters therein;
(1) For all'P' operations, if the stack is full, output'F', otherwise the stack operation will be completed;
(2) For all'A ' operations , If the stack is empty, output'E', otherwise output the value of the top of the stack;
(3) For all'O' operations, if the stack is empty, output'E', otherwise output the value of the top element of the stack and let It pops out of the stack;
each output occupies one line, and after each group of test data (except the last group) is completed, a blank line is output.
Sample

Input

2
5 10
A
P 9
A
P 6
P 3
P 10
P 8
A
P 2
O
2 5
P 1
P 3
O
P 5
A
Output

E
9
8
F
8

3
5

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int date[52];
int main()
{
    
    int top=-1;
char a[10];
    int n,m,h=0,t,i,b;
    scanf("%d",&t);
    while(t--)
    {
    
    top=-1;h=0;
     scanf("%d%d",&m,&n);
     for(i=0;i<n;i++)
     {
    
    
         scanf("%s",a);
         if(a[0]=='P')
         {
    
    
             scanf("%d",&b);
             if(h<m)
             {
    
    date[++top]=b;h++;}
             else
                printf("F\n");
         }
         else if(a[0]=='O')
         {
    
    if(top>-1)
             {
    
    printf("%d\n",date[top]);
                 top--;
             h--;}
             else
                printf("E\n");
         }
         else if(a[0]=='A')
         {
    
    if(top==-1)
         printf("E\n");
         else
             printf("%d\n",date[top]);
         }
     }
     printf("\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/a675891/article/details/103946440
Recommended