0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

キュー,スタックについて勉強した+使ってみた

0
Last updated at Posted at 2026-03-31

キュー...待ち行列、FIFO
FIFO(First-In-First-Out)...先入先出
スタック...LIFO
LIFO(First-In-First-Out)...先入れ後出し
プッシュ...スタックにデータを入れる
ポップ...スタックからデータを取りだす

キュー、スタックを利用する

キューは待ち行列という名前の通り、順番待ちなどの管理を容易に行うことができる

java.utilはキューとスタックのクラスがある。
それを利用した以下の行列管理システムでは

package queue;

import java.util.ArrayDeque;
import java.util.Queue;

public class 予約端末 {
	private int 番号 = 0;
	private Queue<お客様> 待ち行列 = new ArrayDeque<> ();
	public void 登録(String 名前,int 人数) {
		待ち行列.add(new お客様(番号,名前,人数));
		番号++;
	}
	public void 呼び出し() {
		System.out.println(待ち行列.poll().getname());
	}
}
class お客様 {
	private int 番号;
	private String 名前;
	private int 人数;
	
	public String getname() {
		return this.名前;
	}
	
	お客様 (int お客様番号,String お客様名,int お客様人数){
		this.番号 = お客様番号;
		this.名前 = お客様名;
		this.人数 = お客様人数;
	}
}
package queue;

public class main {
	public static 予約端末 予約一号機 = new 予約端末();
	public static void main(String args[]) {		
		try {
			予約一号機.登録("佐藤",2);
			予約一号機.登録("田中",5);
			予約一号機.登録("鈴木",1);
			
			for(int n = 0;n <3;n++) {
				予約一号機.呼び出し();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

出力

佐藤
田中
鈴木

上記のように、非常にシンプルな実装でFIFOを行うことができる。

スタックは、キャンセル機能や巻き戻し機能などの処理を実装できる

package stack;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

public class stage{
	private int[] coordinate = new int[2];
	
	public int[] get() {
		return this.coordinate;
	}
	public void setstage(int x,int y){
		this.coordinate[0] = x;
		this.coordinate[1] = y;
	}
	stage(int x,int y){
		this.coordinate[0] = x;
		this.coordinate[1] = y;
	}
}

class character {
	private int[] coordinate = new int[2];
	private Stack<int[]> history = new Stack<> ();
	
	public void move(int[] move,int[] stage) {
		history.push(coordinate.clone());
		this.coordinate[0] += move[0];
		this.coordinate[1] += move[1];
		
		if(coordinate[0] > stage[0]) {
			coordinate[0] = stage[0];
		}else if(coordinate[0] < 0){
			coordinate[0] = 0;
		}
		if(coordinate[1] > stage[1]) {
			coordinate[1] = stage[1];
		}else if(coordinate[1] < 0) {
			coordinate[1] = 0;
		}
	}
	private void set(int[] coordinate) {
		this.coordinate = coordinate;
	}
	public int[] get() {
		return this.coordinate;
	}
	public void rtn() {
		set(history.pop());
	}
	
	character(int x,int y){
		this.coordinate[0] = x;
		this.coordinate[1] = y;
	}
}
package stack;

public class mapping {
	static void mapping(int chara[], int[] stage) {
		for(int y = 0;y <= stage[1] ;y++) {
			for(int x = 0;x <= stage[0];x++) {
				if(x==chara[0] & y==chara[1]) {
					System.out.print("o");
				}else {
					System.out.print("x");
				}
			}
			System.out.println("");
		}
		System.out.println("");
	};
}
package stack;

public class main {
	public static stage stage = new stage(3,3);
	public static character chara = new character(0,0);
	
	public static void main(String args[]) {
		System.out.println("move");
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {1,1},stage.get());
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {2,2},stage.get());
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {1,1},stage.get());
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {-3,-3},stage.get());
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {1,1},stage.get());
		mapping.mapping(chara.get(),stage.get());
		
		System.out.println("rtn");
		chara.rtn();
		mapping.mapping(chara.get(),stage.get());
		chara.rtn();
		mapping.mapping(chara.get(),stage.get());
		chara.rtn();
		mapping.mapping(chara.get(),stage.get());
		
		System.out.println("move");
		chara.move(new int[] {-2,-2},stage.get());
		mapping.mapping(chara.get(),stage.get());
		chara.move(new int[] {1,1},stage.get());
		mapping.mapping(chara.get(),stage.get());
		
		System.out.println("rtn");
		chara.rtn();
		mapping.mapping(chara.get(),stage.get());
		chara.rtn();
		mapping.mapping(chara.get(),stage.get());
	}
}

出力

move
oxxx
xxxx
xxxx
xxxx

xxxx
xoxx
xxxx
xxxx

xxxx
xxxx
xxxx
xxxo

xxxx
xxxx
xxxx
xxxo

oxxx
xxxx
xxxx
xxxx

xxxx
xoxx
xxxx
xxxx

rtn
oxxx
xxxx
xxxx
xxxx

xxxx
xxxx
xxxx
xxxo

xxxx
xxxx
xxxx
xxxo

move
xxxx
xoxx
xxxx
xxxx

xxxx
xxxx
xxox
xxxx

rtn
xxxx
xoxx
xxxx
xxxx

xxxx
xxxx
xxxx
xxxo

巻き戻しができていることを確認できる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?