JavaScript data structure and algorithm - detailed list (below), implement a list application based on Nodejs

1. Review of the previous article:

In the last article, we implemented a list class, added some properties, and implemented many methods. In this article, we will use lists to implement a book borrowing query system with you. You need to use the list class List() written in JavaScript data structure and algorithm - List Detailed Explanation (above ).

If you want to view the code description of the List() class, please refer to the previous explanation of my list. Here only the source code of List is given for the next writing call:

function List() {
        this.listSize = 0
        this.pos = 0
        this.dataStore = [] // initialize an empty array to hold list elements
        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.hasNext = hasNext
        this.hasPrev = hasPrev
        this.length = length
        this.currPos = currPos
        this.moveTo = moveTo
        this.getElement = getElement
        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) {
            this.dataStore.splice(foundAt, 1)
            this.listSize--
            return true
        }
        return false
    }
    function length() {
        return this.listSize
    }
    function toString() {
        return this.dataStore.toString()
    }
    function insert(element, after) {
        var insertPos = this.find(after)
        if (insertPos > -1) {
            this.dataStore.splice(insertPos + 1, 0, element)
            this.liseSize++
            return true
        }
        return false
    }
    function clear() {
        delete this.dataStore
        this.dataStore.length = 0
        this.listSize = 0
        this.pos = 0
    }
    function contains(element) {
        for (var i = 0; i < this.dataStore.length; i++) {
            return true
        }
        return false
    }
    
    // loop through the list
    function front() {
        this.pos = 0
    }
    function end() {
        this.pos = this.listSize - 1
    }
    function prev() {
        this.pos--
    }
    function next() {
        if (this.pos < this.listSize) {
            this.pos++
        }
    }
    function currPos() {
        return this.pos
    }
    function moveTo(position) {
        this.pos = position
    }
    function hasNext() {
        return this.pos < this.listSize
    }
    function hasPrev() {
        return this.pos >= 0
    }
    function getElement() {
        return this.dataStore[this.pos]
    }

2. Implement a book borrowing query system based on Nodejs using lists

Create a new List folder, which contains a book.txt (for storing books) and a book.js for writing js (please load the List() related code implemented in the previous article first).

2.1 Reading book files

// Created by xiaoqiang on 05/04/2018.
var fs = require('fs')
function readBooks('book.txt') {
   var data = fs.readFileSync(file)
   var dataArray = data.toString (). split ('\ n')
   for (var i = 0; i < dataArray.length; i++) {
	dataArray [i] = dataArray [i] .trim ()
      }
   return dataArray
}

During the writing process, console.log(readBooks('book.txt')) can be added outside the function to facilitate testing, enter the current folder, use the command node book.js , and see the following results to prove that the node environment is ok && function writing question:


2.2 Save books to list

As we said at the beginning, we wrote a List() class in the previous article. Next, we create a book list object through this class, and add elements to the dataStore property in the list object.

function saveBooksToList(file) {
  var booksArray = readBooks (file)
  for (var i = 0; i < booksArray.length; i++) {
      bookList.append(booksArray[i])
   }
}

2.3 Display list information

function displayList(list) {
   for (list.front(); list.currPos() < list.length(); list.next()) {
      console.log(list.getElement())
   }
}

test:

var bookList = new List()
 saveBooksToList('book.txt')
 displayList(bookList)

operation result:


2.4 Create a borrower method

This method is used to save which books were borrowed by which person.

// borrower information
function borrower(name, book) {
   this.name = name
   his.book = book
}

2.5 Implement the borrowing method

// borrow book
function borrowBook(name, book, bookList, borrowerList) {
   if (bookList.contains(book)) {
	var b = new borrower(name, book)
	borrowerList.append(b)
        bookList.remove(book)
	console.log(name + 'borrowing book' + book + 'success!!!')
   } else {
        console.log('The book has been borrowed!!!')
    }
}

Explanation: The meanings of the four parameters are: the name of the borrower, the book to be borrowed, the list of books, and the list of borrowers; first check whether the book exists, if it exists, save the book and the borrower information to the borrower list, if the information does not exist, prompt error message.

2.6 Final result test

