What does Node * head outside of the struct do? - c++

So I have the class LinkedList. Can someone explain why node * head is outside of the struct and node * next, prev are inside the struct? What is the purpose of this and how does it differ from the others? thanks
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
struct node
{
char data;
node * next;
node * prev;
};
node *head;
};

This
struct node
{
char data;
node * next;
node * prev;
};
is an inner declaration of a structure type within a class.
This
node *head;
is a declaration of a data member of the type struct node * of the class LinkedList. To make it more clear rewrite the list definition the following way
struct node
{
char data;
node * next;
node * prev;
};
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
node *head;
};
Declaring the structure inside the class definition as a private declaration makes the structure type invisible for the users of the linked list.

Related

Defining a struct within a class (doubly linked list) using a template

I have implemented each of the methods for this class but I am struggling with this last error. I was given instructions to define the Node struct within the private section of the class linkedlist. I get errors such as:
"error: Node is not a class template"
and
"error: non-template type 'Node' used as a template"
My code works if I rearrange things and place the Node struct outside of the class altogether, but that is not really the solution I am looking for.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
typedef int element_type;
template<class element_type>
class linkedlist
{
private:
struct Node<element_type>{
Node<element_type>* next;
Node<element_type>* prev;
element_type data;
};
Node<element_type>* head;
Node<element_type>* tail;
unsigned int size;
public:
linkedlist();
~linkedlist();
void push_back(const element_type& z);
void push_front(const element_type& z); //add front
void print() const;
// will initialize list with n nodes
explicit linkedlist(unsigned int n);
};
The TL;DR version is to drop the template syntax on Node:
struct Node{
Node* next;
Node* prev;
element_type data;
};
Node* head;
Node* tail;
Because Node is defined inside the class template, it can already access the type element_type. The compiler error is just telling you that you can't use template syntax when declaring a structure which is not itself a template.
Just remove template parameter from struct Node.
struct Node {
Node * next;
Node * prev;
element_type data;
};
Node * head;
Node * tail;
unsigned int size;

Function to Return Node Pointer

everyone! I am making my own linked list template to both practice and for future use; however, I ran into a problem with one of my functions:
Node* LinkedList::FindNode(int x); //is meant to traverse the list and return a pointer to the containing x as its data.
When trying to declare it in my implementation file, I keep getting messages of Node being undefined and incompatibility errors.
Here is my header file:
#pragma once
using namespace std;
class LinkedList
{
private:
struct Node
{
int data;
Node* next = NULL;
Node* prev = NULL;
};
//may need to typedef struct Node Node; in some compilers
Node* head; //points to first node
Node* tail; //points to last node
int nodeCount; //counts how many nodes in the list
public:
LinkedList(); //constructor
~LinkedList(); //destructor
void AddToFront(int x); //adds node to the beginning of list
void AddToEnd(int x); //adds node to the end of the list
void AddSorted(int x); //adds node in a sorted order specified by user
void RemoveFromFront(); //remove node from front of list; removes head
void RemoveFromEnd(); //remove node from end of list; removes tail
void RemoveSorted(int x); //searches for a node with data == x and removes it from list
bool IsInList(int x); //returns true if node with (data == x) exists in list
Node* FindNode(int x); //returns pointer to node with (data == x) if it exists in list
void PrintNodes(); //traverses through all nodes and prints their data
};
If someone can help me define a function that returns a Node pointer, I would greatly appreciate it!
Thank you!
Since Node is declared within another class, did you remember to include the class name when referring to it in your implementation?
LinkedList::Node *LinkedList::FindNode(int x) { ... }
In the class declaration the prefix isn't required because the declaration is inside the class, and therefore Node is implicitly available.

C++ Class has no data member in a hierarchy

my node class for a LLL, and a class derived from it
class node
{
public:
node();
~node();
node *& getNext();
protected:
node * next;
};
class word: public node
{
public:
word();
~word();
protected:
char ** words; //an array of char pointers
};
and I have another class
class sentence
{
public:
sentence();
~sentence();
void insert(node *& head, char *& word_to_add);
sentence *& operator+=(char *& sentence_to_add);
void remove(char *& word_to_remove);
void testing(sentence *& s);
void display();
void removeAll();
protected:
node * head;
};
and here's my implementation of the insert function
void sentence::insert(node *& head, char *& word_to_add)
{
if(!head)
{
head = new word;
insert(head, word_to_add);
}
else
{
for(int i = 0; i < MAX_WORD; ++i)
{
if(!head->words[i])
{
head->words[i] = new char[strlen(word_to_add)+1];
strcpy(head->words[i], word_to_add);
cout << head->words[i] << endl;
return;
}
}
insert(head->getNext(), word_to_add);
}
}
I'm getting the error class node has no data member named words. But if I were to change the prototype void insert(node *& head, char *& word_to_add); into void insert(word *& head, char *& word_to_add); it would say that word has no data member next. I'm kinda stuck because I did this in a previous program and it worked just fine. I was wondering what I'm doing wrong, so if someone could point that out, that'd be great!
edit: I forgot to include that I'm using the friend keyword in class word. For testing purposes. friend class sentence;
I would not derive from the node class.
Have the node be generic and contain pointer to some data (word) instead of having the data directly in the node ?
class node
{
node* next;
word data;
};
class word
{
char** words;
};
Since you tagged this C++, I might suggest you use a standard C++ collection (std::vector) instead of a linked list.

c++ declaring a variable of a class within that class

I have the following code:
template <typename T>
class Node{
public:
Node<T>(T data){
this->data = data;
}
T data;
Node<T> left;
Node<T> right;
};
But it doesn't like how I have member variables of the same type as the class they are in because the compiler doesn't know what "Node" is.
You want to declare left and right as Node<T> *:
Node<T> *left;
Node<T> *right;
The problem is not just that the compiler doesn't have the complete definition yet, but that you are trying to say a Node contains two other Nodes, so there is no sensible definition for the size of a Node.
Create a forwarding class declaration:
template<typename T>
class Node;
template<typename T>
class Node {
public:
T data;
Node<T> left, right;
// implementation here
}
This ---> Node<T> left,right causes compiler error. To fix it, you can declare left and right to be pointers, references, or static nodes:
template<typename T>
class Node {
public:
Node(T data) : data(data){}
T data;
Node<T> *left, *right; // Ok
/* OR */
Node<T> &left, &right; // Ok
/* OR */
static Node<T> left, Node<T> right; // also Ok
Node<T> left, right; // No way, causes error
};

How do I write a public function to return a head pointer of a linked list?

class Newstring
{
public:
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
struct Node
{
char item;
Node *next;
};
Node *head;
};
I am getting a syntax error : missing ';' before '*' on the declaration for my getHead function (yes I can't think of a better name). The purpose of this function is to return the head pointer.
Declare Node before you use it.
You have to declare the Node struct above getHead();
class Newstring
{
public:
struct Node
{
char item;
Node *next;
};
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
Node *head;
};
To answer Brandon about keeping the struct in private, or keeping the current code while adding a declaration, the way is :
class Newstring
{
struct Node; // here the declaration
public:
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
struct Node
{
char item;
Node *next;
};
Node *head;
};
Node * getHead()
Compiler is not able to get the definition of Node when getHead() is encountered.
struct Node
{
char item;
Node *next;
};
Put the definition of Node before it is used.
class Newstring
{
private:
struct Node
{
char item;
Node *next;
};
Node *head;
public:
Newstring(); ...
Node * getHead ();
Another way would be to forward declare Node by placing a struct before Node
:
void createList ();
struct Node * getHead ();
private:
struct Node
{
: