JAVA Sequential stack operation-initialization, stack, stack, judge empty stack, judge stack full __java

Source: Internet
Author: User

Sequential stacks: Use a continuous set of memory to save the data in the stack, and define a top variable to hold the stack number.

The stack structure is the "LIFO" principle.

The most basic operation of the stack is two: push (into stack) + pop (out of Stack)


Import Java.util.Scanner; /** * @author Lilylee * @date April 25, 2017 * @time afternoon 4:25:15 * @Version 1.0 * @email Lilylee_1213@foxmai
		l.com * */public class Mystack {public static void main (string[] args) {stack_type st=new stack_type ();
		Stack_data data1=new stack_data (); Stack_type Stack =st. Stinit ();
		Initialize stack Scanner sc=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Input name Age end with Name= ' 0 '");
			Enter the stack element to end with 0 while (true) {Stack_data sdata=new stack_data ();
			Sdata.name=sc.next ();
			if (Sdata.name.equals ("0")) {break;}
			Sdata.age=sc.nextint (); St.
		Pushst (Stack, sdata);
		String flag= "1";
		System.out.println ("Pop stack End with 0")//Enter any non 0 elements to complete the stack flag=sc.next (); while (!) ( Flag.equals ("0"))) {data1=st.
			Popst (stack);
			if (data1==null) {break;}
			System.out.println (data1.name+ "" +data1.age);
		Flag=sc.next ();
		} System.out.println ("End and free"); St.
		
	Stfree (ST);
	Class stack_data{String name;
int age; } class Stack_type{static final int maxlen=50;//define Maximum length of stack stack_data[] data=new stack_data[maxlen+1];
	
	int top;
		
		Stack_type Stinit () {Stack_type p;
			if ((P=new stack_type ())!=null) {//application stack memory p.top=0;
			return p;
		
	return null;
		Boolean stisempty (Stack_type s) {//To determine whether the stack is an empty Boolean t;
		T= (s.top==0);
	return t;
		Boolean stisfull (Stack_type s) {//To determine whether the stack is full Boolean t;
		T= (S.top==maxlen);
	return t;
	} void Stclear (Stack_type s) {//empty stack s.top=0;
		} void Stfree (Stack_type s) {//release stack occupied space if (s!=null) {s=null;  } int Pushst (Stack_type s,stack_data Data) {if (s.top+1) >maxlen) {return 0;}
		Stack Overflow s.data[++s.top]=data;
	return 1; } stack_data Popst (Stack_type s) {if (s.top==0) {System.out.println ("no elements!"); return null;}//Stack empty, operation failed return
	(s.data[s.top--]);  } stack_data Peekst (Stack_type s) {if (s.top==0) {system.exit (0);}
	Stack is empty, operation failure return (S.data[s.top]);
 }
	
	
}


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.