Code covered by the BSD License  

Highlights from
list, queue, stack

5.0

5.0 | 1 rating Rate this file 50 Downloads (last 30 days) File Size: 6.31 KB File ID: #28922

list, queue, stack

by Zhiqiang Zhang

 

02 Oct 2010 (Updated 05 May 2011)

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 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
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

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.

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)

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.

04 Feb 2012 sudesh

how to use these classes in MATLAB?

Please login to add a comment or rating.
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)

Tag Activity for this File
Tag Applied By Date/Time
queue Zhiqiang Zhang 04 Oct 2010 11:22:33
stack Zhiqiang Zhang 04 Oct 2010 11:22:33
data strcuture Zhiqiang Zhang 04 Oct 2010 11:22:33
oop per isakson 08 Oct 2010 13:50:28
data strcuture Vibhor 04 May 2011 07:29:34

Contact us at files@mathworks.com