Several implementation for a linked list - C++ - c++

I have written a simple LinkedList class. I first have a Node class:
class Node
{
public:
Node* next;
int value;
Node(int val)
{
value = val;
next = NULL;
}
Node(int val, Node* y)
{
value = val;
next = y;
}
}
then implementation for LinkedList is straightforward, with a Node* head member and a addNode(int value) member function.
What are other methods to implement a linked list? could give other such implementations or hint at relevant doc?
Thanks and regards.

The standard library defines a doubly-linked list implementation you can use (see here, for example). I'd advise using that unless you have a very good reason not to.

Boost has some implementations:
http://www.boost.org/doc/libs/1_51_0/doc/html/intrusive/slist.html
http://www.boost.org/doc/libs/1_51_0/doc/html/intrusive/list.html

deleteNode
findNode
Mabe create an iterator.
Also better to use initialisation lists in the constructors and private data members. NULL is for C, use 0 instead.
i.e.
class Node
{
private:
Node* next;
int value;
public:
Node(int val) : next(0), value(val) {}
Node(int val, Node *n) : next(n), value(val) {}
int getVale() { return value}
};

Related

Passing a linked list to a function and making sure it is not modified

Is there a way to pass a linked list to a function and ensuring that it is not modified?
We can pass a const head pointer to a function, and this will make sure that the head is not modified. However, the function could access other nodes from head and modify those.
May be you want to try something like this:
class Node{
private:
Node* _next;
public:
Node(Node* next) : _next(next){}
Node* getNext(){ return _next; }
const Node* getNext() const {return _next; }
};
p.s. IMHO. C++ prorammers very often ignore references and use pointers in places they not needed. May that be an option for your case? :)
struct Node{ Node& _next; Node(Node& next) : _next(next){} };
PP.SS. The second getNext may be not needed in your concrete case. It is just to make the life easier if you have functions taking const node pointers. In the example below I will try to clear the idea with const-method a bit more :
#include <iostream>
#include <cstdlib>
class Node{
private:
Node* _next;
public:
Node(Node* next) : _next(next){}
Node* getNext(){ std::cout << "getNext\n"; return _next; }
const Node * getNext() const { std::cout << "getNext const\n"; return _next; }
};
void f1(Node* node){ node->getNext(); }
void f2(const Node* node){ node->getNext(); }
int main() {
Node* n1 = new Node(NULL);
Node* n2 = new Node(n1);
f1(n2);
f2(n2);
}
if you want to traverse through the linkedList Without changing it,
just overload your traversal functions with constant ones.

Different type of Node created

On geeks for geeks I saw a different way to create the Node for linked list.
struct Node{
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
}
Can someone please explain me how that node is defined.
struct Node {
int data;
Node *next;
Node(int x) : data(x), next(NULL) {}
};
This is just a way to define structure with constructor in C++
You can use them simply like this
Node *node = new Node(4);

Overload operator>> to delete a node from a binary tree

I have the following classes:
class Node
{
private:
char* m_key;
Node* left;
Node* right;
public:
Node() : m_key(nullptr), left(nullptr), right(nullptr) {}
Node(const char* key)
{
this->m_key = new char[strlen(key) + 1];
strcpy_s(this->m_key, strlen(key) + 1, key);
left = nullptr;
right = nullptr;
}
friend class BinSTree;
};
class BinSTree
{
private:
Node* root;
public:
BinSTree() : root(nullptr) {}
friend std::fstream& operator>>(std::fstream& in, Node* p);
Node* deleteNode(Node* p, const char* key);
~BinSTree();
};
I want to overload the operator>> so when I execute the following code :
Node test("Key");
BinSTree bst;
bst>>test;
the node test is deleted from the bst. The problem is that I can't access the private members from Node and also can't access members of class BinSTree. BinSTree is a class that contains the root of a binary tree.Node is a class that represents a node.
You don't have access to members of BinSTree because first of all your operator>> does not operate on BinSTree at all - it operates on std::fstream& and Node*.
What your code provides is a function for "extracting" Node*s from a std::fstream - it has no relation whatsoever with BinSTree.
The correct signature for the operator you want is
void operator>>(BinSTree& tree, Node* p)
or
BinSTree& void operator>>(BinSTree& tree, Node* p)
(the latter would allow you to chain node extractions).

