리스트(List)
리스트 추상적 데이터형(Abstract Data Type)
리스트 adt를 설계하려면 리스트의 정의,프로퍼티,리스트가 수행하는 동작, 리스트에 의해 수행되는 동작 등을 알아야 한다.
리스트는 순서가 있는 일련의 데이터 집단이다. 리스트에 저장된 각 데이터 항목을 요소(element)라 부른다. 자바스크립트에서 모든 데이터형이 리스트의 요소가 될수 있다.
listSize | 프로퍼티 | 리스트의 요소 수 |
---|---|---|
pos | 프로퍼티 | 현재위치 |
length | 프로퍼티 | 리스트의 요소 수 반환 |
clear | 함수 | 리스트의 모든 요소 삭제 |
toString | 함수 | 리스트를 문자열로 표현해 반환 |
getElement | 함수 | 현재 위치의 요소를 반환 |
insert | 함수 | 기존 요소 위로 새요소를 추가 |
append | 함수 | 새요소를 리스트의 끝에 추가 |
remove | 함수 | 리스트의 요소 삭제 |
front | 함수 | 현재 위치를 리스트 첫번째 요소로 설정 |
end | 함수 | 현재 위치를 리스트 마지막 요소로 설정 |
prev | 함수 | 현재 위치를 한 요소 뒤로 이동 |
next | 함수 | 현재 위치를 한 요소 앞으로 이동 |
currPos | 함수 | 리스트의 현재 위치 반환 |
moveTo | 함수 | 현재 위치를 지정된 위치로 이동 |
List 클래스 구현
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;
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;
}
Append : 리스트에 요소 추가
function append(element){
this.dataStore[this.listSize++] = element;
//처음에 넣을때는 0 넣을때마다 this.listSize가 1씩 증가.
}
// prototype으로 한다면?
List.prototype.append = function(element){
this.dataStore[this.listSize++] = element;
}
//이런경우는 prototype을 쓰는게 좋을까 자체 속성에 추가하는게 좋을까..?
Find : 리스트의 요소검색
function find(element){
for(var i = 0; i < this.dataStore.length; i++){
if(this.dataStore[i] == element){
//인덱스에 해당하는 요소가 파라미터와 같다면 인덱스를 반환한다.
return i;
}
}
return -1;
}
Remove : 리스트 요소 삭제
function remove(element){
var foundAt = this.find(element);
//element를 검색해 인덱스값을 foundAt에 저장
if(foundAt > -1){
this.dataStore.splice(foundAt,1);
this.listSize--;
return true;
}
return false;
}
Length : 리스트의 요소 개수
function length(){
return this.listSize;
}
toString : 리스트의 요소 확인
function toString(){
return this.dataStore;
}
Insert : 리스트에 원하는 요소 뒤에 요소를 삽입
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;
}
Clear() : 리스트의 모든 요소 삭제
function clear(){
delete this.dataStore;
//this.dataStore.length = 0; -> 책에 이렇게 쓰여있는데 undefined인 배열에 length를 어케..?
this.dataStore = [];
this.listSize = this.pos = 0;
}
Contains : 리스트에 특정값이 있는지 판단
function contains(element){
for(var i = 0; i < this.dataStore.length; ++i){
if(this.dataStore[i] == element){
return true;
}
}
return false;
}
리스트 탐색(front, end, prev, next, currPos, moveTo, getElement)
function front(){
this.pos = 0;
}
function end(){
this.pos = this.listSize-1;
}
function prev(){
if(this.pos > 0){
this.pos--;
}
}
function next(){
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];
}
리스트와 반복
for(names.front(); names.currPos() < names.length(); names.next()){
console.log(names.getElement());
}
for(names.end(); names.currPos() >= 0; names.prev()){
console.log(names.getElement());
}
텍스트 파일 읽기
const fs = require('fs');
var movies, movieList;
function createArr(file){
movies = fs.readFileSync(file, 'utf8').split("\n");
for(var i = 0; i < movies.length; i++){
movies[i] = movies[i].trim();
}
}
createArr("film.txt");
리스트로 상점 관리하기
- movies 배열의 내용을 리스트로 저장.
movieList = new List();
for(var i = 0; i < movies.length; i++){
movieList.append(movies[i]);
}
- 영화목록 출력
function displayList(list){
for(list.frond(); list.currPos() < list.length(); list.next()){
if(list.getElement() instanceof Customer){
console.log(list.getElement()["name"] + "," + list.getElement()["movie"]);
}
else{
console.log(list.getElement());
}
}
}
var customers = new List();
function Customer(name, movie){
this.name = name;
this.movie = movie;
}
function checkOut(name, movie, filmList, customerList){
if(movieList.contains(movie)){
var c = new Customer(name, movie);
customerList.append(c);
filmList.remove(movie);
}
else{
//목록에 없는경우
console.log(movie + " is not available.");
}
}
- 실행
const fs = require('fs');
var movies,
movieList = customer = new List();
function createArr(file){
movies = fs.readFileSync(file, 'utf8').split("\n");
for(var i = 0; i < movies.length; i++){
movies[i] = movies[i].trim();
console.log(movies[i]);
}
}
createArr("film.txt");
checkOut("minwoo", "apple", movieList, customer);
결과코드
const fs = require('fs');
var movieList = new List(),
customer = new List();
function List(){
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
this.find = find;
this.remove = remove;
this.append = append;
this.length = length;
this.toString = toString;
this.clear = clear;
this.insert = insert;
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);
//element를 검색해 인덱스값을 foundAt에 저장
if(foundAt > -1){
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 = [];
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 displayList(list){
for(list.frond(); 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 Customer(name, movie){
this.name = name;
this.movie = movie;
}
function checkOut(name, movie, filmList, customerList){
if(filmList.contains(movie)){
var c = new Customer(name, movie);
customerList.append(c);
filmList.remove(movie);
console.log(name+"님이 "+movie+"를 대여함.")
console.log(filmList.dataStore);
console.log(customerList);
}
else{
//목록에 없는경우
console.log(movie + " is not available.");
}
}
function createArr(file){
var movies = fs.readFileSync(file, 'utf8').split("\n");
for(var i = 0; i < movies.length; i++){
movieList.append(movies[i].trim());
console.log(movies[i]);
}
}
createArr("film.txt");
checkOut("minwoo", "apple", movieList, customer);