I have a code of doubly Linked list and there are few lines of that code which idea is not clear . I request for experts comments on the lines which have comment in the following .Its long since I last time used C++ . There are just two line which i indicated are not understandable for me.
template <typename T>
class double_linked
{
struct node
{
T data;
node* prev;
node* next;
node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
};
node* head;
node* tail;
public:
double_linked() : head( NULL ), tail ( NULL ) {}
template<int N>
double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL )
{
for( int i(0); i != N; ++i)
push_back(arr[i]);
}
bool empty() const { return ( !head || !tail ); } // this doing?
operator bool() const { return !empty(); } // this doing? i know operators need in C++ but dont know the use of it here
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~double_linked()
{
while(head)
{
node* temp(head);
head=head->next;
delete temp;
}
}
};
operator bool() const is a conversion operator. If an instance of double_linked is used in a context where a bool is required, this function will be called to do the conversion. (And will evaluate to true if the list is not empty in your case.)
The other function is a plain old function, which will return true if either head or tail is null.
For more about conversion operators, see: How do conversion operators work in C++?
The first is a function to determine if the list is empty. In a doubly linked list, if you have at least one element then the head and tail pointers (which point to the start and the end of the list respectively) must point to a valid element. Therefore, you can test whether the list is empty by testing if both those pointers do not point to a valid element (i.e. are null). That is what the expression !head || !tail does - checks if either pointer is null, and if so the list is empty.
The operator bool() thing is a conversion operator. It basically means, whenever the list is cast to bool, that function is called and the result used as the value of the bool. The function returns whether the list is not empty, so an expression like bool result = myList; will make result be true if the list is not empty.
This is a predicate telling us if it is empty or not, it is checking if the pointers are 0 (null).
bool empty() const { return ( !head || !tail ); } // this doing?
This allows the user to treat a instantiation of the container as a boolean, which is true if it is not empty.
operator bool() const { return !empty(); } // this
Implemented a linked list is a good programming excercise but if you want to use a linked list in your actual code then you should really use std::list (in ).
Related
I have realized Double Linked List. And now i need to overload == operator.
dll.cpp:67:17: error: expected expression
if ([ind] != sp[ind]) {
The problem i don't understand how to overload == operator if only one parameter is given. I mean if i write bool operator ==(DLL sp1, DLL sp2){} compiler says error: overloaded 'operator==' must be a binary operator (has 3 parameters)
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class DLL {
public:
DLL(){
size = 0;
head = nullptr;
tail = nullptr;
}
T operator [](int ind) {
int counter = 0;
T res = T();
Node *cur = this->head;
while(cur != nullptr) {
if(counter == ind) {
res = cur->data;
}
cur = cur->next;
counter++;
}
return res;
}
bool operator ==(DLL<int> sp){
bool isequal = true;
for(int ind = 0; ind < sp.length(); ind++){
if ([ind] != sp[ind]) {
isequal = false;
}
}
return isequal;
}
void clear() {
while(size != 1)
pop_front();
delete tail;
size--;
}
int length() {return size;}
}
private:
class Node{
public:
T data;
Node *next;
Node *prev;
Node(T data = T(), Node *prev= nullptr, Node *next = nullptr) {
this->data = data;
this->next = next;
this->prev = prev;
}
};
int size;
Node *head;
Node *tail;
};
The way you have it defined as a member function (and it only takes list of int for some reason (you should probably remove the <int>).
bool operator ==(DLL<int> sp); // There is a small issue in that you
// passing by value and thus causing a copy.
// Another issue with this is that it should
// probably marked "const" to indicate state
// is not changed by the call.
When the compiler see's this.
list1 == list2
This is simply syntactic sugar for:
list1.operator==(list2);
Thus is why you only need one parameter when you declare it as a member function. The alternative is to declare it as a friend function.
friend bool operator ==(DLL<T> const& lhs, DLL<T> const& rhs);
In this case it is a free standing function. When the compiler sees:
list1 == list2
This is syntactic sugar for:
operator==(list1, list2)
The problem was that you were defining a member function with two parameters. The left hand side is the class object and then you were expecting two objects on the right hand side (but the == operator only has one place on the right). That is why it is complaining about three parameters.
So the real question is should it be a member or a friend.
It does not matter here.
There are situations where it "can".
Example: If your class contains a single argument constructor (lets say you could create a list from an integer) and you use a member operator==()
DLL<int> list;
if (list == 5) {
}
This will now compile. Because your member operator uses a parameter and the compiler can convert an integer into the DLL parameter using a single argument constructor.
The counter argument to this is that normally you don't want automatic conversions of your type so you should mark single argument constructors as explicit to prevent this.
So:
If you class can automatically be created via a one argument constructor (most cases this is not true but it can be).
Then you should prefer a friend function version.
otherwise it does not matter and I would probably fall towards the member function.
You're doing the right thing for the most part.
The problem that is causing this error:
dll.cpp:67:17: error: expected expression if ([ind] != sp[ind]) {
is that you actually want to do this instead:
*this[ind] != sp[ind]
Also, there appears to be an extra } right here:
int length() {return size;}
} // <- not exactly sure what that's about, but I don't think you need it.
A comparison operator is a binary operator that treats both the operands equally, it is advisable to make it a friend function and not a member.
So the declaration for the function would change to
friend bool operator ==(const DLL<int> & lhs, const DLL<int> & rhs);
You can choose to define it inside or outside the class.
Read here on when you need to make an operator member vs non-member.
I have an iterator class nested in a LinkedList class. My question is how do I make the insert_after function using iterators. The rest of the code is given for information purposes, but the function I'm trying to get working is at the end.
Insert_After takes a position and inserts a value after it.
template <typename T>
class LinkedList : public LinkedListInterface<T> {
private:
struct Node {
T data; // data can be any type
Node* next; // points to the next Node in the list
Node(const T& d, Node* n) : data(d), next(n) {}
};
Node* head; // Is a pointer
class Iterator
{
private:
Node* iNode;
public:
Iterator(Node* head) : iNode(head){ }
~Iterator() {}
bool operator!=(const Iterator& rhs) const { return iNode != rhs.iNode; }
Iterator& operator++() { iNode = iNode->next; return *this; }
T& operator*() const { return iNode->data; }
};
/** Return iterator pointing to the first value in linked list */
Iterator begin(void) {
return LinkedList<T>::Iterator(head);
}
/** Return iterator pointing to something not in linked list */
Iterator end(void) {
return LinkedList<T>::Iterator(NULL);
}
/** Return iterator pointing found value in linked list */
Iterator find(Iterator first, Iterator last, const T& value) {
Iterator current = first;
bool found = false;
while (current != last) {
if (*current == value) {
return current;
}
++current;
}
return last;
}
Iterator insert_after(Iterator position, const T& value)
{
// Need help here
}
What I've tried so far resulted in a few errors.
Iterator insert_after(Iterator position, const T& value)
{
// Need to insert after position
Iterator previous = position;
++position;
Node* newNode = new Node(value, position);
previous->next = newNode;
}
The error I got was Error C2664 'function' : cannot convert argument n from 'type1' to 'type2' for the line
Node* newNode = new Node(value, position);
Compiler Error C2819 type 'type' does not have an overloaded member 'operator ->' for line
previous->next = newNode;
I understand the errors but I'm not sure how to work around them.
I think the short answer to your compiler errors is that you are likely supposed to pass a Node or Node* as your second argument and not an iterator. previous is also an iterator, and therefore does not have a next call.
Long answer below about generally fixing the function in question:
There's a lot [not] going on in that function, which gets me wondering about the rest of the linked list class as well. I've only looked at this function, as it's the one you claim is causing you trouble.
SUBJECTIVE THOUGHT I generally hate working with iterators in my class functions. Deal with the Nodes directly as much as possible. The iterator pattern exists for universal traversal of containers no matter how they're laid out, and that abstraction makes it a pain to deal with inside your class.
Iterator insert_after(Iterator position, const T& value)
{
// Need to insert after position
Iterator previous = position;
++position;
Node* newNode = new Node(value, position);
previous->next = newNode;
}
As it currently stands, if position is anywhere but the last element, you will break your list and leak memory. This is because you never check what is after position. That first mistake leads into the second. newNode->next never gets set. Maybe it's default constructed to nullptr, that's fine. But if I'm inserting into the middle of my list, I need to connect newNode to whatever came after position originally.
Another question that you need to consider is "what if this is called on an empty list?" Does your begin() function handle that? Is it supposed to throw?
Iterator insert_after(Iterator position, const T& value)
{
Node* pos = position.iNode;
if (!pos) { // assumes an empty list if this is true
// Correctly build first Node of your list and get everything assigned
// that you can
// return the new iterator;
// or just throw
}
if (!pos->next) { // position at end of list if true
pos->next = new Node(value, position); // Is that second argument
// supposed to be an iterator?
return Iterator(pos->next);
} else {
// Some of this is probably redundant depending on how you are actually
// building Nodes, but the gist is it's important to ensure the list is
// not broken; connecting tmp before changing existing nodes helps the
// list stay intact for as long as possible
Node* tmp = new Node(value, position);
tmp->next = pos->next;
pos->next = tmp;
return Iterator(tmp);
}
A doubly linked list never seems attractive at first blush to a student, but it makes certain operations like erasing from the middle of the list so much easier. Yes, you have one extra pointer to deal with, but it makes it harder to lose Nodes as well. Along with bi-directional iteration.
I am trying to create an appendToTail function which will add a node to the end of a singly linked list.
I am having trouble in adding a node if the head is NULL(the linked list is empty)
class Node {
private:
Node* next;
int data;
public:
Node(int d, Node* n = NULL)
: data(d)
, next(n)
{
}
void appendToTail(int);
//other trivial functions(getters and setters etc. ) defined and
//declared
};
void Node::appendToTail(int d)
{
Node* end = new Node(d);
Node* n = this;
if (n == NULL)
n = end;
else {
while (n->next != NULL)
n = n->next;
n->next = end;
n->next->next = NULL;
}
end = NULL;
delete end;
}
int main()
{
Node* n = NULL;
n->appendToTail(5);
std::cout << n->getData(); //getData() is a function which
//retrieves the Data member variable
}
I am expecting to get 5 but I am getting an error which appears to be caused because my node remains null.
Now with modern C++ idioms we use smart pointers instead of raw pointers, it gives you the benefit of RAII (Resource acquisition is initialization) mechanism. In addition if you want an elegant solution to your problem you should introduce a List class with which you can express more clearly the concept of an empty list. It would give something like this:
#include <memory>
#include <iostream>
class List
{
public:
class Node
{
private:
std::shared_ptr<Node> next;
int data;
public:
Node(int d):next(nullptr),data(d){}
inline int getData() const {return data;}
inline std::shared_ptr<Node> getNext() const {return next;}
friend List;
};
List():head(nullptr),tail(nullptr){}
void appendToTail(int );
inline std::shared_ptr<Node> getHead() const {return head;}
inline std::shared_ptr<Node> getTail() const {return tail;}
private:
std::shared_ptr<Node> head;
std::shared_ptr<Node> tail;
};
void List::appendToTail(int d)
{
auto newTail = std::make_shared<Node>(d);
if (head == nullptr)
{
head = tail = newTail;
}
else
{
tail->next = newTail;
tail = newTail;
}
}
int main()
{
List l;
l.appendToTail(5);
std::cout<<l.getHead()->getData();
}
But you should definitely prefer std::list<T> or std::vector<T>.
Unfortunately there several errors with your approach. Semantic errors and a logical error with your interpretation of a linked list. Let's start with your initial misunderstanding. You cannot add a new tail to an empty list. Because it is emtpy. Meaning, not yet existing. Only if some object is existing/instantiated you can add a tail. You cannot add something to not existing stuff. So your idea to start with a Node* n = nullptr cannot work logically.
Additionally you are dereferencing a nullptr (major bug). That is also the main problem of your code. Nothing works. You need an instantiated object, before you can call it's member functions.
So before you can populate the list, you need to create/instantiate it initially. So you need to explicitly create the first node in your main function with
Node* n = new Node (5)
Then the list is existing and from now on you can add new members with calling appendToTail.
There are more semantic errors in your code which have luckily no side effects.
You must not delete the 'end' variable in your function. You want to keep the newly allocated memory for the new tail. But you introduced an additional sematic error by setting 'end' to nullptr and then call delete. Deleting a nullptr is a noOp and will do nothing. So, although you have a semantic error, this will not cause any trouble.
There is more:
For a pointer to Null you should always use nullptr.
And, your
if (n == NULL)
is always false. Before that, you assigned this to n. This is never NULL. You can delete the if else. Keep the statements from the else, except the
n->next->next = NULL;
That's not necessary. The constructor did that already for you. As explained, the next 2 statements should also be elimanted.
Additionally you may want to read a little more on the concept of linked lists.
I hope I could help a little
I want to check, whether a linked list in C++ is empty or not. I have following class:
class IntLinkedList
{
private:
struct LinkedListNode // Structure for linked list
{
int value;
struct LinkedListNode *next;
};
LinkedListNode *head; // List head pointer
public:
IntLinkedList(void) // Constructor
{ head = NULL; }
~IntLinkedList(void); // Destructor
void AppendNode(int);
void InsertNode(int);
void DeleteNode(int);
void DisplayList(void);
bool isEmpty(LinkedListNode*);
};
// isEmpty function
bool IntLinkedList::isEmpty(LinkedListNode *node)
{
bool status;
node = head;
if ( node->next == NULL )
status = true;
else
status = false;
return status;
}
But I can't use this function in other class via an object of the same class.
How can I check the empty list using a function that would be accessible in another class through the object of the same class?
The error you are getting is caused by the fact that you declared your function as bool isEmpty(LinkedListNode) but you are trying to define it as bool isEmpty(LinkedListNode*). The difference is that in the definition you have a pointer, while in the declaration there is just an object. You have to pick one, as these are completely different things.
That said, I don't see why you need the argument at all to check whether your list is empty. Just drop the argument altogether and use if ( head->next == NULL ) - non-static member functions are always called through an instance of the class.
Just for completeness, first item in your list is pointed to by head, so in order to check if there is anything in the list, you should check if it is a null pointer:
bool IntLinkedList::isEmpty() const
{ //added const for const-correctness, should be added to declaration as well
return head == nullptr;
}
Following list.empty(),
Returns whether the list container is empty (i.e. whether its size is
0).
Two suggestions:
Have a size variable that checks for the number of nodes in your list, this way your isEmpty() is just return size == 0;
Or in your current implementation, just modify to:
bool isEmpty() {
return head == null; // if head is null, there's no node in list
}
I've got a linked list where I save data, and a pointer to next node, Node<T>* next, like this:
template <class T>
struct Node
{
T data;
Node<T>* next;
};
The thing is I want to put in this a post-increment operator, so it returns the previous value of my node, but increment the reference. So if I do this
Node<int>* someNode = someList.SomeNode();
Node<int>* tmp = someNode++;
tmp would be the original someNode value, but someNode would be someNode->next.
is it possible to put an operator in the struct? I've tried to, and searched how to do it, but as I don't deal with operators I don't know how to do.
You cannot add member function to basic type like pointer.
What are you trying to define is an iterator. Use wrapper class over your node pointer to succeed:
template <class T>
struct NodeIterator
{
NodeIterator(Node<T>* current) : current(current) {}
NodeIterator& operator ++() { current = current->next; return *this; }
NodeIterator operator ++(int) {
NodeIterator retVal = *this;
++(*this);
return retVal;
}
T* operator-> () const { return ¤t->data; }
T& operator * () const { return current->data; }
Node<T>* current;
};
See std::slist<> implementation for references. Look at template<typename _Tp> struct _List_iterator. Reading STL implementation is better than many books.
Usage:
NodeIterator<T> it = &node;
++it;
T& t = *it;
Node<T>& operator++(int) {…}
is the member you want to implement.
For your code to work, you'd need to be able to define operator++ for your pointer class. That's not allowed, though. You're welcome to define some other named function, though. For example:
template <typename Node>
Node goto_next(Node& node) {
Node result = node;
node = node->next;
return result;
}
Then you can use it like this:
Node<int>* tmp = goto_next(someNode);
Another option is to provide a real iterator class instead of just using a pointer:
Node<int>::iterator someNode = someList.begin();
Node<int>::iterator tmp = someNode++;
Make your iterator keep a Node<T>* member, and make the ++ operator update that internal pointer before it returns a copy of the iterator object.
You really don't want to do that. The idea of using ++ on a pointer is dangerously close to the common iterator pattern. You should just go the full distance and make a real iterator class. Think of std::list<T>::iterator.
Iterators are very lightweight wrappers to give a sensible interface to a node pointer, which provides things like operator ++ to move to the next node, and overloads operator -> to provide simple access to the node data. Converting client code from using a pointer to using an iterator is very straight-forward because the syntax is almost identical.