• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java模拟数据结构中栈的进队出队

OC/C/C++ 水墨上仙 1343次浏览

Java模拟数据结构中栈的进队出队


package com.stackANDqueue;

import java.io.DataInputStream;
import java.io.IOException;

/*
 * 这是一个栈的模拟
 * 模拟的是入栈和出栈
 */
public class Stack {
	static int MAX = 10;
	static String[] item = new String[10];
	static int top;
	
	Stack(){
		top = -1;
	}
	
	/**
	 * 入栈函数
	 */
	@SuppressWarnings("deprecation")
	public static void push_f(){
		DataInputStream in = new DataInputStream(System.in);
		if(top >= MAX-1) System.out.print("\n   Stack is full ! \n"); //当栈已满,显示错误!
		else{
			top ++;
			System.out.print("\n  Please Enter item to insert ! \n");
			System.out.flush();
			try {
				item[top] = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("");
	}
	
	/**
	 * 出栈函数
	 */
	public static void pop_f(){
		if(top < 0) System.out.print("\n  No item , stack is empty ! \n");//当栈没有数据存在,显示错误!
		else{
			System.out.print("\n  Item "+ item[top] + "  delete !  \n");
			top--;
		}
		System.out.println("");
	}
	
	/**
	 * 输出函数
	 */
	public static void list_f(){
		int count=0, i=0;
		if(top < 0) System.out.print("\n  No item , stack is empty ! \n");
		else{
			System.out.print("\n\n ITEM\n");
			System.out.print("-------------------\n");
			for(i=top;i>=0;i--){
				System.out.print(" ");
				System.out.println(item[i]);
				count++;
			}
			System.out.print("-------------------\n");
			System.out.print("Total item :" + count +"\n\n");
		}
		System.out.println("");
	}
	
	
	@SuppressWarnings({ "deprecation", "unused" })
	public static void main(String args[]){
		DataInputStream in = new DataInputStream(System.in);
		String op = "";
		int option = 0;
		Stack obj = new Stack();
		do{
			System.out.println("****************** Stack Program *******************");
			System.out.println("                                                    ");
			System.out.println("                 <1> Insert Node                    ");
			System.out.println("                 <2> Delete Node                    ");
			System.out.println("                 <3> List  Node                     ");
			System.out.println("                 <4> Exit !                         ");
			System.out.println("                                                    ");
			System.out.println("****************************************************");
			System.out.print("\n         Choice :  ");
			System.out.flush();
			op = "";
			try {
				op = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			option = 0;
			try{
				option = Integer.valueOf(op).intValue();
			}catch(NumberFormatException e){
				System.out.println("\n Please input (1,2,3,4).....");
				System.out.println("\n\n\n");
			}
			switch(option){
				case 1:
					push_f();
					break;
				case 2:
					pop_f();
					break;
				case 3:
					list_f();
					break;
				case 4:
					System.exit(0);
			}
		}while(true);
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java模拟数据结构中栈的进队出队
喜欢 (0)
加载中……