Sample code for JavaScript encapsulation of one way linked list

  • 2021-08-21 19:37:13
  • OfStack

Use JavaScript to encapsulate one-way linked list:

1. A class that encapsulates LinkList to represent our linked list structure.

2. Within the LinkList class, there is an Node class that encapsulates information on each node (data and next).

3. Save two attributes in the linked list, one is the length of the linked list, and one is the first node in the linked list.

4. Common methods of encapsulating 1 linked list:

append (element): Want to add a new item to the end of the list; insert (position, element): Inserts a new item into a specific position in the list; get (position): Gets the element at the corresponding position; indexOf (element): Returns the index of the element in the linked list, or-1 if the element is not in the linked list; update (position, element): Modify an element at a certain location; removeAt (postion): Removes 1 item from a specific position in the list; remove (element): Remove 1 item from the list; isEmpty (): Returns true if the list does not contain any elements, otherwise returns false; size (): Returns the number of elements contained in the linked list; toString (): Outputs the values of linked list elements;

<script type="text/javascript">
	function LinkList(){
		/*  Node class  */
		function Node(data){
			this.data = data
			this.next = null
		}
		
		this.head = null
		this.length = 0
		/*  Append method  */
		LinkList.prototype.append = function(data){
			/*  Create a new node  */
			var newNode = new Node(data)
			if(this.length === 0){
				this.head = newNode
			}else{
				/*  Find the last 1 Nodes  */
				var current = this.head
				while(current.next){
					current = current.next
				}
				current.next = newNode
			}
			this.length += 1
		}

		/* toString Method  */
		LinkList.prototype.toString = function(){
			var current = this.head
			var listString = ""
			
			while(current){
				listString += current.data +" "
				current = current.next
			}
			return listString
		}

		/* insert Method  */
		LinkList.prototype.insert = function(position,data){
			/*  Right position Make a cross-border judgment  */
			if(position<0||position>this.length) return false
			var node = new Node(data)
			if(position == 0){
				node.next = this.head
				this.head = node
			}else{
				var index = 0
				var current = this.head
				var previous = null
				while(index++ < position){
					previous = current
					current = current.next
				}
				node.next = current
				previous.next = node
			}
			this.length += 1
			return true
		}
		
		/* get Method  */
		LinkList.prototype.get = function(position){
			/*  Cross-border judgment  */
			if(position<0 || position >= this.length) return null
			
			var current = this.head
			var index = 0
			while(index++ < position){
				current = current.next
			}
			return current.data
		}

		/* indexOf Method  */
		LinkList.prototype.indexOf = function(data){
			/*  Defining variables  */
			var current = this.head
			var index = 0
			/*  Start searching  */
			while(current){
				if(current.data === data){
					return index
				}else{
					current = current.next
					index += 1
				}
			}
			return -1
		}

		/* update Method  */
		LinkList.prototype.update = function(position,data){
			/*  Cross-border judgment  */
			if(position<0 || position >= this.length) return false
			
			var current = this.head
			var index = 0
			while(index++ < position){
				current = current.next
			}
			/*  Modify data */
			current.data = data
			return true
		}

		/* removeAt Method  */
		LinkList.prototype.removeAt = function(position){
			/*  Cross-border judgment  */
			if(position<0 || position >= this.length) return null
			var current = this.head
			if(position === 0){
				this.head = this.head.next
			}else{
				var index = 0
				var previous = null
				while(index++ < position){
					previous = current
					current = current.next
				}
				previous.next = current.next
			}
			this.length -= 1
			return current.data
		}
		
		/* remove */
		LinkList.prototype.remove = function(data){
			/*  According to data Find a position  */
			var position = this.indexOf(data)
			return this.removeAt(position)
		}
		
		LinkList.prototype.isEmpty = function(){
			return this.length === 0
		}
		
		LinkList.prototype.size = function(){
			return this.length
		}
		
	}
	
	
	/*  Test  */
	var list = new LinkList()
	list.append('a')
	list.append('b')
	list.append('c')
	console.log(list.toString()) /* a b c */
	
	list.insert(3,'d')
	console.log(list.toString())/* a b c d */
	
	console.log(list.get(2)) /* c */
	console.log(list.indexOf('d')) /* 3 */
	
	list.update(1,'bbb')
	console.log(list.toString()) /* a bbb c d */
	
	console.log(list.removeAt(2)) /* c */
	console.log(list.toString())/* a bbb d */
	
	console.log(list.remove('a'))
	console.log(list.toString())/* bbb d */
	
	console.log(list.isEmpty()) /* false */
	
	console.log(list.size()) /* 2 */
</script>

The above is JavaScript encapsulated unidirectional linked list sample code details, more about JavaScript encapsulated unidirectional linked list information please pay attention to this site other related articles!


Related articles: