js队列操作函数 结构体数组

js队列操作函数

		//队列操作函数
		function Mesh_Send_List(){
			this.dataStore = [];					//新建队列
			this.enqueue = function enqueue ( element ) {	//向队列末尾添加一个元素,直接调用 push 方法即可
				this.dataStore.push( element );
			};
			this.dequeue = function dequeue () {	//删除队列首的元素,可以利用 JS 数组中的 shift 方法
				if( this.empty() ) return 'This queue is empty';
				else this.dataStore.shift();
			};
			this.front = function front(){			//查看队首元素,直接返回数组首个元素即可
				if( this.empty() ) return 'This queue is empty';
				else return this.dataStore[0];
			};
			this.back = function back () {			//读取队列尾的元素
				if( this.empty() ) return 'This queue is empty';
				else return this.dataStore[ this.dataStore.length - 1 ];
			};
			this.getall = function getall(){		//查看对了所有元素,我这里采用数组的 join 方法实现
				return this.dataStore.join('\n');
			};
			this.clear = function clear(){			//清空当前队列,也很简单,我们直接将 dataStore 数值清空即可
				delete this.dataStore;
				this.dataStore = [];
			};
			this.empty = function empty(){		   //我们通过判断 dataStore 的长度就可知道队列是否为空
				if( this.dataStore.length == 0 ) return true;
				else return false;
			};
		}
		
		var queue = new Mesh_Send_List();
		console.log( queue.empty() );  //true
		//添加几个元素
		queue.enqueue({
			Mesh_type:0,
			Mesh_addrH:0,
			Mesh_addrL:0,
			Mesh_data:0,
		});
		queue.enqueue({
			Mesh_type:1,
			Mesh_addrH:1,
			Mesh_addrL:1,
			Mesh_data:1,
		});
		queue.enqueue({
			Mesh_type:2,
			Mesh_addrH:2,
			Mesh_addrL:2,
			Mesh_data:2,
		});
		console.log( queue.empty() );  // false
		//查看队首元素
		console.log( queue.front().Mesh_type ); // Apple
		//查看队尾元素 
		console.log( queue.back().Mesh_type ); // Pear
		//出队
		queue.dequeue();
		//查看队首元素
		console.log( queue.front().Mesh_type ); // Banana
		//查看队尾元素 
		console.log( queue.back().Mesh_type ); // Pear
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值