วันจันทร์ที่ 29 กุมภาพันธ์ พ.ศ. 2559

JavaScript DataStructure & Algorithms : List ADT


ช่วงนี้กระแส Javascript ของปี 2016 มาแรงมากมาย คงจะมีหลายคนที่อยากจะฝึกเขียน Javascript แต่เริ่มต้นไม่ถูก จะฝึกเขียนโปรแกรม ก็ไม่รู้จะเริ่มฝึกจากเขียนอะไร ไม่รู้จะตั้งโจทย์อะไร หรือเขียนไปแล้วไม่รู้จะเอาไปใช้ประโยชน์อะไร นึกภาพไม่ออก

เอางี้ครับ คือการที่เราจะเขียนโปรแกรมภาษาใดภาษาหนึ่งได้อย่างลึกซึ้ง เราต้องรู้จัก Data Structure ของแต่ล่ะภาษากันก่อน และเรียนรู้การเขียน Algorithm ในภาษานั้นๆด้วย เพราะแต่ล่ะภาษาจะมีลักษณะเฉพาะของมันเอง ซึ่งเราควรจะเขียนให้คล่อง โดยใช้ Algoritm นี่แหละครับเป็นโจทย์ของเรา

วันนี้ฝากตัวอย่าง Class List ไว้ก่อน (มันเป็น Class แบบสมมตินะครับ ของจริงต้อง ECMAScript6)  เดี๋ยวจะมาอธิบายอีกทีว่ามันคืออะไร ทำงานยังไง
(ไม่ได้เขียนเรื่อง Javascript ไว้แต่แรก บังเอิญจะทำเป็นข้อมูลเก็บเอาไว้พอดี ไหนๆก็ไหนๆ เอามาลง Blog ซ่ะเลย ฮ่าๆๆ)

/*
ADT list
listSize(property)      Number of element in list
pos(property)           Current position in list
length(property)        Returns the number of elements in list
clear(function)         Clears all elements from list
toString(function)      Returns string representation of list
getElement(function)    Returns element at current position
insert(function)        Inserts new element after existing element
append(function)        Adds new element to end of list
remove(function)        Removes element from list
front(function)         Sets current position to first element of list
end(function)           Sets current position to last element of list
prev(function)          Moves current position back one element
next(function)          Moves current position forward one element
currPos(function)       Returns the current position in list
moveTo(function)        Moves the current position to specified position
*/
// A List Class Implementation
var List = function(){
  this.listSize = 0;
  this.pos = 0;
  this.dataStore = []; // initializes an empty array to store list elements
};
List.prototype.clear = function(){
  delete this.dataStore;
  this.dataStore = [];
  this.listSize = this.pos = 0;
};
List.prototype.find = function(element){
  for (var i = 0; i < this.dataStore.length; ++i){
    if(this.dataStore[i] == element){
      return i;
    }
  }
  return -1;
};
List.prototype.toString = function(){
  return this.dataStore;
};
List.prototype.insert = function(element,after){
  var insertPos = this.find(after);
  if(insertPos > -1){
    this.dataStore.splice(insertPos+1,0,element);
    ++this.listSize;
    return true;
  }
  return false;
};
List.prototype.append = function(element){
  // After the element is appended , listSize is incremented by 1.
  this.dataStore[this.listSize++] = element;
};
List.prototype.remove = function(element){
  var foundAt = this.find(element);
  if(foundAt > -1){
    this.dataStore.splice(foundAt,1);
    --this.listSize;
    return true;
  }
  return false;
};
List.prototype.length = function(){
  return this.listSize;
};
List.prototype.contains = function(element){
  for (var i = 0; i < this.dataStore.length; ++i){
    if(this.dataStore[i] == element){
      return true;
    }
  }
  return false;
};
// Traversing a List
List.prototype.front = function(){
  this.pos = 0;
};
List.prototype.end = function(){
  this.pos = this.listSize - 1;
};
List.prototype.prev = function(){
  if (this.pos > 0){
    --this.pos;
  }
};
List.prototype.next = function(){
  if (this.pos < this.listSize-1){
    return ++this.pos;
  }
};
List.prototype.currPos = function(){
  return this.pos;
};
List.prototype.moveTo = function(position){
  this.pos = position;
};
List.prototype.getElement = function(){
  return this.dataStore[this.pos];
};
var names = new List();
names.append("Cynthia");
names.append("Raymond");
names.append("Barbara");
console.log(names.toString());
names.remove("Raymond");
console.log(names.toString());
names.front();
console.log(names.getElement());
names.clear();
console.log(names.toString());

ไม่มีความคิดเห็น:

แสดงความคิดเห็น