Rename year directories to allow natural ordering
This commit is contained in:
Binary file not shown.
28
year2/semester2/CT248/Assignments/Assignment-03/mystack.m
Normal file
28
year2/semester2/CT248/Assignments/Assignment-03/mystack.m
Normal file
@ -0,0 +1,28 @@
|
||||
function[push, pop, peek] = mystack()
|
||||
% function to return handles to the subfunction push, pop, & peek
|
||||
push = @push;
|
||||
pop = @pop;
|
||||
peek = @peek;
|
||||
end
|
||||
|
||||
function [stack] = push(stack, value)
|
||||
% function to push a value onto the stack at location 1 and return the
|
||||
% stack
|
||||
stack = [value; stack];
|
||||
end
|
||||
|
||||
function [stack] = pop(stack)
|
||||
% function to pop the value at location 1 off the stack and return the
|
||||
% stack
|
||||
stack(1) = [];
|
||||
end
|
||||
|
||||
function [value] = peek(stack)
|
||||
% function to return the top value from the stack (arrau location 1)
|
||||
% returns NaN if there is no value at location 1
|
||||
if isempty(stack)
|
||||
value = NaN;
|
||||
else
|
||||
value = stack(1);
|
||||
end
|
||||
end
|
13
year2/semester2/CT248/Assignments/Assignment-03/test.m
Normal file
13
year2/semester2/CT248/Assignments/Assignment-03/test.m
Normal file
@ -0,0 +1,13 @@
|
||||
% test script as specified in assignment spec
|
||||
[push, pop, peek] = mystack();
|
||||
|
||||
stack = []
|
||||
stack = push(stack, 100)
|
||||
|
||||
stack = push(stack, 200)
|
||||
|
||||
peek(stack)
|
||||
|
||||
stack = pop(stack)
|
||||
|
||||
peek(stack)
|
Reference in New Issue
Block a user