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
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
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;     }​​​​​...
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...
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:...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can //--------------------one void NumberList::appendNode(double num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new listNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode. { // Find the last...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
I am having problem for the splitStrange function, and here is the feedback I get from...
I am having problem for the splitStrange function, and here is the feedback I get from the professor, but I am not sure how to do it.... Inside the loop, if the odd element is the first in the list(list_head) then you just call remove(); But if it's just another element in the list you need to delete it differently Like with using temp and pointers and delete //Strange.h template <typename ElementType> bool strange(const ElementType &element) { return true; }...
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()...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
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);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT