자료구조 & 알고리즘 관련/자료구조&알고리즘

https://www.acmicpc.net/problem/2346 처음 보자마자, 이건 양방향 연결 리스트를 사용하면 되겠구나 라는 생각이 들었다. 코드import java.io.*;import java.util.StringTokenizer;class Balloon { public int index; public int value; public Balloon prev; public Balloon next; public Balloon(int index, int value) { this.index = index; this.value = value; }}public class Main { public static void main(String[] ..
Queue 2개로 Stack구현하기 import java.util.LinkedList; import java.util.Queue; public class Stack { private Queue q1; private Queue q2; public Stack() { this.q1 = new LinkedList(); this.q2 = new LinkedList(); } public void push(T value) { q1.add(value); } public T pop() { while (q1.size() > 1) { q2.add(q1.poll()); } Queue temp = q1; q1 = q2; q2 = temp; return q2.poll(); } @Override public String toStr..
Stack 두개를 이용하여 Queue구현하기 자바 import java.util.Stack; class Queue { Stack inStack; Stack outStack; public Queue() { this.inStack = new Stack(); this.outStack = new Stack(); } public void enqueue(T value) { inStack.add(value); } public T dequeue() { if (!inStack.isEmpty()) { while(!inStack.isEmpty()) { outStack.add(inStack.pop()); } } return outStack.pop(); } @Override public String toString() { re..
두둥.. 다시 코테를 준비하기로 마음 먹었다.! 그래서 다시 초심으로 돌아가서 공부일지를 쓰려고 하고, 그 시작은 자료구조란? 뭘까?? 데이터를 저장하고 관리하는 방식 컴퓨터 구조 관점에서 생각해보기로 하자! 데이터를 저장하는 곳은 어디일까? 더보기 메모리(Memory) HDD, RAM이 대표적이라고 볼 수 있다. 코딩을 막 짜고 스프링 부트에서 Application을 실행시킬때의 코드이당. -> 요즘 하고 있는 사이드 플젝 public class BoardBackApplication { public static void main(String[] args) { SpringApplication.run(BoardBackApplication.class, args); } } 저장버튼을 누르면 코드들이 하드디스크..
이전에 만든 노드 클래스는 취약하다. 헤더가 링크드 리스트의 대표이면서 첫번째 값이기도 하다. 이 첫번째 값이 삭제될 경우 문제가 발생하게 된다. class LinkedList{ Node header = null; static class Node{ int data; Node next = null; } public LinkedList() { this.header = new Node(); } void append(int d){ Node end = new Node(); end.data = d; Node n = this.header; while(n.next != null){ n = n.next; } n.next = end; } void delete(int d){ Node n = this.header; while..
// 단방향 링크드 리스트 구현 class Node{ private final int data; // 현재 데이터 private Node next = null; // 다음 데이터 public Node(int d){ this.data = d; } // 뒤에 더해주면 된다. void append(int d){ Node end = new Node(d); // 마지막으로 들어갈 노드를 생성해준다. Node node = this; // 현재 노드 while(node.next != null){ // 노드의 다음 인자가 있다면 다음으로 계속 찾아준다. node = node.next; // 다음 노드로 넘어가는 부분 } node.next = end; // 다음 노드의 주소값을 end로 설정 } // 노드의 next 값..
솜사탕코튼
'자료구조 & 알고리즘 관련/자료구조&알고리즘' 카테고리의 글 목록