PS/Data Structure

โœจ Constructor & Basic Attribute private Node head; private Node tail; private int size; private class Node{ private int value; private Node next; private Node(int data) { this.value = data; this.next = null; } public String toString() { return String.valueOf(this.value); } } ์ œ์ผ ์•ž์— ์žˆ๋Š” ๋…ธ๋“œ, ๋’ค์— ์žˆ๋Š” ๋…ธ๋“œ, size๋ฅผ ์ •์˜ํ•œ๋‹ค. ๋˜ํ•œ ๋…ธ๋“œ๋ฅผ ๋‚ด๋ถ€ class๋กœ ์ƒ์„ฑํ•˜์—ฌ contructor๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. โœจ addFirst(int data) public vo..
๋“œ๋””์–ด ๋ธ”๋กœ๊ทธ์— ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๊ธ€์„ ์“ด ์ด์œ ,, LinkedList์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์—ฐ๊ฒฐ์ด ๋ฌด์—‡์ธ๊ฐ€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š”๊ฐ€? cpu: ๊ณ„์‚ฐํ•˜๊ณ  ์—ฐ์‚ฐ, ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ storage, memory, cpu ์ค‘์— ๊ฐ€์žฅ ๋น ๋ฆ„ storage: HDD, SSD ํŒŒ์ผ, ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์ €์žฅ, ๊ฐ€๊ฒฉ์ด ์ €๋ ด, ์šฉ๋Ÿ‰์ด ํฌ๊ณ , ๋น„ํœ˜๋ฐœ์„ฑ ๋ฉ”๋ชจ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ: ๊ธฐ์–ต์„ ๋‹ด๋‹น, ์šฉ๋Ÿ‰์ด ์•„์ฃผ ์ ์Œ, ํœ˜๋ฐœ์„ฑ ๋ฉ”๋ชจ๋ฆฌ, storage ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋น ๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ. cpu๊ฐ€ storage์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์“ฐ๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. cpu๊ฐ€ ๊ณ„์‚ฐ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” storage์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ memory๋กœ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ์— memory๊ฐ€ cpu์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š”๊ฒŒ ๋น ๋ฅด..
ArrayList๋ฅผ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ int ํ˜• ArrayList๋ฅผ ๋‚˜ํƒ€๋‚ด ๋ณด๊ณ ์ž ํ•œ๋‹ค. โœจ Constructor & Basic Attribute private int[] elementData; // ๊ฐ’ ๋„ฃ์„ ๊ณณ private int size; // arraylist size private int currentCapacity; // ํ˜„์žฌ arraylist ํฌ๊ธฐ private int initCapacity = 10; // ์ตœ์ดˆ arraylist ํฌ๊ธฐ //constructor public MyArrayList() { this.elementData = new int[initCapacity]; this.size = 0; this.currentCapacity = initCapacity; } ๊ฐ’ ๋„ฃ์„ eleme..
โœจ ArrayList ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ โœจ ArrayList Insert & Delete arrayList.add(10); arrayList.add(20); arrayList.add(30); arrayList.add(40); 20 ์ž๋ฆฌ์— 50์„ ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด, ๋งจ ๋’ค์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋’ค๋กœ ์ด๋™ํ•˜์—ฌ ์ž๋ฆฌ๋ฅผ ๋งŒ๋“  ํ›„ 50์˜ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. -> ์‚ญ์ œ๋„ ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€ โœจ ArrayList Get ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜๋ฅผ ์•Œ๊ณ  ์žˆ์Œ. index๋ฅผ ์ด์šฉํ•˜์—ฌ List์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค . โœจ ArrayList Pros and Cons [์žฅ์ ] ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ [๋‹จ์ ] ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋ฉด ํ•˜๋‚˜์”ฉ ๋‹ค ์˜ฎ๊ฒจ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด..
โœจ List vs Array ์ €์žฅ ๋‘˜ ๋‹ค, ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ๋˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๋ณตํ•˜์—ฌ ์ €์žฅ ๊ฐ€๋Šฅ Array๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ ์ €์žฅ ์œ„์น˜๊ฐ€ ์ค‘์š”ํ•จ. (index) ์ฃผ์†Œ๋ฅผ ๊ฐ–๊ณ  ๋ฐ”๋กœ ์ฐพ์•„๊ฐˆ ์ˆ˜ ์žˆ์Œ array[0] = 10; array[1] = 20; array[2] = 30; array[3] = 40; array์—์„œ index 3์— 50์˜ ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋˜๋ฉด 40 ์œ„์— ๋ฎ์–ด์”Œ๊ฒŒ ๋œ๋‹ค. array[0] = 10; array[1] = 20; array[2] = 30; array[3] = 50; List๋„ index๊ฐ€ ์ค‘์š”ํ•˜์ง€๋งŒ, element์˜ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์ธ์ง€๊ฐ€ ๋” ์ค‘์š”ํ•จ. ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•จ. list[0] = 10; list[1] = 20; list[2] = 30; list[3] = 40; ar..
ํ˜„์žฌ ํ•™๊ต์—์„œ ๋ฐ์ดํ„ฐ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์žˆ๋Š”๋ฐ, ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋ถ€๋ถ„์—์„œ ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ๋งŽ๋‹ค. MyArrayList๊นŒ์ง€๋Š” ๊ดœ์ฐฎ์•˜์ง€๋งŒ, LinkedListd(์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ)๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•˜๋‹ค. ๋”ฐ๋ผ์„œ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋ณ‘ํ–‰ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๊ฐœ๋…๊ณผ ์ฝ”๋“œ๋ฅผ ๋ธ”๋กœ๊ทธ์— ๋ณต๊ธฐํ•˜๋Š” ์‹์œผ๋กœ ๊ณต๋ถ€๋ฅผ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค. โœจ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ, ์›์†Œ๋“ค์ด ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ •์˜๋œ ๊ทœ์น™์— ์˜ํ•ด ๋‚˜์—ด๋˜๋ฉฐ ์ž๋ฃŒ์— ์˜ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž๋ฃŒ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ‘œํ˜„ํ•œ ๊ฒƒ - ํŠน์ง• 1. ํšจ์œจ์„ฑ ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ์˜ ๊ด€๋ฆฌ ๋ฐ ์‚ฌ์šฉ, ์ ์ ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉ ๊ฑฐ๋Œ€ํ•œ ๋ฐ์ดํ„ธ๋ฅด ํšจ๊ณผ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ ex) ์ฑ… ํ•œ ๊ถŒ์€ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, ์ฑ…์ด ๋ช‡ ๋ฐฑ๊ถŒ์ด ๋˜๋ฉด ๊ทธ๋ƒฅ ๋‘์–ด๋„ ๋˜์ง€๋งŒ ๊ณต๊ฐ„๋„ ..
chaerlo127
'PS/Data Structure' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก