- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Implement a Queue in Lua Programming?
There are different approaches one can take to declare a queue in Lua; the most basic one is to make use of the tables and repeat function, and take two variables that will help us to insert and delete elements from the queue.
Example
Consider the code shown below −
queue = {} queue.first = 0 queue.last = -1 queue.data = {} function insert(q, val) q.last = q.last + 1 q.data[q.last] = val end function remove(q) if (q.first > q.last) then rval = -1 else print("remove: q.data[q.first]= ", q.data[q.first], " q.first= ", q.first) local rval = q.data[q.first] print("remove: rval= ", rval) q.data[q.first] = nil q.first = q.first + 1 print("remove: q.first= ", q.first) end return rval end insert(queue,"a") insert(queue,"b") insert(queue,"c") for i,v in ipairs(queue.data) do print(i, v) end repeat local x = remove(queue) print("list item= ", x) until (queue.first > queue.last)
Output
1 b 2 c remove: q.data[q.first]= a q.first= 0 remove: rval= a remove: q.first= 1 list item= nil remove: q.data[q.first]= b q.first= 1 remove: rval= b remove: q.first= 2 list item= nil remove: q.data[q.first]= c q.first= 2 remove: rval= c remove: q.first= 3 list item= nil
- Related Articles
- How to Use lua-mongo library in Lua Programming?
- How to make a namespace in Lua programming?
- How to split a string in Lua programming?
- How to convert a string to int in Lua programming?
- How to work with MySQL in Lua Programming?
- How to define and call a function in Lua Programming?
- How to Implement Priority Queue in Python?
- Comments in Lua Programming
- How to use the Insert function in Lua Programming?
- How to Use the Remove function in Lua programming?
- How to use the require function in Lua programming?
- How to use the Time package in Lua programming?
- Queue in Dart Programming
- How to implement Multithreaded queue With Python
- How to use Lua Programming to check if a file exists?

Advertisements