Question: You are given a Matlab script ?le queueTest.m that contains a number of functions that allow you to create and manipulate a queue. The queue

You are given a Matlab script ?le queueTest.m that contains a number of functions that allow you to create and manipulate a queue. The queue is implemented as a cell array. A queue has the property that the ?rst item in is the ?rst item out. The main function, queueTest, just tests that the rest of the functions are working properly. It creates a new queue, adds and removes items to the queue, gets the ?rst item of the queue and checks if the queue is empty. After each such operation, it displays the contents of the queue, so that you can check that each operation has been correctly executed. This function, and the function createQueue, are complete. You need to write the code for the functions enqueue, dequeue, searchQueue, displayQueue, peek and isEmpty. Comments above each function will tell you what the function is meant to do. Implement each of the functions and then run queueTest from within Matlab, to check that everything is working properly. The ?rst two functions are worth 2.5 marks each, the second two 3 marks each and the last two 1 mark each.

CODE:

% --------------------------------------------------------------------- % The main function just tests that the rest of the functions are working % properly. It creates a queue, displays what is in the queue, searches % for an item in the queue, displays it if found, removes the top item % from the queue and displays the remaining items in the queue. The queue in % this case only contains string items, for the sake of simplicity, in terms % of implementing the displayQueue function. You need to implement the % code for the functions with empty bodies. The function signatures and % what each function is meant to do are given, as comments. % ---------------------------------------------------------------------

function queueTest

clear; % Creates a new queue and adds some items

q=createQueue(); % Displays what's in the queue displayQueue(q); % Searches for a particular item foundItem=searchQueue(q,'I Am... Sasha Fierce'); % Displays the item, if found if (length(foundItem) == 1) disp(' '); disp('Details of item found in queue'); disp('******************************'); disp(q{foundItem}); disp(' '); end % Removes the item at the top of the queue [q item]=dequeue(q); % Displays what's in the queue displayQueue(q); end

% Creates a new queue collection.

function q = createQueue()

q={}; q = enqueue(q,'Genius Loves Company'); q = enqueue(q,'Blues'); q = enqueue(q,'Amore'); q = enqueue(q,'I Am... Sasha Fierce'); q = enqueue(q,'Destination');

end

% Adds an item to the end of the queue. Returns the new queue.

function q = enqueue(q,item)

%-- Insert your code here --%

end

% Removes the first item from the queue. Returns the item and the % new queue.

function [q item] = dequeue(q)

%-- Insert your code here --%

end

% Returns the first item in the queue without changing the queue.

function item = peek(q)

%-- Insert your code here --%

end

% Checks if the queue is empty. Returns 1 if true; 0 otherwise.

function ans = isEmpty(q)

%-- Insert your code here --%

end

% Displays the items in the queue, starting from the front of the % queue. Assumes that all the items are strings.

function displayQueue(q)

%-- Insert your code here --%

end

% Searches for the given item in the queue. Assume that the queue only % contains strings. Returns the index of the first occurrence of the item % in the queue, starting from the front of the queue, if found; % else returns -1.

function ans = searchQueue(q, item)

%-- Insert your code here --%

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!