// book list object
 var bookList = new List()
 // borrower list object
 var borrowerList = new List()
 // save book to list
 saveBooksToList('book.txt')
 console.log('------------Currently available books-------------------')
 // display book information
 displayList(bookList)
 console.log('-----------------Start borrowing books-------------------')
 // borrow book
 borrowBook('xgq', 'Nodejs详解', bookList, borrowerList)
 console.log('-----------------borrowing information-------------------')
 // Display the list of borrowers
 displayList(borrowerList)
 console.log('-----------------Remaining books that can be borrowed---------------')
// List of books left after borrowing
 displayList(bookList)

result:

2.7 The complete code of book.js (integration of the previous part):

// Created by xiaoqiang on 05/04/2018.
 var fs = require('fs')
 var readline = require('readline')
// JavaScript data structure and algorithm - detailed list (on)
 function List() {
	 this.listSize = 0
	 this.pos = 0
	 this.dataStore = [] // initialize an empty array to hold list elements
	 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.hasNext = hasNext
	 this.hasPrev = hasPrev
	 this.length = length
	 this.currPos = currPos
	 this.moveTo = moveTo
	 this.getElement = getElement
	 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) {
		 this.dataStore.splice(foundAt, 1)
		 this.listSize--
		 return true
	 }
	 return false
 }
 function length() {
	 return this.listSize
 }
 function toString() {
	 return this.dataStore.toString()
 }
 function insert(element, after) {
	 var insertPos = this.find(after)
	 if (insertPos > -1) {
		 this.dataStore.splice(insertPos + 1, 0, element)
		 this.liseSize++
		 return true
	 }
	 return false
 }
 function clear() {
	 delete this.dataStore
	 this.dataStore.length = 0
	 this.listSize = 0
	 this.pos = 0
 }
 function contains(element) {
	 for (var i = 0; i < this.dataStore.length; i++) {
		 return true
	 }
	 return false
 }
 // traverse the list method
 function front() {
	 this.pos = 0
 }
 function end() {
	 this.pos = this.listSize - 1
 }
 function prev() {
	 this.pos--
 }
 function next() {
	 if (this.pos < this.listSize) {
		 this.pos++
	 }
 }
 function currPos() {
	 return this.pos
 }
 function moveTo(position) {
	 this.pos = position
 }
 function hasNext() {
	 return this.pos < this.listSize
 }
 function hasPrev() {
	 return this.pos >= 0
 }
 function getElement() {
	 return this.dataStore[this.pos]
 }

// read the book file
 function readBooks (file) {
	var data = fs.readFileSync(file)
	var dataArray = data.toString (). split ('\ n')
	for (var i = 0; i < dataArray.length; i++) {
		dataArray [i] = dataArray [i] .trim ()
	}
	return dataArray
}
// save book to list
function saveBooksToList(file) {
	var booksArray = readBooks (file)
	for (var i = 0; i < booksArray.length; i++) {
		bookList.append(booksArray[i])
	}
}
// display list information
function displayList(list) {
	for (list.front(); list.currPos() < list.length(); list.next()) {
		console.log(list.getElement())
	}
}

// borrower information
function borrower(name, book) {
	  this.name = name
		this.book = book
}

// borrow book
function borrowBook(name, book, bookList, borrowerList) {
	if (bookList.contains(book)) {
		var b = new borrower(name, book)
		borrowerList.append(b)
		bookList.remove(book)
		console.log(name + 'borrowing book' + book + 'success!!!')
	} else {
		console.log('The book has been borrowed!!!')
	}
}
// test
 // book list object
 var bookList = new List()
 // borrower list object
 var borrowerList = new List()
 // save book to list
 saveBooksToList('book.txt')
 console.log('------------Currently available books-------------------')
 // display book information
 displayList(bookList)
 console.log('-----------------Start borrowing books-------------------')
 // borrow book
 borrowBook('xgq', 'Nodejs详解', bookList, borrowerList)
 console.log('-----------------borrowing information-------------------')
 // Display the list of borrowers
 displayList(borrowerList)
 console.log('-----------------Remaining books that can be borrowed---------------')
// List of books left after borrowing
 displayList(bookList)

Please point out any mistakes, thank you.

finished~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325384372&siteId=291194637