Lua - List as Queue



The queue is a data structure which follows First-In-First-Out (FIFO) approach. It represents a collection of elements where the elements maintain the order in which they are entered into the collection.

Queue Implementation Methods

We'll implement following methods to implement queue.

  • enqueue− inserts an element to the queue.

  • dequeue− deletes an element from the queue.

enqueue method implementation

During enqueue() operation, the element will be pushed to the end of the linked list. The prev and next references are updated accordingly.

-- insert an element to the end of the list
function list:enqueue(t)
   -- move till last node    
   if self.last then
      self.last._next = t
      t._prev = self.last
      self.last = t
   else
      -- set the node as first node
      self.first = t
      self.last = t
   end
   -- increment the length of the list
   self.length = self.length + 1
end

dequeue method implementation

During dequeue() operation, the element will be removed from the start of the linked list. The prev and next references are updated accordingly.

-- remove an element from the start of the list
function list:dequeue()
  -- if list is empty
  if not self.first then return end
  
  local returnedValue = self.first
  
  if returnedValue._next then
    returnedValue._next._prev = nil
    self.first = returnedValue._next
    returnedValue._next = nil
  else
    -- this was the only node
    self.first = nil
    self.last = nil
  end
  
  self.length = self.length - 1
  return returnedValue
end

Test Queue Operations

-- create the queue
local queue = list()

-- insert values to the queue
queue:enqueue({'A'})
queue:enqueue({'B'})
queue:enqueue({'C'})

-- print the size of the queue
print("Queue Initial Size:", queue:size())

-- pop top element from the queue
print("Value Retrived: ", queue:dequeue()[1])

-- print the updated size of the queue
print("Queue Updated Size:", queue:size())

-- pop top element from the queue
print("Value Retrived: ", queue:dequeue()[1])

-- print the updated size of the queue
print("Queue Updated Size:", queue:size())

Complete Example - Queue Implementation using List

Following is the complete example of a Queue implementation using a linked list.

main.lua

-- List Implementation
list = {}
list.__index = list

setmetatable(list, { __call = function(_, ...)
   local t = setmetatable({ length = 0 }, list)
      for _, v in ipairs{...} 
         do t:push(v) 
      end
      return t
   end })

-- insert an element to the end of the list
function list:enqueue(t)
   -- move till last node    
   if self.last then
      self.last._next = t
      t._prev = self.last
      self.last = t
   else
      -- set the node as first node
      self.first = t
      self.last = t
   end
   -- increment the length of the list
   self.length = self.length + 1
end

-- remove an element from the start of the list
function list:dequeue()
  -- if list is empty
  if not self.first then return end
  
  local returnedValue = self.first
  
  if returnedValue._next then
    returnedValue._next._prev = nil
    self.first = returnedValue._next
    returnedValue._next = nil
  else
    -- this was the only node
    self.first = nil
    self.last = nil
  end
  
  self.length = self.length - 1
  return returnedValue
end

function list:size()
   return self.length
end

-- create the queue
local queue = list()

-- insert values to the queue
queue:enqueue({'A'})
queue:enqueue({'B'})
queue:enqueue({'C'})

-- print the size of the queue
print("Queue Initial Size:", queue:size())

-- pop top element from the queue
print("Value Retrived: ", queue:dequeue()[1])

-- print the updated size of the queue
print("Queue Updated Size:", queue:size())

-- pop top element from the queue
print("Value Retrived: ", queue:dequeue()[1])

-- print the updated size of the queue
print("Queue Updated Size:", queue:size())

Output

When we run the above code, we will get the following output−

Queue Initial Size:	3
Value Retrived: 	A
Queue Updated Size:	2
Value Retrived: 	B
Queue Updated Size:	1
Advertisements