Legendary Data Structure

Source: Internet
Author: User

Legendary Data Structure Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ I have been studying at the university for one semester. Most people have a basic understanding of the major they have learned. Many of you have learned that a course will be held in your sophomore year called "Data Structure". Today we will give you a brief introduction to the simplest Data Structure: Stack. There are three basic stack operations: Push, pop, and top.
For example, give you a series: 1 2 3 4  
Push: Add a number to the stack, for example, Push 5. The number of columns is 1, 2, 4, and 5.
Pop: Delete the last number from the stack, for example, Pop. The number of columns is 1 2 3. (The number of columns is changed but not output. If the stack is empty, that is, the pop operation is not allowed, an error is output, but the next operation will continue ). 
Top: Find the number at the end of the stack, such as top, and you need to output 4. (If there is no number in the stack, that is, the top operation is not allowed, then empty is output ).
Then, you can see that, in fact, the stack is a data structure that comes out first (the more advanced the elements come out later). It is very simple, next we will test your learning performance. Input

The input contains multiple groups of test data.
The first behavior of each group of data is an integer T (1 <=t <= 1000), followed by the T action on the stack operation.

Output

If the operation is top, the final number is output. If there is no number in the stack, the "empty" is output (excluding quotation marks ).
If the operation is pop and the stack is empty, an error is output (excluding quotation marks ).
Add a line feed to the last test data of each group.

Sample Input
 
8 Push 1 Push 2 Push 3 push 4toppoptoppop3push 1 PopTop
Sample output
 
43 empty
Prompt source Qinchuan sample program
# Include <stdio. h> # include <stdlib. h> # include <string. h> int main () {char st [20]; int t, n, j = 0; int num [1, 1002]; while (~ Scanf ("% d", & T) {getchar (); j = 0; memset (Num, 0, sizeof (Num); While (t --) {scanf ("% s", St); If (strcmp (St, "push") = 0) {scanf ("% d", & N ); num [J ++] = N;} else if (strcmp (St, "TOP") = 0) {If (J! = 0) printf ("% d \ n", num [J-1]); else printf ("Empty \ n");} else if (strcmp (St, "pop ") = 0) {If (j = 0) printf ("error \ n"); else j -- ;}} printf ("\ n") ;}return 0 ;}

 

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.