error: lvalue required as left operand of assignment tail->getnext() = a; - c++

My code is below as well as the description of my problem:
class node
{
public:
node(float val1, node* next1)
: val(val1), next(next1) { }
node(int val1)
:val(val1), next(NULL) { }
node() { }
float getvalue() { return val;}
node* getnext() { return next;}
private:
float val;
node* next;
};
class linkedlist
{ public:
linkedlist(): head(NULL), tail(NULL) { }
node* addnode(node* a)
{
tail->getnext() = a;
tail = a;
}
float printlist()
{
node* tmp = head;
while(tmp!=NULL)
{
std::cout << tmp->getvalue() << std::endl;
tmp = tmp->getnext();
}
}
private :
node* head;
node* tail;
};
int main()
{
}
Could anyone help me fix the error? I just want to set tail->next = a, but
as next is a private value, I can't do this. I've been using tail->getnext() = a, but then the error occurs. Why does the error occur?
Thanks!

node::genNext () returns a pointer an it is an R-value, so you cannot assign anything to it.
To do this, you have to return a reference to the pointer, which is a modifiable L-value:
node*& getnext() { return next;}

Related

Data insertion and deletion speed of queue in STL of c++

I have question about insertion and deletion speed of queue in c++ STL.
When i tried to slove algorithm question using Dequeue i made, i face running time out problem.
So i think my Dequeue is so slow, and i want to know what is the difference between my Dequeue and queue in c++ STL.
Here is my queue code.
Please give me some advice.
In this code, i suppose value in Node class can't have negative number.
class Node
{
private:
int value;
Node* prev;
Node* next;
public:
Node();
Node(int value);
Node(int value, Node* next);
~Node();
void setValue(int value);
void setPrev(Node* prev);
void setNext(Node* next);
int getValue();
Node* getPrev();
Node* getNext();
};
Node::Node()
: value(-1), prev(nullptr), next(nullptr)
{
}
Node::Node(int value)
: value(value), prev(nullptr), next(nullptr)
{
}
Node::Node(int value, Node* next)
: value(value), prev(nullptr), next(next)
{
}
Node::~Node()
{
}
void Node::setValue(int value)
{
this->value = value;
}
void Node::setPrev(Node* prev)
{
this->prev = prev;
}
void Node::setNext(Node* next)
{
this->next = next;
}
int Node::getValue()
{
return this->value;
}
Node* Node::getPrev()
{
return this->prev;
}
Node* Node::getNext()
{
return this->next;
}
class Dequeue
{
private:
Node* front;
Node* back;
public:
Dequeue();
~Dequeue();
void pushFront(int value);
void pushBack(int value);
void popFront();
void popBack();
int getFront();
int getBack();
int getSum();
};
Dequeue::Dequeue()
{
this->back = new Node(-1, nullptr);
this->front = new Node(-1, this->back);
this->back->setPrev(front);
}
Dequeue::~Dequeue()
{
}
void Dequeue::pushFront(int value)
{
Node* node = new Node(value, this->front->getNext());
node->setPrev(this->front);
this->front->setNext(node);
node->getNext()->setPrev(node);
}
void Dequeue::pushBack(int value)
{
Node* node = new Node(value, this->back);
node->setPrev(this->back->getPrev());
this->back->setPrev(node);
node->getPrev()->setNext(node);
}
void Dequeue::popFront()
{
if (this->front->getNext() == this->back)
return;
Node* node = this->front->getNext();
this->front->setNext(node->getNext());
node->getNext()->setPrev(this->front);
node->setNext(nullptr);
node->setPrev(nullptr);
delete node;
}
void Dequeue::popBack()
{
if (this->back->getPrev() == this->front)
return;
Node* node = this->back->getPrev();
this->back->setPrev(node->getPrev());
node->getPrev()->setNext(this->back);
node->setNext(nullptr);
node->setPrev(nullptr);
delete node;
}
int Dequeue::getFront()
{
if (this->front->getNext() == this->back)
return -1;
return this->front->getNext()->getValue();
}
int Dequeue::getBack()
{
if (this->back->getPrev() == this->front)
return -1;
return this->back->getPrev()->getValue();
}
You Dequeue is implemented via a linked list, where nodes are allocated/deallocated in each push/pop operation. std::queue is implemented via a std::deque, which is much more efficient (it allocates only once a while).
Linked lists are good if you need to insert in the middle, but this is not your case. std::deque is basically a dynamic sequence of fixed-size arrays.
Relevant questions:
Why does std::queue use std::dequeue as underlying default container?
Which STL container should I use for a FIFO?

