Courses/CS 460/Fall 2005/Homework/Cynthia York/Oct 1

From CSWiki

Jump to: navigation, search

October 1st homework solutions


Contents


[edit] Homework Page

Homework page is: | http://cs.calstatela.edu/~wiki/index.php/Courses/CS_460/Fall_2005/Homework/Cynthia_York

Note that this same link (to the parent of this page as well as the rest of its path) is generated automatically at the top of every subpage.

[edit] Queue

  • Procedure Queue stores an object with a key in fifo order
  • I presented this procedure in class
local Q0 Q1 Q2 Q3
% Procedure Queue stores an object with a key in fifo order
   proc {Queue Key Object QueueIn QueueOut}
      local K O MEM in
	 K=Key
	 O=Object
	 MEM=mem(K O)
	 case MEM#QueueIn
	 of nil#QueueIn then QueueOut=QueueIn
	 [] MEM#nil then QueueOut=MEM
	 else  
	    QueueOut = QueueIn|MEM
	 end
      end
   end	
 
in
   {Queue 24346957 email21 nil Q0}
   {Queue 24336997 email32 Q0 Q1}
   {Queue 24573218 email27 Q1 Q2}
   {Queue 24218010 email95 Q2 Q3}
   {Browse Q3}
end

[edit] DeQueue

Procedure DeQueue removes members from a queue in fifo order

local Q0 Q1 Q2 Q3
% Procedure DeQueue removes members from a queue in fifo order
   proc {DeQueue QueueIn}
	 case QueueIn
	 of nil then {Browse 'End of queue reached'}
	 [](Q|Qr) then
	    {DeQueue Q}
	    {Browse 'member='#Qr}
	 else
	    {Browse 'member='#QueueIn}
	 end
   end

% Procedure Queue stores an object with a key in fifo order
   proc {Queue Key Object QueueIn QueueOut}
      local K O MEM in
	 K=Key
	 O=Object
	 MEM=mem(K O)
	 case MEM#QueueIn
	 of nil#QueueIn then QueueOut=QueueIn
	 [] MEM#nil then QueueOut=MEM
	 else  
	    QueueOut = QueueIn|MEM
	 end
      end
   end	
    
 
in
   {Queue 24346957 email21 nil Q0}
   {Queue 24336997 email32 Q0 Q1}
   {Queue 24573218 email27 Q1 Q2}
   {Queue 24218010 email95 Q2 Q3}
   {Browse 'loaded queue:'#Q3}
      
   {DeQueue Q3}
      
end
Personal tools