Data Structure in C++ I keep getting the same warning, and I cant seem to fix it
ID: 3800171 • Letter: D
Question
Data Structure in C++
I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong?
Warning:
dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)':
dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*'
dlist.cc: In function 'dlist operator+(dlist&, dlist&)':
dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+'
dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist'
My code:
#include "dlist.h"
dlist::node*dlist::at(int n){
node*current = head();
while(n != 0){
current= current->next;
n--;
}
return current;
}
void dlist::insert(node *previous, int value){
node*n = new node{value, previous -> next,previous};
previous -> next = n;
}
void dlist::del(node* which){
node*c= which -> next;
which -> next = which-> next-> next;
delete c;
}
void dlist::push_back(int value){
node*c = tail();
insert(c,value);
}
void dlist::push_front(int value){
insert(head(), value);
}
void dlist::pop_front(){
node*n = head();
n = head() -> next;
delete n;
}
void dlist::pop_back(){
node*n = tail();
n = tail() -> prev;
delete n;
}
int dlist::size(){
node*c = head();
int s = 0;
while(c){
c = c -> next;
s++;
}
return s;
}
bool dlist::empty(){
if(size() == 0)
return true;
else
return false;
}
std::ostream& operator<< (std::ostream& out, dlist& l){
dlist::node*c = l.head();
while(true){
out << c;
c = c-> next;
return c;
}
}
bool operator== (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
while(ca&&cb){
if(ca-> next != cb->next)
return false;
else{
ca = ca->next;
cb = cb->next;
}
}
if(ca== nullptr && cb == nullptr)
return true;
else
return false;
}
dlist operator+ (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
int result;
while(ca&&cb){
ca + cb = result;
ca = ca -> next;
cb = cb -> next;
}
return result;
}
dlist reverse(dlist& l){
dlist l2;
dlist::node* c = l.head();
while(c->next!=l.tail()){
l2.push_front(c->next->value);
c = c-> next;
}
return l2;
}
"dlist.h"
Explanation / Answer
See , what is needed first
Returns a new list consisting of all the elements of a, followed by all the
elements of b (i.e., the list concatenation).
the list concatenation : Iterate the first list then again iterate the secodn list , So resultant list will have 2n lements if both the list are of length n
dlist operator+ (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
int result;
while(ca&&cb){
ca + cb = result; ///Error , because u cannot add two address as well as you are assigning integer to it
ca = ca -> next;
cb = cb -> next;
}
return result; //Wrong , result is integer and return type is dlist
}
===========================================================================
How to solve it :
Algorithm
dlist::node* result = nullptr;
while(a ) {
insert (result , a->data );
a =. a->next;
}
while(b ) {
insert (result , b->data );
b =. b->next;
}
return result;
Do in this way, It will work ..You just need to attach all the nodes of a and b to result ,
Eg : if a contains 2,3,4,5 , , , if b contains 6,7,8, 9, ,
Then result should contain 2,3,4,5,6,7,8 , , ,
Thanks, let me know if there is any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.