[자료구조] 2주차 LinkedList

안우진·2024년 3월 22일

자료구조

목록 보기
1/12

MainJava.java

public class MainJava {

	public static void main(String[] args) {
		InputManager inputManager = new InputManager();
		int[] inputs = inputManager.getContents("input.txt");
		Assignment assignment = new Assignment(inputs);
		assignment.assignment();
	}
}

InputManager.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class InputManager {
	
	public int[] getContents(String path) {
		String[] contents = getFileContents(path).split(" ");
		int[] inputs = new int[contents.length];
		for (int i=0; i<contents.length; i++) {
			inputs[i] = Integer.parseInt(contents[i]);
		}
		return inputs;
	}
	
	private String getFileContents(String path) {
		try {
			File file = new File(path);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = br.readLine();
			br.close();
			return line;
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		}
		return null;
	}
}

Assignment.java

import java.util.NoSuchElementException;

class DNode<E> {
	private E item;
	private DNode prev, next;
	
	public DNode(E item, DNode prev, DNode next) {
		this.item = item;
		this.prev = prev;
		this.next = next;
	}
	
	public E getItem() {
		return item;
	}
	public DNode getPrev() {
		return prev;
	}
	public DNode getNext() {
		return next;
	}
	public void setItem(E item) {
		this.item = item;
	}
	public void setPrev(DNode prev) {
		this.prev = prev;
	}
	public void setNext(DNode next) {
		this.next = next;
	}
}

class DList<E> {
	private DNode head, tail;
	private int size;
	
	public void add(E item) {
		DNode newNode = new DNode(item, null, null);
		if (handleEmpty(newNode)) return;
		tail.setNext(newNode);
		newNode.setPrev(tail);
		tail = newNode;
		size++;
	}
	
	public void addFirst(E item) {
		DNode newNode = new DNode(item, null, null);
		if (handleEmpty(newNode)) return;
		newNode.setNext(head);
		head.setPrev(newNode);
		head = newNode;
		size++;
	}
	
	public void remove(DNode target) {
		if (isEmpty()) {
			throw new NoSuchElementException();
		}
		target.getPrev().setNext(target.getNext());
		target.getNext().setPrev(target.getPrev());
		size--;
	}
	
	public void print() {
		DNode p = head;
		while(p != null) {
			System.out.print(p.getItem().toString() + " ");
			p = p.getNext();
		}
	}
	
	private boolean handleEmpty(DNode node) {
		if (isEmpty()) {
			head = node;
			tail = node;
			size = 1;
			return true;
		}
		return false;
	}
	
	public boolean isEmpty() {
		return head == null;
	}
}

public class Assignment {
	private final int[] inputs;
	
	public Assignment(int[] inputs) {
		this.inputs = inputs;
	}
	
	public void assignment() {
		DList<Integer> dList = new DList();
		for (int item : inputs) {
			if (item%2 == 0) {
				dList.addFirst(item);
			} else {
				dList.add(item);
			}
		}
		dList.print();
	}
}

input.txt

1 2 3 4 5 6

output

6 4 2 1 3 5

0개의 댓글