SDUT-3335_ data structure experiment stack and queue eight: basic operation of Stack

Source: Internet
Author: User

Data structure experiment stack and queue eight: basic operation of Stack

Time limit:1000 Ms Memory limit:65536 KiB

Problem Description

A stack is a basic data structure. The stack has two basic modes of operation, push and pop. Push a value pushes it to the top of the Stack, and POPs pops the value of the top of the stack. Now let's verify the use of the stack.

Input

First enter the integer t (1 <= T <= 10), which represents the number of groups tested, and later the T-group input.
For each set of test data, the first line enters two positive integers m (1 <= m <= 100), N (1 <= n <= 1000), where m represents the maximum length of the current stack, and n represents the number of operands to be entered below for this group test. The next n lines, the first character of each line may be ' P ' or ' O ' or ' a ', if ' P ', followed by an integer, indicating that the data is pressed onto the stack, and if ' O ', the stack top element is out of the stack, and if it is ' a ', the value of the current stack top is queried.

Output

For each set of test data, the stack is processed according to the command character;
(1) For all the ' P ' operation, if the stack full output ' F ', otherwise complete the press stack operation;
(2) for all the ' A ' operation, if the stack is empty, then output ' E ', otherwise output the value of the top of the stack;
(3) for all the ' O ' operation, if the stack is empty, then output ' E ', otherwise the value of the top element of the output stack, and let it out of the stack;
Each output occupies one row, and after each set of test data (except for 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

Sample Output

E
9
8
F
8

3
5

Hint

Recommendation: Read the operation characters in string mode (%s).

Stack of several basic operations, there is nothing to say, look at the code is good, note that the input because a single character easy to input errors, so it is recommended to use a string to read in.
Note the output.

#include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct node{int data; struct node *next;}    node;typedef struct stack{Node *base,*top; int Len;} stack;struct num{int data,next;} S[100050];    Node *newnode ()//build nodes {*t;    T = (node *) malloc (sizeof (node));    T->next = NULL; return t;};    Stack *newstack ()//build new stack {stack *t;    t = (stack *) malloc (sizeof (stack));    T->top = NewNode ();    T->base = t->top;    T->len = 0; return t;}    void push (Stack *t,int x)//Inbound {Node *p = NewNode ();    P->data = x;    P->next = t->top->next;    T->top->next = p;    T->base = p; t->len++;} int top (stack *t)//ask stack top element {return t->top->next->data;}    void pop (stack *t)//out stack {Node *p;    p = t->top->next;    T->top->next = t->top->next->next;    Free (p); t->len--;}    int empty (stack *t)//asks if the stack is empty {if (t->top->next==null) return 1; return 0;} void del (Stack *t)//Emptystack {while (!empty (t)) pop (t);}    int main () {int t,i,m,n,x;    Char s[10];    Stack *q;    Q = Newstack ();    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        while (!empty (q)) pop (q);            for (i=0;i<m;i++) {scanf ("%s", s);                if (s[0]== ' A ') {if (empty (q)) printf ("e\n");            else printf ("%d\n", Top (q));                } else if (s[0]== ' P ') {scanf ("%d", &x);                if (q->len==n) printf ("f\n");            else push (Q,X);                } else if (s[0]== ' O ') {if (empty (q)) printf ("e\n");                    else {printf ("%d\n", Top (q));                Pop (q);    }}} if (t!=0) printf ("\ n"); } return 0;}

SDUT-3335_ data structure experiment stack and queue eight: basic operation of the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.