Java Algorithm Learning (stack operation instance)

Source: Internet
Author: User

CompleteCodeAs follows:

Package COM. xu. main; import Java. util. role; public class p4_1 {/*** @ function: stack structure operation * @ Author: * @ Date: 2012-10-16 */public static void main (string [] ARGs) {stacktype ST = new stacktype (); data Data = new data (); stacktype stack = ST. stinit (); inputs input = new partition (system. in); system. out. println ("inbound stack operation:"); system. out. println ("input name age for Stack import:"); do {data d = new data (); D. name = input. next (); If (D. name. equals ("0") {Break; // If the input is 0, exit} else {d. age = input. nextint (); ST. pushst (stack, d) ;}} while (true); string temp = "1"; system. out. println ("out-of-stack operation: any non-zero key out-of-stack operation:"); temp = input. next (); While (! Temp. equals ("0") {DATA = ST. popst (stack); system. out. printf ("data from the stack is: (% s, % d) \ n", Data. name, Data. age); temp = input. next ();} system. out. println ("test ended! "); ST. stfree (ST) ;}} class data {string name; int age;} class stacktype {static final int maxlen = 50; data [] DATA = new data [maxlen + 1]; // data element int top; // stack top @ suppresswarnings ("UNUSED") stacktype stinit () // initialize stack {stacktype P; if (P = new stacktype ())! = NULL) {P. top = 0; return P;} return NULL;} Boolean stisempty (stacktype s) // judge whether the stack is empty {Boolean t; t = (S. top = 0); Return t;} Boolean stisfull (stacktype s) // judge whether the stack is full {Boolean t; t = (S. top = maxlen); Return t;} void stclear (stacktype s) // clear the stack {S. top = 0;} void stfree (stacktype s) // release the space occupied by the stack {If (s! = NULL) {S = NULL;} int pushst (stacktype S, data) // The inbound stack operation {If (S. top + 1)> maxlen) {system. out. println ("stack overflow! "); Return 0;} s. data [++ S. top] = data; return 1;} data popst (stacktype s) // The output stack operation {If (S. top = 0) {system. out. println ("Stack is empty! "); System. exit (0);} return (S. data [S. top --]);} data peekst (stacktype s) // read the top data of the stack {If (S. top = 0) {system. out. println ("Stack is empty! "); System. Exit (0);} return S. Data [S. Top];}

Execution result:

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.