File Exchange

image thumbnail

list, queue, stack

version 1.1 (6.31 KB) by

implement the list, queue, stack data structures in matlab

4.375
9 Ratings

22 Downloads

Updated

View License

The files implement the list, queue, stack data structures in matlab, and the queue and stack are a litter faster than the java's alternatives.

CStack define a stack data strcuture

It likes java.util.Stack, however, it could use CStack.content() to
return all the data (in cells) of the Stack, and it is a litter faster
than java's Stack.

s = CStack(c); c is a cells, and could be omitted
s.size() return the numble of element
s.empty() return whether the stack is empty
s.push(el) push el to the top of stack
s.pop() pop out the top of the stack, and return the element
s.top() return the top element of the stack
s.remove() remove all the elements in the stack
s.content() return all the data of the stack (in the form of a
cells with size [s.size(), 1]

CQueue define a queue data strcuture

It likes java.util.Queue, however, it could use CQueue.content() to
return all the data (in cells) of the Queue, and it is a litter faster
than java's Queue.

q = CQueue(c); c is a cells, and could be omitted
s.size() return the numble of element
s.empty() return whether the qeueu is empty
s.push(el) push el to the top of qeueu
s.pop() pop out the the beginning of queue, and return the element
s.front() return the the the beginning element of the qeueu
s.back() return the the the rear element of the qeueu
s.remove() remove all data from the queue
s.content() return all the data of the queue (in the form of a
cells with size [s.size(), 1]

update 2011-05-05
1. add an method "isempty" to determine if the stack and queue is emtpy, and the method "emtpy" is to remove the content of the stack and queue. (thanks to Vojtech)
2. throw a warning when top an empty stack, and back, front an empty queue. throw an error when pop an empty stack and queue. (Thanks to Derek)

Comments and Ratings (16)

David

David (view profile)

Lack robustness.

jack

jack (view profile)

Thx for the code but i have used your implemented CQueue to hold cell arrays but I keep getting this error:

Undefined function 'ge' for input arguments of type 'cell'.

Error in CQueue/push (line 96)
if obj.rear >= obj.front

Error in epsilonGridSampling (line 54)
ranges.push({tmp{i,:}});

How can i solve this issue?

** My queue is created as follows:
Q = CQueue();

Ali Askari

As also mentioned by "The Lion" the Queue.m file has a bug. Otherwise works really good.

Ali Askari

can any one tell me pls how to print all elements in stack and store it in an array??

The Lion

I found a bug in Queue.m file
Line 96

if obj.rear >= obj.front

is a mistake.
obj.front is the value of the first element, not the position of the firs element.

correct it like this:
if obj.rear >= obj.beg
obj.buffer(1:sz) = obj.buffer(obj.beg:obj.rear-1);
else
obj.buffer(1:sz) = obj.buffer([obj.beg:obj.capacity 1:obj.rear-1]);
end

Frank Huang

Hi,Mr.Zhang
Thanks for your code
CStack.m and CQueue.m worked well.However, CList.m didn't work well.
the function list.add(el,k) seems work out of expectation: When I used it to add elements to list, its size remain zero.

Yiming

Yiming (view profile)

Vivi

Vivi (view profile)

How should I use it?
Thx
Beginner Matlab user

sudesh

sudesh (view profile)

how to use these classes in MATLAB?

Karel

Karel (view profile)

There is a considerably bug in CQueue. On line 96, it says: if obj.rear >= obj.front. This should be obj.rear >= obj.beg, as front points to the front object, not to the index of the front of the queue. This bug first occurs after the 100th element is added, because that is the default capacity.

Zhiqiang Zhang

Zhiqiang Zhang (view profile)

I makes two updates (waiting for moderation)

1. add an method "isempty" to determine if the stack and queue is emtpy, and the method "emtpy" is to remove the content of the stack and queue. (thanks to Vojtech)
2. throw a warning when top an empty stack, and back, front an empty queue. throw an error when pop an empty stack and queue. (Thanks to Derek)

Derek

Derek (view profile)

Possible issues with CQueue. Popped items still appear at the back of the queue. And popping when it is empty causes problems.

q = CQueue % "The queue is empty"
q.front() % "[]"
q.back() % "[]" So far so good.
q.push(100)
q.front() % "100"
q.back() % "100" That's correct.
q.pop() % "100"
q.front() % "[]"
q.back() % "100" Huh? Queue should be empty at this point.
q % "The queue is empty"
q.pop() % "[]" Popping an empty queue shouldn't change anything.
q % "1-th element of the stack:
2-th element of the stack:
3-th element of the stack:
4-th element of the stack:
5-th element of the stack:
6 ..." Queue is suddenly 100 items long.

Vojtech

Hi,
maybe the description for mathod empty() is a bit confusing. What I expected from the description was logical value 1 if queue is empty and 0 if queue is not empty. However the method returns size of the queue and empties it. So it is bit redundant - methods size() and empty() can do this work.

My suggestion is to rename the function to isempty() and change the code of this method:
function b = isempty(obj)
b = ~logical(obj.size());
end

Updates

1.1

1. add an method "isempty"
2. throw a warning when top an empty stack, and back, front an empty queue. throw an error when pop an empty stack and queue. (Thanks to Derek)

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video