18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

258 Conta<strong>in</strong>ers<br />

The code for Insert is complex due to the necessity of handl<strong>in</strong>g <strong>in</strong>sertion at different places <strong>in</strong> the l<strong>in</strong>ked<br />

list. In particular, the list’s access values to the physical storage of the list will need to be updated. Remember, the<br />

iterator only knows about the current position <strong>in</strong> the list.<br />

In the implementation of Insert there are four dist<strong>in</strong>ct cases to handle when a data item is <strong>in</strong>serted. This is<br />

summarized <strong>in</strong> the table below:<br />

Position<br />

On an empty list<br />

Beyond the last item <strong>in</strong> the list<br />

Before the first item<br />

In the middle of the list<br />

Commentary<br />

Will need to update the list’s access values<br />

Cur_List_First and Cur_List_Last as<br />

well as update the current position Cur_Node <strong>in</strong><br />

the iterator.<br />

Will need to update the list’s access value<br />

Cur_List_Last as well as update the current<br />

position Cur_Node <strong>in</strong> the iterator.<br />

Will need to update the list’s access value<br />

Cur_List_First.<br />

No updat<strong>in</strong>g required to the list’s access values<br />

nor the current position of the iterator.<br />

The implementation of the <strong>in</strong>sert procedure is as follows:<br />

procedure Insert( The:<strong>in</strong> out List_Iter; Data:<strong>in</strong> T ) is<br />

Tmp : P_Node;<br />

Cur : P_Node := The.Cur_Node; --Current element<br />

First : P_P_Node := The.Cur_List_First;<br />

Last : P_P_Node := The.Cur_List_Last;<br />

beg<strong>in</strong><br />

if Cur = null then --Empty or last item<br />

if First.all = null then -- Empty list<br />

Tmp := new Node'( null, Data, null );<br />

First.all := Tmp;<br />

Last.all := Tmp;<br />

The.Cur_Node := Tmp;<br />

else<br />

-- Last<br />

Tmp := new Node'( Last.all, Data, null );<br />

Last.all.Next := Tmp;<br />

Last.all := Tmp;<br />

The.Cur_Node := Tmp;<br />

end if;<br />

else<br />

Tmp := new Node'( Cur.Prev, Data, Cur );<br />

if Cur.Prev = null then --First item<br />

First.all := Tmp;<br />

else<br />

Cur.Prev.Next := Tmp;<br />

end if;<br />

Cur.Prev := Tmp;<br />

end if;<br />

end Insert;<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!