Question

Data Structure in C++ I keep getting the same warning, and I cant seem to fix...

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"

#pragma once
/*
  dlist.h
  Doubly-linked lists of ints
 */
#include 

class dlist {
  public:
    dlist() { }

    // Implement the destructor, to delete all the nodes
    ~dlist();

    struct node {
        int value;
        node* next;
        node* prev;
    };

    node* head() const { return _head; }
    node* tail() const { return _tail; }

    // **** Implement ALL the following methods ****

    // Returns the node at a particular index (0 is the head).
    node* at(int);

    // Insert a new value, after an existing one
    void insert(node *previous, int value);

    // Delete the given node
    void del(node* which);

    // Add a new element to the *end* of the list
    void push_back(int value);

    // Add a new element to the *beginning* of the list
    void push_front(int value);

    // Remove the first element
    void pop_front();

    // Remove the last element
    void pop_back();

    // Get the size of the list
    int size();

    // Returns true if the list is empty (size == 0)
    bool empty();

  private:
    node* _head = nullptr;
    node* _tail = nullptr;
};

// **** Implement ALL the following functions ****

/* out << l
   Prints a list to the ostream out. This is mostly for your convenience in
   testing your code; it's much easier to figure out what's going on if you
   can easily print out lists!
*/
std::ostream& operator<< (std::ostream& out, dlist& l);

/* a == b
   Compares two lists for equality, returning true if they have the same
   elements in the same positions. (Hint: it is *not* enough to just compare
   pointers! You have to compare the values stored in the nodes.)
*/ 
bool operator== (dlist& a, dlist& b);

/* a + b
   Returns a new list consisting of all the elements of a, followed by all the
   elements of b (i.e., the list concatenation).
*/
dlist operator+ (dlist& a, dlist& b);

/* reverse(l)
   Returns a new list that is the *reversal* of l; that is, a new list 
   containing the same elements as l but in the reverse order.
*/
dlist reverse(dlist& l);

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT