{"size": 1959, "ext": "js", "max_stars_repo_path": "data-structures-and-algorithms-javascript/chapter6/index.js", "max_stars_repo_name": "Jocs/reading-notes", "max_stars_repo_stars_event_min_datetime": "2021-08-04T07:59:48.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T23:58:17.000Z", "max_issues_repo_path": "data-structures-and-algorithms-javascript/chapter6/index.js", "max_issues_repo_name": "Jocs/reading-notes", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "data-structures-and-algorithms-javascript/chapter6/index.js", "max_forks_repo_name": "Jocs/reading-notes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.0194174757, "max_line_length": 71, "alphanum_fraction": 0.6722817764}
var Node = function(element) {
	this.element = element
	this.next = null
}

var LinkedLint = function() {
	this.length = 0
	this.currentNode = null
	this.next = null
	this.find = find
	this.findPrevious = findPrevious
	this.remove = remove
	this.insert = insert
	this.advance = advance
	this.back = back
	this.show = show

	function find(item) {
		if (this.length === 0) return
		var node = this.currentNode
		var count = this.length
		while (count--) {
			if (node.element === item) return node
			node = node.next
		}
		return false
	}

	function insert(item, previous) {
		if (this.length === 0) {
			this.currentNode = new Node(item)
			this.currentNode.next = this.currentNode
		} else {
			var previousNode = this.find(previous)
			var node = new Node(item)

			node.next = previousNode.next
			previousNode.next = node
		}
		this.length++

	}

	function findPrevious(item) {
		if (this.length === 0) return false
		var node = this.currentNode
		var count = this.length
		while (count--) {
			if (node.next.element === item) return node
			node = node.next
		}
		return false
	}

	function remove(item) {
		var previousNode = this.findPrevious(item)
		var currentNode = this.find(item)
		if (this.currentNode === currentNode) this.currentNode = previousNode
		previousNode.next = currentNode.next
		currentNode.next = null
		this.length--
		return currentNode
	}

	function advance(n) {
		while(n--) {
			this.currentNode = this.currentNode.next
		}
	}
	function back(n) {
		while(n--) {
			this.currentNode = this.findPrevious(this.currentNode.element)
		}
	}
	function show() {
		return this.currentNode.element
	}
}

function killToM(n, m) {
	var linkedArray = new LinkedLint()
	linkedArray.insert(1)
	var i = 0;
	while(i < n - 1) {
		linkedArray.insert(i + 2, i + 1)
		i ++
	}
	while(linkedArray.length >= 3) {
		linkedArray.advance(m)
		var node = linkedArray.remove(linkedArray.currentNode.element)
		console.log(node)
	}
}
killToM(40, 3)








