13772 - EE2310_Lec_13_2   

Description

Friend Classes

A friend class has access to all private members of another class. In this exercise, you are going to practice friend classes by re-implementing the linked list structure.

What You Need to Do

  1. Declare a class called Node that has two private data members: data and next.
  2. Declare another class called Linked_list that has two private data members: head and tail and four public member functions: output(), add_node(int), reverse(), as well as a default constructor.
  3. In the class Node, declare friend class Linked_list;
  4. Implement those four member functions of Linked_list, in which the add_node(int)function adds a node at the tail.
  5. You are given the following main function. You should make your class implementations compatible with the given main function. Do not change main().
  6. Submit everything.
int main(){
    Linked_list my_list;
    
    int data, input_size;
    /* when you add a node, you add it at the tail */
    
    cin >> input_size;
    
    for(int i=0; i < input_size; ++i) {
        cin >> data;
        my_list.add_node(data);
    }
    my_list.output();
    my_list.reverse();
    my_list.output();
    
    return 0;
}

DO NOT CHANGE MAIN, otherwise your work will not be regarded as correct even if you pass all test cases.  You may get PENALTY POINTS for changing main().

Sample I/O

Input1

5
1 2 3 4 5

Output1

1 2 3 4 5
5 4 3 2 1

Input2

9
3 98 34 9 1 4 8 8 54

Output2

3 98 34 9 1 4 8 8 54
54 8 8 4 1 9 34 98 3

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss