The iQueue Service

CS121 Data Structures and Algorithms
1. Various parts refer to Appendices I and II. Appendix I gives the package specification
you were required to implement in your second DSA coursework. Appendix II gives the
specification of the package providing an iQueue service, which was also involved in the
same piece of coursework.
a) Suppose we have:
superMkt : iQueue_Cllctn;
Draw a diagram of the actual physical data structure associated with variable
superMkt, given that:
•
•
•
the iQueue collection has the name “Tesco”
the iQueue collection comprises just two iQueues with the names “Checkout A”
and “Checkout B” (the former having been created first)
iQueue “Checkout A” comprises two customers known as “BR” and “SS” (in that
order), and iQueue “Checkout B” is empty.
Make sure you include in your diagram ALL ‘storage objects/cells’ (and the values
they hold) which contribute to the complete data structure. Add any brief explanatory
comments you feel are appropriate. (NOTE: there is no need to pad out string values
with trailing spaces.)
(5 marks)
b) Explain the significance of LIMITED PRIVATE in the declarations of types
iQueue_Cllctn and iQueue at the start of the package specifications in Appendices I
and II respectively. Would it be OK to drop the keyword LIMITED and use just
PRIVATE in either declaration? Explain your answer in the case of type iQueue.
(5 marks)
c) Both the package iQCllctn_ati and the package iQueue_ati use exceptions to
signal errors. BRIEFLY summarise the basic ‘mechanics’ of how this method of error
signalling works.
(5 marks)
d) Suppose a program called Bham_Airport makes use of the package iQCllctn_ati as
well as performing some textual input and output. Describe AS A DIAGRAM the full
client-service structure which exists in this application. Your diagram should include
the program and all library units (packages, etc) involved (remember that the body of
package iQueue_ati performs storage deallocation). Can the program Bham_Airport
perform iQueue — as opposed to iQueue collection — operations? Explain your
answer.
(5 marks)
Page 1 of 7
CS121 Data Structures and Algorithms
2. Consider a set of Digital Video Discs (DVDs). Each DVD has a title (no longer than 32
characters) and an approximate total viewing time in minutes and seconds.
In order to represent a set of DVDs in an Ada program, the following outline scheme will
be used:
— for DVD titles
— for DVD total viewing times
— for representing ‘DVD nodes’ in linked lists
— overall type for representing DVD sets
SUBTYPE DVD_Title … … …
TYPE DVD_ViewTime … … …
TYPE DVD_Node … … …
TYPE DVD_Set … … …
Now answer the following questions:
a) Briefly describe with the aid of a diagram what is meant by a ‘bucketed hash table’.
State ONE main advantage of using hash tables (of any kind).
(5 marks)
b) Complete the declarations in the above scheme given that a data structure of type
DVD_Set is to be implemented as a bucketed hash table. Note that you will need to
add further subsidiary declarations of your own in order to complete the scheme.
Marks will be awarded for good style/use of Ada.
(8 marks)
c) What is a hash function, and what is its role in hash-table schemes?
Suppose there is a hash function called hf for the scheme you have described in part
(b). Given:
cthd : CONSTANT DVD_Title := “Crouching Tiger, Hidden Dragon
myDVDs : DVD_Set;
”;
write some code which, given your data structure scheme in part (b), determines
whether the set of DVDs myDVDs includes a DVD with the title cthd. A variable
found should be set appropriately to indicate the result. Make sure you declare all
additional variables that your code uses.
(7 marks)
Page 2 of 7
CS121 Data Structures and Algorithms
3. a) Briefly describe TWO main advantages of using Ada packages in software
development.
(4 marks)
b) It is required to implement a software service for handling ‘traversable stack’ data
structures, or ‘tStacks’. The handling of a tStack includes the normal stack operations
of Init(ts) (initialises a tStack ts to empty), Push(e,ts) (places element e on the
top of tStack ts), Pop(ts,e) (obtains by removal the top element e of tStack ts),
depth(ts) (gives the number of elements in tStack ts), and isEmpty(ts) (tests
whether tStack ts is empty). However, tStacks have the following additional features:
•
A tStack has an extra component called a ‘cursor’; basically, the cursor is used to
access different elements of the tStack (not just its top element).
•
There are three additional operations, all of which involve the cursor component
of a tStack:
read(ts)
Down(ts)
Reset(ts)
returns the element currently accessed by the cursor of tStack ts
moves the cursor of tStack ts down one position
moves the cursor to the top of the tStack ts.
Provide the COMPLETE code of the SPECIFICATION of an Ada package called
tStack_ati which provides a service for handling tStacks whose elements are
natural numbers. The data type scheme must be ARRAY-BASED and fully exploit
information hiding. You should ignore all issues connected with error signalling, etc,
in your answer. Do NOT provide any code for the package body.
(12 marks)
c) Given the data structure scheme in your package specification, provide a complete
implementation of the operation read as it would appear in the body of package
tStack_ati. State any assumption(s) that you make in your code and what would
happen if your assumption(s) did not hold.
(4 marks)
Page 3 of 7
CS121 Data Structures and Algorithms
4. Here is a data structure scheme for iQueues based on that used in package iQueue_ati in
Appendix II but with one difference: an extra level of access between iQueue variables
and iQueue ‘header records’.
TYPE Node;
TYPE Pntr_to_Node IS ACCESS Node;
TYPE Node IS RECORD
value : Elem;
link : Pntr_to_Node;
END RECORD;
TYPE iQHeader IS RECORD
front : Pntr_to_Node := Null;
len : Natural := 0;
END RECORD;
TYPE iQueue IS ACCESS iQHeader;
a) With the aid of a diagram, explain what the code
uniQue.front.link.value
(*)
refers to, given:
uniQue : iQueue;
and that uniQue is referencing a suitable data structure. Under what circumstances
would the code (*) cause a run-time exception to be raised?
Suppose the value of variable uniQue is Null. What meaning, if any, does this have
in iQueue terms?
(6 marks)
b) Code up the iQueue operation Init for the data structure scheme above, given that
the operation does appropriate storage deallocation. You may assume the existence of
a procedure FreeList(ptr : IN OUT Pntr_to_Node) which deallocates an entire
chain of nodes accessed by ptr.
(3 marks)
c) What changes, if any, would need to be made to the data structure scheme at the start
of the question so that it was suitable for implementing sets rather than iQueues.
Justify your answer.
Explain what similarities exist between queues and sequential files.
(5 marks)
d) Complete the coding of the following procedure:
PROCEDURE BackUpiQ(iQ : IN iQueue) IS … … …
This procedure backs up the elements of the iQueue iQ into an existing sequential file
called “iQBU.dat”. Although this file is not open when the procedure is called (and
must be closed when the procedure returns), you may assume that then following
declaration has been previously elaborated:
PACKAGE Elem_SIO IS NEW Ada.Sequential_IO(Elem);
(6 marks)
Page 4 of 7
CS121 Data Structures and Algorithms
5. a) The overall kind of data structure used for implementing iQueue collections in
package iQCllctn_ati in Appendix I is based on the idea of a ‘binary search tree’
(BST). Summarise the distinguishing characteristics of such a tree scheme and its
main advantages.
(5 marks)
b) Consider the following BST scheme where nodes include an extra field noccs to
maintain a count of how many times an element has been ‘entered’ into the tree:
TYPE Node;
TYPE BinST IS ACCESS Node;
TYPE Node IS RECORD
value : Elem;
noccs : Natural; -- number of occurrences
leftST, rightST : BinST;
END RECORD;
Assuming Elem denotes characters, draw the actual concrete tree data structure (of
type BinST above) that would result from inserting the characters in the following
sequence in strict left-to-right order, given that the BST is initially empty (ignore the
spacing between characters):
H I D D E N D R A G O N
(5 marks)
c) Complete the coding of the following insertion procedure:
PROCEDURE Insert(e : IN Elem; bst : IN OUT BinST) IS … … …
where type BinST is as in part (b).
(6 marks)
d) Explain in your own words (do NOT write any code) how — by having the noccs
field per BST node as in part (b) above, and making this field subtype Natural (as
opposed to Positive) — this enables a much simpler deletion algorithm to be coded
than would normally be the case.
(4 marks)
Page 5 of 7
CS121 Data Structures and Algorithms
APPENDIX I
The iQueue_Collection Service
WITH iQueue_ati; USE iQueue_ati;
PACKAGE iQCllctn_ati IS
-- main ‘abstract type’:
TYPE iQueue_Cllctn IS LIMITED PRIVATE;
-- auxiliaries:
max_iQC_NameLen : CONSTANT Positive := 12;
SUBTYPE iQCllctn_Name IS String(1 .. max_iQC_NameLen);
max_iQ_NameLen : CONSTANT Positive := 8;
SUBTYPE iQueue_Name IS String(1 .. max_iQ_NameLen);
TYPE iQName_List IS ARRAY(Positive RANGE <>) OF iQueue_Name;
-- operations on iQueue collections:
PROCEDURE Create(iQCn : IN iQCllctn_Name; iQC : IN OUT iQueue_Cllctn);
PROCEDURE Init(iQC : IN OUT iQueue_Cllctn);
PROCEDURE Create_iQ(iQn : IN iQueue_Name; iQC : IN OUT iQueue_Cllctn);
PROCEDURE Close_iQ(iQn : IN iQueue_Name; iQC : IN OUT iQueue_Cllctn);
PROCEDURE Merge_iQs(iQn1, iQn2 : iQueue_Name; iQC : IN OUT iQueue_Cllctn);
PROCEDURE Find_elem(qel : IN Elem; iQC : IN iQueue_Cllctn;
epos : OUT Natural; iQn : OUT iQueue_Name);
FUNCTION iQ_isIn(iQn : iQueue_Name; iQC : iQueue_Cllctn) RETURN Boolean;
FUNCTION iQusOf(iQC : iQueue_Cllctn) RETURN iQName_List;
FUNCTION sizeOf(iQC : iQueue_Cllctn) RETURN Natural;
FUNCTION nameOf(iQC : iQueue_Cllctn) RETURN iQCllctn_Name;
-- operations on individual iQueues in an iQueue collection:
PROCEDURE iQInit(iQC : IN iQueue_Cllctn; iQn : IN iQueue_Name);
PROCEDURE iQAdd(iQC : IN iQueue_Cllctn; qel : IN Elem; iQn : IN iQueue_Name);
PROCEDURE iQRemove(iQC : IN iQueue_Cllctn; iQn : IN iQueue_Name;
qel : OUT Elem);
FUNCTION iQlength(iQC : iQueue_Cllctn; iQn : iQueue_Name) RETURN Natural;
FUNCTION iQelemAt(iQC : iQueue_Cllctn; qpos : Positive;
iQn : iQueue_Name) RETURN Elem;
FUNCTION iQposOf(iQC : iQueue_Cllctn; qel : Elem;
iQn : iQueue_Name) RETURN Natural;
-- error signals:
iQName_In_Use, iQName_Not_Found, iQueue_NonEmpty : EXCEPTION;
PRIVATE
-- data structure scheme:
TYPE Node;
TYPE iQuBst IS ACCESS Node;
TYPE Node IS RECORD
iQname : iQueue_Name;
iQu : iQueue;
leftSt, rightSt : iQuBst;
END RECORD;
TYPE iQueue_Cllctn IS RECORD
iQCname : iQCllctn_Name;
iQCsize : Natural := 0;
iQus : iQuBst;
END RECORD;
END iQCllctn_ati;
Page 6 of 7
CS121 Data Structures and Algorithms
APPENDIX II
The iQueue Service
PACKAGE iQueue_ati IS
-- auxiliaries:
SUBTYPE Elem IS String(1..6);
-- main ‘abstract type’:
TYPE iQueue IS LIMITED PRIVATE;
-- construction & modification:
PROCEDURE Init(iQ : OUT iQueue);
PROCEDURE Add(e : IN Elem; iQ : IN OUT iQueue);
PROCEDURE JumpAdd(e : IN Elem; pos : IN Positive; iQ : IN OUT iQueue);
PROCEDURE Leave(pos : IN Positive; iQ : IN OUT iQueue);
PROCEDURE Remove(iQ : IN OUT iQueue; e : OUT Elem);
PROCEDURE PrioRemove(pos : IN Positive; iQ : IN OUT iQueue; e : OUT Elem);
-- access and observation:
TYPE iQElems IS ARRAY(Integer RANGE <>) OF Elem;
FUNCTION elmntsOf(iQ : iQueue) RETURN iQElems;
FUNCTION elemAt(pos : Positive; iQ : iQueue) RETURN Elem;
FUNCTION isEmpty(iQ : iQueue) RETURN Boolean;
FUNCTION length(iQ : iQueue) RETURN Natural;
FUNCTION posOf(e : Elem; iQ : iQueue) RETURN Natural;
-- error signals:
Already_In_iQueue, iQueue_Empty, Invalid_Position : EXCEPTION;
PRIVATE
-- data structure scheme:
TYPE Node;
TYPE Pntr_to_Node IS ACCESS Node;
TYPE Node IS RECORD
value : Elem;
link : Pntr_to_Node;
END RECORD;
TYPE iQueue IS RECORD
front : Pntr_To_Node := Null;
len : Natural := 0;
END RECORD;
END iQueue_ati;
END OF EXAMINATION PAPER
Page 7 of 7