Topic 1108: Use of stacks

Source: Internet
Author: User

Title 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:

For each set of test data, the first line is a positive integer n,0<n<=10000 (n=0 end). 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 into the stack, if it is ' O ', the value of the top of the stack pops out, if there is no element in the stack, ignore the operation; A ', which indicates the value at the top of the current stack, and outputs ' E ' if the stack is empty at the time. The stack starts empty.

Output:

For each set of test data, the stack is processed according to the command character, and for all ' A ' operations, the value at the top of the stack is output, each occupying a row, and the output ' E ' if the stack is empty at the time. When each set of test data finishes, output a blank line.

Sample input:
3AP 5a4p 3P 6O A0
Sample output:
E53
Summarize:
  1. When using scanner to get an input stream, first use the nextint of in (its variable) to get an integer, and enter the end instead of the space end, the next time in will get an empty string, rather than want, so add a line in.nextline () to remove the empty line
  2. It is easy to compare strings for equality by using the equals of string instead of using = = directly.
1 ImportJava.util.Scanner;2 ImportJava.util.Stack;3  4  Public classmain{5      Public Static voidMain (string[] args) {6Scanner in=NewScanner (system.in);7         intN;8n=in.nextint ();9          while(n!=0){TenStack<integer> st=NewStack (); OneString line=in.nextline (); A string[] t; -   -              for(inti=0;i<n;i++){ theLine=in.nextline (); -T=line.split (""); -                 if(T[0].equals ("P")){ -St.push (Integer.parseint (t[1])); +                 } -                 Else if(T[0].equals ("O")){ +                     if(!st.isempty ()) A St.pop (); at                 } -                 Else if(T[0].equals ("A")){ -                     if(St.isempty ()) -System.out.println ("E"); -                     Else - System.out.println (St.peek ()); in                 } -             } to System.out.println (); +n=in.nextint (); -         } the     } * } $ /**************************************************************Panax Notoginseng problem:1108 - user:0000h the Language:java + result:accepted A time:1570 Ms the memory:111344 KB + ****************************************************************/

Topic 1108: Use of stacks

Related Article

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.