Question: Question 2 Implement the 2 outstanding housekeeping functions: the copy - constructor and the assignment copy - constructor. 1 template 2 class LinkedList { 3

Question 2 Implement the 2 outstanding housekeeping functions: the copy-constructor and the
assignment copy-constructor.
1 template
2 class LinkedList {
3 public :
4 LinkedList ();
5 LinkedList (const LinkedList & obj );
6 LinkedList & operator =(const LinkedList & obj );
7 ~ LinkedList ();
8 bool isEmpty () const;
9 const E& front () const throw( LinkedListEmpty );
10 const E& back () const throw( LinkedListEmpty );
11 void addFront (const E& e);
12 void removeFront () throw( LinkedListEmpty );
13 void addBack (const E& s);
14 void removeBack () throw( LinkedListEmpty );
15 friend ostream & operator <<( ostream & out , const LinkedList & obj ){
16 Node * temp = obj.head;
17 if(temp == NULL ){ out <<"[]"; return out ;}
18 out <<"[";
19 while(temp != NULL ){
20 out << temp ->elem;
21 if(temp ->next != NULL ){ out <<"]-->[";}
22 temp = temp ->next;
7
23}
24 out <<"]";
25 return out;
26}
27 private :
28 Node * head;
29};
Input Main:
1 int main(void ){
2 LinkedList * myList = new LinkedList ();
3 cout <<*myList << endl;
4// Adding to the front
5 cout << myList -> isEmpty ()<< endl;
6 myList -> addFront (" Gandalf ");
7 cout <<*myList << endl;
8 myList -> addFront (" Aragorn ");
9 cout <<*myList << endl;
10 myList -> addFront (" Legolas ");
11 cout <<*myList << endl;
12 cout << "Front element : \t"<< myList ->front ()<< endl;
13 cout << "Back element : \t"<< myList ->back ()<< endl;
14// Removing from the front
15 myList -> removeFront ();
16 cout <<*myList << endl;
17 myList -> removeFront ();
18 cout <<*myList << endl;
19 myList -> removeFront ();
20 cout <<*myList << endl;
21// Should be able to handle this
22 myList -> removeFront ();
23// Adding to the back
24 myList -> addBack ("Gollum");
25 cout <<*myList << endl;
26 myList -> addBack ("Bilbo Baggins ");
27 cout <<*myList << endl;
28 myList -> addBack (" Saruman ");
29 cout <<"1: "<<*myList << endl;
30 LinkedList * myList2= new LinkedList (* myList );
31 cout <<"2: "<<* myList2<< endl;
32 LinkedList * myList3= new LinkedList ();
33* myList3=*myList;
34 cout <<"3: "<<* myList3<< endl;
35 cout << "Front element : \t"<< myList ->front ()<< endl;
36 cout << "Back element : \t"<< myList ->back ()<< endl;
37// Removing from the back
38 myList -> removeBack ();
39 cout <<*myList << endl;
8
40 myList -> removeBack ();
41 cout <<*myList << endl;
42 myList -> removeBack ();
43 cout <<*myList << endl;
44// Should be able to handle this
45 myList -> removeBack ();
46 cout <<*myList << endl;
47 return 0;
48}
Program Output:
[]
1
[Gandalf]
[Aragorn]-->[Gandalf]
[Legolas]-->[Aragorn]-->[Gandalf]
Front element: Legolas
Back element: Gandalf
[Aragorn]-->[Gandalf]
[Gandalf]
[]
Removing the front of an empty linked list
[Gollum]
[Gollum]-->[Bilbo Baggins]
1: [Gollum]-->[Bilbo Baggins]-->[Saruman]
2: [Gollum]-->[Bilbo Baggins]-->[Saruman]
3: [Gollum]-->[Bilbo Baggins]-->[Saruman]
Front element: Gollum
Back element: Saruman
[Gollum]-->[Bilbo Baggins]
[Gollum]
[]
Removing the back of an empty linked list
[]
9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!