[자료구조] 05 실습 및 과제

안우진·2024년 4월 10일

자료구조

목록 보기
8/12

[MainJava]

import java.util.ArrayList;

public class MainJava {

	public static void main(String[] args) {
		Practice practice = new Practice("I go to school");
		practice.practice();
		
		InputManager inputManager = new InputManager();
		ArrayList<String> inputs = inputManager.getFileContents("input.txt");
		for (String item : inputs) {
			int[] input = inputManager.getInt(item);
			Assignment assignment = new Assignment(input);
			assignment.assignment();
		}
	}
}

[InputManager]

import java.io.*;
import java.util.ArrayList;

public class InputManager {
	
	public int[] getInt(String content) {
		String[] temp = content.split(" ");
		int[] res = new int[temp.length];
		for (int i=0; i<temp.length; i++) {
			res[i] = Integer.parseInt(temp[i]);
		}
		return res;
	}
	
	public ArrayList<String> getFileContents(String path) {
		ArrayList<String> res = new ArrayList<String>();
		try {
			File file = new File(path);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line;
			while ((line = br.readLine()) != null) {
				res.add(line);
			}
			br.close();
			fr.close();
			return res;
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		}
		return null;
	}
}

[ArrayStack]

import java.util.EmptyStackException;

public class ArrayStack<E> {
	
	private E s[];
	private int top;

	public ArrayStack(int n) {
		s= (E[]) new Object[n];
		top = -1;
	}
	
	public void push(E item) {
		if (size() == s.length) return;
		s[++top] = item;
	}
	
	public E pop() {
		if (isEmpty()) throw new EmptyStackException();
		E item = s[top];
		s[top--] = null;
		return item;
	}
	
	public int size() {
		return top + 1;
	}
	
	public boolean isEmpty() {
		return top == -1;
	}
	
	public E peek() {
		if (isEmpty()) throw new EmptyStackException();
		return s[top];
	}
}

[Practice]

public class Practice {
	
	private final String input;
	
	public Practice(String input) {
		this.input = input;
	}
	
	public void practice() {
		ArrayStack<Character> stack = new ArrayStack<>(input.length());
		
		for(int i=0; i<input.length(); i++) {
			char c = input.charAt(i);
			if (c == ' ') {
				while (!stack.isEmpty()) {
					System.out.print(stack.pop());
				}
				System.out.print(" ");
				continue;
			}
			stack.push(c);
		}
		while (!stack.isEmpty()) {
			System.out.print(stack.pop());
		}
		System.out.println();
	}
}

[Assignment]

public class Assignment {
	private final int[] inputs;
	
	public Assignment(int[] inputs) {
		this.inputs = inputs;
	}
	
	public void assignment() {
		ArrayStack<Integer> A = new ArrayStack<>(50);
		ArrayStack<Integer> B = new ArrayStack<>(50);
		
		for (int e : inputs) {
			if (A.isEmpty()) {
				A.push(e);
				continue;
			}
			if (A.peek() >= e) {
				A.push(e);
			} else {
				int size = A.size();
				while (!A.isEmpty()) {
					B.push(A.pop());
				}
				boolean inserted = false;
				while (size-->0) {
					int pop = B.pop();
					if (!inserted && pop <= e) {
						inserted = true;
						A.push(e);
					}
					A.push(pop);
				}
			}
		}
		
		while (!A.isEmpty()) {
			B.push(A.pop());
		}
		
		while(!B.isEmpty()) {
			System.out.print(B.pop() + " ");
		}
		System.out.println();
	}
}

0개의 댓글