make a fifo queue in matlab

Here's a simple implementation of a FIFO queue in Matlab using a cell array:

main.m
classdef fifoQueue < handle
    
    properties (Access = private)
        data % the data
        head % head of the queue
        tail % tail of the queue
        size % size of the queue
        capacity % maximum capacity of the queue
    end
    
    methods
        
        function obj = fifoQueue(n)
            % Constructor method
            obj.data = cell(n,1);
            obj.head = 1;
            obj.tail = 1;
            obj.size = 0;
            obj.capacity = n;
        end
        
        function emptyFlag = isempty(obj)
            % Check if the queue is empty
            emptyFlag = (obj.size == 0);
        end
        
        function fullFlag = isfull(obj)
            % Check if the queue is full
            fullFlag = (obj.size == obj.capacity);
        end
        
        function push(obj, value)
            % Push an element to the queue
            if obj.isfull
                error('Queue is full!');
            end
            
            obj.data{obj.tail} = value;
            obj.tail = mod(obj.tail, obj.capacity) + 1;
            obj.size = obj.size + 1;
        end
        
        function value = pop(obj)
            % Pop an element from the queue
            if obj.isempty
                error('Queue is empty!');
            end
            
            value = obj.data{obj.head};
            obj.head = mod(obj.head, obj.capacity) + 1;
            obj.size = obj.size - 1;
        end
        
        function value = front(obj)
            % Get the front element of the queue without popping it
            if obj.isempty
                error('Queue is empty!');
            end
            
            value = obj.data{obj.head};
        end
        
        function value = back(obj)
            % Get the back element of the queue without popping it
            if obj.isempty
                error('Queue is empty!');
            end
            
            index = mod(obj.tail - 2, obj.capacity) + 1;
            value = obj.data{index};
        end
        
        function clear(obj)
            % Clear the queue
            obj.head = 1;
            obj.tail = 1;
            obj.size = 0;
        end
        
    end
    
end
2213 chars
83 lines

To use the fifoQueue class, simply create an instance of the class with the desired capacity:

main.m
myQueue = fifoQueue(10);
25 chars
2 lines

You can then push elements onto the queue using the push method:

main.m
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
51 chars
4 lines

You can get the front element of the queue using the front method:

main.m
frontElement = myQueue.front();
32 chars
2 lines

You can pop an element from the front of the queue using the pop method:

main.m
poppedElement = myQueue.pop();
31 chars
2 lines

And you can get the back element of the queue using the back method:

main.m
backElement = myQueue.back();
30 chars
2 lines

Finally, you can check if the queue is empty or full using the isempty and isfull methods, respectively:

main.m
isEmptyFlag = myQueue.isempty();
isFullFlag = myQueue.isfull();
64 chars
3 lines

gistlibby LogSnag