Segmentation fault (C++ pointers)

I'm trying to implement a linked list class in C++ using a node class and a pointer to the next node.
template<class T>
class Node {
public: T val;
public: Node* next;
public: Node() { }
public: Node(T val) { this.val = val; }
};
I created a linked list class with a pointer to the head and a function append to add nodes to the list:
template<class T>
class LinkedList {
private: Node<T>* head;
public: LinkedList() { }
public: LinkedList(T val) { head -> val = val; }
public: void append(T val) {
Node<T>* temp = head;
while(temp -> val != NULL) {
temp = temp -> next;
}
temp -> val = val;
}
};
but after compilation and running, it throws this error "Segmentation fault (core dumped)"
I know a little about segmentation fault like trying to access a memory location that is no longer existed but I don't get it in this example, any help??
The main problem is that you don't initialize the next pointer with null pointer, but you assume it is null pointer by default. Here's how you fix it:
template <class T>
class Node {
public:
T val;
Node* next;
Node( T val_ = {} )
: val{ val_ }
, next{ nullptr }
{ }
};
Besides that, in the append there are problems. Here's how the code should have looked like completely:
template <class T>
class Node {
public:
T val;
Node* next;
Node(T val_ = {}) : val{ val_ }, next{ nullptr } {}
};
template <class T>
class LinkedList {
private:
Node<T>* head;
public:
LinkedList(T val_ = {}) : head{ new Node{val_} } {}
void append(T val) {
Node<T>* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new Node<T>{ val };
}
};
int main() {
LinkedList<int> l;
l.append(10);
}
Other comments:
this.val won't compile, because this is a pointer. You meant this->val.
You should consider using member initializer lists instead of this->val = val.
Initialize all members in all constructors.

How to return an object of a class whose object is parameter inside another struct

If I have a class structure such as:
class Data
{
public:
Data(int a, int b) : mem_a(a), mem_b(b) { };
private:
int mem_a;
int mem_b;
}
class UseData
{
public:
UseData() { };
Data* returnDataObj(int c) { return DataObj; }
private:
struct node
{
Data dat;
node* next;
node(const Data aData) : dat(aData), next(NULL) { };
}
node** table;
}
Inside returnDataObj, I have somthing that looks like:
Data* UseData::returnDataObj(int c)
{
node* head = table[c];
if(head == NULL)
return NULL; //<-- No issues here
else
return head;// I get an error on this line - return type does not match;
}
That was expected since head is of type node. Is there a way I can return type Data from returnDataObj ?
You method should be updated to return &head->dat, which is of the same type that the method is declared to return.
Data* UseData::returnDataObj(int c)
{
node* head = table[c];
if(head == NULL)
return NULL;
else
return &head->dat;
}
Currently you are trying to return just head which is of type node*. Clearly you can see the type mismatch.

Error: request for member ... in ... which is of non-class type

Just started learning c++ for a class, I can't figure out what is wrong with this code! I'm making a stack class with a helper class nested inside it called node that acts as a linked list. The error I'm getting is on line 12 and is:
Stack.cpp: In destructor ‘Stack::~Stack()’:
Stack.cpp:12:24: error: request for member ‘getNext’ in ‘((Stack*)this)->Stack::node’, which is of non-class type ‘Stack::Node*’
Here's my code:
#include "Stack.h"
Stack:: Stack ()
{
height = 0;
node = 0;
}
Stack:: ~Stack()
{
while(node != 0){
Node *next = *node.getNext();
delete node;
node = next;
}
node = 0;
}
And Here's my header file:
using namespace std;
class Stack
{
private:
int height;
class Node{
private:
int data;
Node* next;
public:
void setData(int x){
data = x;
}
void setNext(Node* x){
next = x;
}
int getData(){
return data;
}
Node* getNext(){
return next;
}
};
Node* node;
public:
Stack();
~Stack();
void push(int x);
int pop();
int peek();
int getHeight();
bool isEmpty();
};
Node *next = *node.getNext();
should be
Node *next = (*node).getNext();
Since . operator has higher precedence than * deference operator.
You can also use:
Node *next = node->getNext();

