Last active
February 13, 2017 15:10
-
-
Save momoci99/93679ba241dc90f954d0c201c7870fd0 to your computer and use it in GitHub Desktop.
자바스크립트 + node.js 와 함께하는 자료구조와 알고리즘 - 리스트 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
/* | |
이 소스코드는 node.js 런타임 환경에 맞게 작성되었습니다. | |
node.js 런타임환경에서 실행시켜주세요 | |
Data structure : List | |
*/ | |
//node.js에서 파일을 읽기위한 모듈 참조 | |
fs = require('fs'); | |
//List 클래스 구현 | |
//List Class implements | |
function List() { | |
this.listSize = 0; | |
this.pos = 0; | |
this.dataStore = []; | |
this.clear = clear; | |
this.find = find; | |
this.toString = toString; | |
this.insert = insert; | |
this.append = append; | |
this.remove = remove; | |
//반복자를 위한 함수 | |
//function for iterator | |
this.front = front; | |
this.end = end; | |
this.prev = prev; | |
this.next = next; | |
this.length = length; | |
this.currPos = currPos; | |
this.moveTo = moveTo; | |
this.getElement = getElement; | |
this.length = length; | |
this.contains = contains; | |
} | |
function append(element) { | |
this.dataStore[this.listSize++] = element; | |
} | |
function find(element) { | |
for (var i = 0; i < this.dataStore.length; i++) { | |
if (this.dataStore[i] == element) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
function remove(element) { | |
var foundAt = this.find(element); | |
if (foundAt > -1) { | |
// splice 메서드 설명 | |
// https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice | |
this.dataStore.splice(foundAt, 1); | |
this.listSize--; | |
return true; | |
} | |
return false; | |
} | |
function length() { | |
return this.listSize; | |
} | |
function toString() { | |
return this.dataStore; | |
} | |
function insert(element, after) { | |
var insertPos = this.find(after); | |
if (insertPos > -1) { | |
this.dataStore.splice(insertPos + 1, 0, element); | |
++this.listSize; | |
return true; | |
} | |
return false; | |
} | |
function clear() { | |
delete this.dataStore; | |
this.dataStore.length = 0; | |
this.listSize = this.pos = 0; | |
} | |
function contains(element) { | |
for (var i = 0; i < this.dataStore.length; i++) { | |
if (this.dataStore[i] == element) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function front() { | |
this.pos = 0; | |
} | |
function end() { | |
this.pos = this.listSize - 1; | |
} | |
function prev() { | |
if (this.pos > 0) { | |
this.pos--; | |
} | |
} | |
function next() { | |
//'<' 이면 pos는 항상 listsize보다 1 적게된다. | |
// if '<' then pos always smaller than listsize | |
if (this.pos <= this.listSize - 1) { | |
this.pos++; | |
} | |
} | |
function currPos() { | |
return this.pos; | |
} | |
function moveTo(position) { | |
this.pos = position; | |
} | |
function getElement() { | |
return this.dataStore[this.pos]; | |
} | |
//Customer 클래스 구현 | |
//Customer Class implements | |
function Customer(name, movie) { | |
this.name = name; | |
this.movie = movie; | |
} | |
function createArr(file) { | |
//동기적으로 파일 읽기. 순차적인 실행환경을 보장해야 정상 작동한다. | |
//sequential execution environment must be guaranteed to logic normally. | |
var data = fs.readFileSync(file, 'utf8'); | |
var arr = data.split("\n"); | |
for (var i = 0; i < arr.length; i++) { | |
arr[i] = arr[i].trim(); | |
} | |
return arr; | |
} | |
function displayList(list) { | |
for (list.front(); list.currPos() < list.length(); list.next()) { | |
if (list.getElement() instanceof Customer) { | |
console.log(list.getElement()["name"] + "," + list.getElement()["movie"]); | |
} | |
else { | |
console.log(list.getElement()); | |
} | |
} | |
} | |
//대여처리하는 함수 | |
function checkOut(name, movie, filmList, customerList) { | |
//대여가능한 목록에 빌리고자하는 영화가 있다면 | |
if (movieList.contains(movie)) { | |
//새로운 Customer 객체를 생성한다. | |
var c = new Customer(name, movie); | |
//고객목록(List)에 Customer 객체를 추가한다. | |
customerList.append(c); | |
//대여가능한 목록에서 대여된 영화를 제거한다. | |
filmList.remove(movie); | |
} | |
else { | |
console.log(movie + " is not available"); | |
} | |
} | |
var movies = createArr("films.txt"); | |
var movieList = new List(); | |
var customers = new List(); | |
for (var i = 0; i < movies.length; i++) { | |
movieList.append(movies[i]); | |
} | |
console.log("Available movies: \n"); | |
displayList(movieList); | |
console.log("\nEnter your name: "); | |
//Node.js는 'putstr' 메서드가 존재하지 않는다. | |
//Node.js Doesn't exsit 'putstr' method | |
//putstr 대체 | |
//replace 'putstr' | |
var name = "Jane Doe"; | |
console.log(name); | |
console.log("What movie would you like? "); | |
var movie = "The Godfather"; | |
console.log(movie); | |
checkOut(name, movie, movieList, customers); | |
console.log("\nCustomer Rentals: \n"); | |
displayList(customers); | |
console.log("\nMovies Now Available\n"); | |
displayList(movieList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment