Overloading Operator << Linked List

Hello, I am trying to overload the oprator "<<", but I dont know what i am doing wrong, if someone can help me please, I will be grateful.

This is my overloading declaration, each part:
friend ostream &operator<<( ostream&, const OrderedList& );

1
2
3
4
5
6
7
8
9
10
11
12
13
OrderedList::node* OrderedList::search(int value){
    node *p = new node;
    p = first;
    while(p != NULL){
            if(p->data == value){
                       cout <<"here";
                       return p->link;
                       }
            p = p ->link;
            }
    cout << "The element searched has not been found." << endl;
    
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>

using namespace std;

class OrderedList{
      friend ostream &operator<<( ostream&, const OrderedList& );
      
      private:
              struct node{
                     int data;
                     node *link;
                     };
              node *first, *last;
              int count;
      public:
             OrderedList();
             ~OrderedList();
             void insert(int);
             void display();
             int remove(int);
             node* search(int);
             //<<
      
};

OrderedList::OrderedList(){
                           
                           first = NULL;

}

OrderedList::~OrderedList(){
                            while(first != NULL){
                                      node *destruct;
                                      destruct = first;
                                      first = first -> link;
                                      delete destruct;
                            }
                            delete first;
                            cout << "Destructor called";                        
}

void OrderedList::insert(int value){

                    node *NewNode = new node;
                    NewNode -> data = value;
                    NewNode -> link = NULL;
                    
                    node *p = new node;
                    p = first;
                    
                    node *before = p;

                    if(first == NULL){
                            first = NewNode;
                                 }
                    else{
                         while(p != NULL){
                                 
                                 if(value < first->data){
                                            NewNode -> link = first;
                                            first = NewNode;
                                            break;
                                            }
                                 else if(p -> data >= value){
                                      NewNode -> link = before -> link;
                                      before -> link = NewNode;
                                      break;
                                      }
                                      before = p;
                                      p = p -> link;
                                 if(p == NULL){
                                      before -> link = NewNode;
                                      }
                                 }
                         
                         
                         }
}
void OrderedList::display(){
     node *p = new node;
     p = first;
          while(p != NULL){
                  cout << p -> data << " ";
                  p = p -> link;
                  }
          cout << endl;
}

int OrderedList::remove(int value){
    node *p = new node;
    p = first;
    node *before = p;
    while(p != NULL){
            if(first->data == value){
                           before = first;
                           first = first ->link;
                           delete before;
                           break;
                           return 0;
                           }
            before = p;
            p= p->link;
            if(p->data == value){
                       before->link = p->link;
                       delete p;
                       break;
                       return 0;
                       }
            
            }
    return -1;
}

OrderedList::node* OrderedList::search(int value){
    node *p = new node;
    p = first;
    while(p != NULL){
            if(p->data == value){
                       cout <<"here";
                       return p->link;
                       }
            p = p ->link;
            }
    cout << "The element searched has not been found." << endl;
    
    }

OrderedList::ostream &operator<<( ostream &output, const OrderedList& Olist){
        node *p;
        for(p = Olist; p != 0; p = p -> link){
              output << p -> data << endl;
}
        
        return output;
}
        

int main(){
    OrderedList list;
    list.insert(-1);
    list.insert(7);
    list.insert(2);
    list.insert(1);
    list.insert(6);
    list.insert(-9);
    
    list.display();
    list.remove(7);
    list.remove(-9);
    list.remove(1);
    
    cout << endl << list.search(7);
    
    cout << list;
    
    system("pause");
    return 0;
    }
Last edited on
OrderedList::ostream &operator<<( ostream &output, const OrderedList& Olist){
MiiNiPaa,
If I do what you are telling me, this is what happens, I get this errors:

In function `std::ostream& operator<<(std::ostream&, const OrderedList&)':
`node' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

`p' undeclared (first use this function)

`Olist' undeclared (first use this function)
Use qualified names:
OrderedList::node* p;

Also for(p = Olist; p != 0; p = p -> link){ should probable be for(p = Olist.first; p != 0; p = p -> link){
YES IT WORKS!!!

THANKS YOU VERY MUCH MiiNiPaa!!
Last edited on
Topic archived. No new replies allowed.