Hash Table in C++ Through Custom Struct and Linked List Classes

I'm trying to create a spell checking program in C++ by reading in a dictionary from a .txt file. I've got the read in function working perfectly fine, the issue I'm coming across is when I try to navigate and add to my linked list.
When I try to set the pointer of the newest node to add, to the value of the head pointer, I'm getting an error stating No viable conversion from 'Node' to 'Node *'.
What is the best way to perform this conversion.
I've already tried turning my 'Node Head;' inside of my linked list class to a pointer but receive the same error.
To start I created my Node struct (Declared in a header file)
struct Node
{
private:
std::string word;
Node *nextNode;
public:
//Default constructor
Node();
~Node();
//My Setters and getters for the class
void setWord(std::string _word) { word = _word; }
std::string getWord() { return word; }
void setNode(Node *_nextNode) { nextNode = _nextNode; }
Node getNode() { return *nextNode; }
};
Followed by my LinkedList Class (Also declared in a Header file)
class LinkedList
{
private:
Node head;
int listSize;
public:
LinkedList();
~LinkedList();
void setListSize(int _listSize) { listSize = _listSize; }
int getListSize() { return listSize; }
void setHead(Node _head) { head = _head; }
Node getHead() { return head; }
//Function that adds the next node to the head
void addToHead(LinkedList &myList, Node &myNode);
};
Heres my Function
void LinkedList::addToHead(LinkedList &myList, Node &myNode)
{
myNode.setNode(myList.getHead().getNode());
//Here is where I'm getting my error
//"No viable conversion from 'Node' to 'Node *'
myList.setHead(myNode);
}
The LinkedList class shouldn't own the first Node.
The member head should be a Node* width default value nullptr (the list is empty).
listSize should also have a default value assigned.
LinkedList() head(nullptr), listSize(0) {};
Edit
Personally I would avoid to force the external code to manage the single nodes.
Keep an implementation independent interface.
class LinkedList
{
private:
Node *head_;
int size_;
public:
LinkedList();
~LinkedList();
int size() const { return listSize; }
// insert after the i-th element
void insert(std::size index, std::string const& word);
// return the i-th element
std::string &at(std::size index);
std::string const &at(std::size index) const;
// removes the i-th element
void remove(size::size index);
};
In this way you centralize all list manipulation code into the LinkedList class.
You should also consider problems related to copying a LinkedList object.

Differences between struct and class and constructing a node for double linked lists?

I'm attempting to create my own double linked list for learning experience. My book showed the node struct below and I was wondering is that equivalent to my Node class I created? Is that function in the struct just a type of constructor assigning values to each data type in the struct?
//===== Struct =====
struct Node
{
Node *next;
Node *prev;
std::string val;
Node(const std::string &value, Node *nextVal = NULL, Node *prevVal = NULL) :
val(value), next(nextVal), prev(prevVal) {}
};
//===== Class ====
class Node
{
public:
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL);
virtual ~Node(void);
protected:
Node *next;
Node *prev;
std::string val;
};
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL)
{
next = nVal;
prev = pVal;
val = value;
}
Yes - that's exactly what it is.
Here's a page with an example of a struct constructor.
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++Structures.html
This is called a constructor initializer list and it is meant to initialize the attributes of a struct or a class.
This is usually the preferred way of initializing attributes. here's a discussion explaining why:
Is it possible to defer member initialization to the constructor body?
Long story short, if you don't explicitly initialize an attribute in the initializer list, it is initialized implicitly using the default constructor and therefor, you will be initializing the variable twice.
Also, you need accessors for your pointer.
class Node
{
public:
Node():next(NULL),prev(NULL),val("") {};
Node(std::string value):next(NULL),prev(NULL),val(value) {};
Node(std::string value, Node *pVal):next(NULL),prev(pVal),val(value) {};
Node(std::string value, Node *pVal, Node *nVal):next(nVal),prev(pVal),val(value) {};
virtual ~Node(void);
std::string getValue()
{
return val;
}
void setValue(std::string v)
{
val = v;
}
Node * getNext()
{
return next;
}
void setNext(Node * n)
{
next = n;
}
Node * getPrevious()
{
return prev;
}
void setPrevious(Node * n)
{
prev= n;
}
protected:
Node *next;
Node *prev;
std::string val;
};