Last active
January 30, 2021 04:22
-
-
Save skawnkk/204fe065b75c82494e6c37400065f3a6 to your computer and use it in GitHub Desktop.
영상편집기 _ single_linked_list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ | |
class Node { | |
constructor(data, next = undefined) { | |
this.data = data; | |
this.next = next; | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head = undefined; | |
this.size = 0; | |
} | |
addLast(data) { | |
let node = new Node(data); | |
let current; | |
if (!this.head) { | |
this.head = node; | |
} else { | |
current = this.head; | |
while (current.next) { | |
current = current.next; | |
} | |
current.next = node; | |
} | |
this.size++; | |
} | |
insertion(data, index) { | |
//index가 범위를 벗어난 경우 | |
if (index > this.size) { | |
return; | |
} | |
//index가 0일 경우 | |
if (index === 0) { | |
let node = new Node(data); | |
if (!this.head) { | |
this.head = node; | |
this.size++ | |
return; | |
} else { | |
let current = this.head; | |
node.next = current; | |
this.head = node; | |
this.size++; | |
return; | |
} | |
} | |
//중간에 insert하는 경우 | |
const node = new Node(data); | |
let current, previous; | |
current = this.head; | |
while ((index--) !== 0) { //(let count; count++) | |
previous = current; | |
current = current.next; | |
} | |
node.next = current; | |
previous.next = node; | |
this.size++; | |
} | |
deletion_id(info) { | |
console.log(this); | |
let id = info[0]; | |
let current = this.head; | |
let previous; | |
while (current) { | |
if (current.data[0] != id) { | |
previous = current; | |
current = current.next; | |
} else { | |
if (this.size === 1) { | |
this.head = null; | |
return; | |
} | |
if (previous) { | |
previous.next = current.next; | |
} else { | |
this.head = current.next | |
} | |
this.size--; | |
return; | |
} | |
} | |
} | |
render() { | |
let current = this.head; | |
let totalPlayingTime = 0; | |
let count = 0; | |
while (current) { | |
count++ | |
totalPlayingTime += current.data[1]; | |
current = current.next; | |
} | |
console.log(`영상클립:${count}개`); | |
console.log(`전체길이:${totalPlayingTime}sec`); | |
} | |
printList() { | |
console.log('---영상클립---') | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
let current = this.head; | |
let count = 1; | |
while (current) { | |
let nodeId = ''; | |
for (let i = 0; i < 4; i++) { | |
let number = Math.floor(Math.random() * alphabet.length); | |
nodeId += alphabet.charAt(number); | |
} | |
console.log(`제목${count} (${nodeId}): ${current.data}`); | |
let listObject = { | |
'title': `제목${count}`, | |
'id': nodeId, | |
'data': current.data | |
} | |
listArr.push(listObject); | |
current = current.next; | |
count++; | |
} | |
return listArr; | |
} | |
printEdit() { | |
let print = "|---" | |
let current = this.head; | |
while (current) { | |
print += `[${current.data[0]}, ${current.data[1]}sec]---`; | |
current = current.next; | |
} | |
print += '---[end]'; | |
console.log(print); | |
} | |
} | |
//------------• 재생목록 •----------------- | |
let listArr = []; | |
const newList = new LinkedList(); | |
for (i = 1; i < 14; i++) { | |
let playTime = Math.ceil(Math.random() * 15); | |
newList.addLast(playTime); | |
} | |
newList.printList(); | |
//---------------• 편집 •----------------- | |
const video = new LinkedList(); | |
const readline = require("readline"); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
let input = []; | |
rl.on("line", function (line) { | |
input = line.split(' ') | |
const instruction = String(input[0]); | |
const editId = String(input[1]); | |
const editIndex = Number(input[2]); //insert에 필요함. | |
let data; | |
let info = []; | |
//!한번에 변수로 정의하는 법 | |
function infoCheck(editId) { | |
for (let el of listArr) { | |
if (el['id'] == editId) { | |
data = el["data"]; | |
info.push(editId, data); | |
} | |
} | |
return info; | |
}; | |
info = infoCheck(editId) | |
//!깔꼼하게쓰는법 | |
if (instruction === 'add') { | |
video.addLast(info); | |
} | |
if (instruction == 'insert') { | |
video.insertion(info, editIndex); | |
} | |
if (instruction == 'delete') { | |
video.deletion_id(info); | |
} | |
if (instruction == 'render') { | |
video.render(); | |
} | |
video.printEdit(); | |
if (instruction == 'quit') { | |
rl.close(); | |
} | |
}).on("close", function () { | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
delete 1번 인자 삭제 오류 수정
그외 깔끔한 표현 보강 필요