c++ doubly linked list with null object model

I'm trying to create a doubly-linked list with the null object model. So far, I've implemented a method to add a node to the beginning of the list and a method to display the node. My problem is that the display function always displays 0. Can anyone point out where I've gone wrong and how to fix it? Also, am I on the right track to correctly implementing the null object model here?
Note: This is a school assignment. Please don't just post a solution without an explanation. I want to learn and understand what's going on here.
Edit: After fixing the display problem, I have another: When calling getHead() or getTail() with a list that is empty or has nodes, it keeps wanting to use self() from the node class, rather than the nullNode class (in the event of an empty list) or elementNode class (in the event of a list with nodes). I'm stuck on how to fix this.
If I print out the addresses of container.getNext() and container (for an empty list), both addresses are the same so shouldn't adding ->self() to the end call the self() method from the nullNode class?
class node {
public:
node(){/* Do nothing */}
node(int e){ element = e; }
int getData(){ return element; }
void setData(int e){ element = e; }
friend class list;
protected:
node* getNext(){ return next; }
void setNext(node* n){ next = n; }
node* getPrev() { return prev; }
void setPrev(node* n){ prev = n; }
node* self();
private:
int element;
node* next;
node* prev;
};
class nullNode : public node{
public:
nullNode(){/* Do nothing */}
int getData(){ return NULL; }
void setData(int e){ /* Do Nothing */ }
node* getNext(){ return head; }
void setNext(node* n){ head = n; }
node* getPrev() { return tail; }
void setPrev(node* n){ tail = n; }
node* self(){ return NULL; }
private:
node* head;
node* tail;
};
class elementNode : public node{
public:
elementNode(){/* Do nothing */}
elementNode(int element){
setData(element);
}
int getData(){ return node::getData(); }
void setData(int e){ node::setData(e); }
node* getNext(){ return node::getNext(); }
void setNext(node* n){ node::setNext(n); }
node* getPrev() { return node::getPrev(); }
void setPrev(node* n){ node::setPrev(n); }
node* self(){ return this; }
};
class list{
public:
list();
node* getHead(){ return (container.getNext())->self(); }
node* getTail(){ return (container.getPrev())->self(); }
node* addHeadNode(int e);
void removeNode(node* n);
void insertBefore(node* n, int e);
void insertAfter(node* n, int e);
void displayNode(node *n);
private:
nullNode container;
};
list::list()
{
container.setNext(&container);
container.setPrev(&container);
}
node* list::addHeadNode(int e)
{
node* foo = new elementNode(e);
foo->setPrev(&container);
foo->setNext(container.getNext());
container.getNext()->setPrev(foo);
container.setNext(foo);
return foo;
}
void list::displayNode(node* n)
{
cout << "Node Data: " << n->getData() << endl;
}
int main()
{
list myList;
node* myNode;
myNode = myList.addHeadNode(5);
myList.displayNode(myNode);
return 0;
}
elementNode(int element)
{
node e;
e.setData(element);
}
What is this code doing? You create node e, but it appears to then be thrown away and not added to any list.
The problem hides in
elementNode(int element){
node e;
e.setData(element);
}
What is going on here? First you create an instance of the node class and then call its setData member function. Sure enough e is modified with the value of element but the very next moment both e and element are vanished out of existence because the scope where they were initialized has ceased to its end (terminated by }) while the information in element hasn't been saved anywhere.
However, if you replace the above code with
elementNode(int element){
setData(element);
}
it calls the inherited setData member function, the value of element is saved, and the program outputs 5 as expected.
Your elementNode constructor is trying to initialize it's node part:
elementNode(int element){
node e;
e.setData(element);
}
You actually just construct an unrelated node then discard it.
What you want is to call your superclass constructor, which can be done in the subclass constructor's initialization list:
elementNode(int element) : node(element) {
}