Computer Science Worksheet Page 3

ADVERTISEMENT

B. Write a member function Merge that will concatenate 2 Doubly Linked List into one.
(5 marks)
template <class T>
void DLList<T>::merge(DLList l)
1 Mark
{
DLLNode <T>*tmp=head;
1 Mark
for(;tmp->next!=0;tmp=tmp->next);
1 Mark
tmp->next=l.head;
1 Mark
l.head->prev=tmp;
1 Mark
}
Q3) Given the following definition of Circular Doubly linked List use the functions whose
prototype appear in the public section (if needed) to answer questions A:
template <class T>
class CDLList
{
CDLLNode<T> * tail;
// Pointer points to the last node
public:
bool is_empty( )
//returns true if the Circular Doubly linked List is empty
int size( )
//return number of nodes in Circular Doubly linked List
.
.
.
};
A) Define a member function deleteTail that will delete the Last node from Circular Doubly
linked List defined above.
(5 marks)
template<class T>
void CDLList<T>::deleteTail()
{
if(tail->prev==tail)
{
1 Mark
delete tail;
tail=0;
}
else
{
CDLLNode <T>*tmp=tail;
tail->prev->next=tail->next;
1 Mark
tail->next->prev=tail->prev;
1 Mark
1 Mark
tail=tail->prev;
1 Mark
delete tmp;
}
3

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3