4.8

4.8 | 5 ratings Rate this file 49 Downloads (last 30 days) File Size: 6.31 KB File ID: #28922

list, queue, stack

by

 

02 Oct 2010 (Updated )

implement the list, queue, stack data structures in matlab

| Watch this File

File Information
Description

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)

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
20 Feb 2014 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

21 Nov 2013 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.

30 Jun 2013 Sachchit Vekaria  
29 Nov 2012 Yiming  
21 Oct 2012 Vivi

How should I use it?
Thx
Beginner Matlab user

04 Feb 2012 sudesh

how to use these classes in MATLAB?

22 Oct 2011 Karel

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.

05 May 2011 Zhiqiang Zhang

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)

04 May 2011 Derek

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.

23 Dec 2010 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
05 May 2011

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)

Contact us