I want to create a class of LinkedList and I have to put the class of Node inside of the class of LinkedList, how do you prefer me to do it?
I think something like:
Class LinkedList {
private:
class Node* head;
public:
class Node {
private:
int data;
Node* next;
Node* prev;
};
};
but I think this is not good.
I would do it like this
class LinkedList {
private:
struct Node {
int data;
Node* next;
Node* prev;
};
Node* head;
public:
...
};
No need for anything in Node to be private since it's not useable outside of LinkedList.
Related
I created a class BST and I am facing problem in creating constructor of a struct node in it.
class BST{
private:
struct node{
int key;
node* left;
node* right;
};
node* root;
public:
//constructor for BST
BST();
//constructor for node
node(int x);
};
I get an error expected unqualified-id before 'int'.
So,I read what is an unqualified-id but didn't understand why is it needed here.
Constructors for classes and structs must be declared within the definition of the class/struct. Your version does not do this, as node(int) is declared outside the node class.
Change your code to this:
class BST
{
private:
struct node
{
int key;
node* left;
node* right;
node(int x);
};
node* root;
public:
BST();
};
Then if you decide to implement the node constructor outside of the BST class:
BST::node::node(int x)
{
// code here
}
I am trying to write a template for a class. This class uses a Node class within it so I have defined the Node class as having the List class as a friend. See below.
template <typename T>
class Node {
private:
Node() {
next = nullptr;
prev = nullptr;
}
Node(int data) : Node() { this->data = data; }
Node *next;
Node *prev;
int data;
friend class DoubleLinkedList;
};
template<typename T>
class DoubleLinkedList {
public:
DoubleLinkedList();
~DoubleLinkedList();
private:
Node *_head;
Node *_tail;
};
I have another file where the classes are implemented. I get this error with or without the template definition above the List class. Can someone explain this?
This works for me:
template <typename T>
class Node
{
private:
Node()
{
next = nullptr;
prev = nullptr;
}
Node(int data) : Node() { this->data = data; }
Node *next;
Node *prev;
int data;
template<typename T> friend class DoubleLinkedList;
};
template<typename T>
class DoubleLinkedList
{
public:
DoubleLinkedList();
~DoubleLinkedList();
private:
Node<T> *_head;
Node<T> *_tail;
};
I am quite a newbie when it comes to design patterns so am having a hard time grasping the concept of the decorator design pattern. Is it possible to decorate a singly linked list class to a doubly linked list class which inherits from it? I would like to decorate the following class:
ListAsSLL.h:
#ifndef LISTASSLL_H
#define LISTASSLL_H
class ListAsSLL
{
protected:
struct node{
int i;
struct node* next;
};
node* head;
node* tail;
int listSize;
public:
ListAsSLL();
virtual void addToBeginning(int obj);
virtual void addAtPos(int obj, int i);
virtual void addToEnd(int obj);
virtual void del(int i);
virtual void overwrite(int obj, int i);
virtual void grow();
virtual void shrink();
};
#endif //LISTASSLL_H
Giving the doubly linked list class the same functionality with the added feature of having a struct with a pointer to the previous node.
Hopefully someone can shed some light on how to do this. Thanks in advance.
Here is an example of how it can be implemented. I added another virtual method createNode and show possible implementation of addToBeginning().
class ListAsSLL
{
protected:
struct node{
int i;
struct node* next;
};
node* head;
node* tail;
int listSize;
virtual node *createNode() { return new node; }
public:
virtual void addToBeginning(int obj)
{
node *node = createNode();
node->i = obj;
node->next = head;
if( !head ) tail = node;
head = node;
++listsize;
}
...
};
class ListAsDLL
{
protected:
struct dnode : node{
node* prev;
};
virtual node *createNode() { return new dnode; }
public:
virtual void addToBeginning(int obj)
{
node *prevHead = head;
ListAsSLL::addToBeginning( obj );
static_cast<dnode *>( head )->prev = prevHead;
}
...
};
Code was not tested, though may have logic errors, as was written to show general idea.
i'm going to be crazy for this problem.
I'm trying to use list in c++. I can't use <vector> or <list>, cause my professor wants it in "pure" c++. (With class, in short..).
I can create list which has only an int field, for example:
class List{
private:
struct node{
int *data;
node* next;
};
typedef struct node* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
List();
void AddNode(int addData);
void deleteNode(int delData);
void PrintList();
};
(this works, this isn't the entire code, but it works.)
The problem born now:
How could I create a list of objects, instead list of "int" data?
If i have, for example, to create a list of People, like an address book, how should I do?
I'm gonna be crazy, please help me. Thanks in advance.
(sorry for my bad english, i'm not that good :)
The answer lies in template classes.
http://www.cplusplus.com/doc/tutorial/templates/
<template class T>
class List{
private:
struct node{
T *data;
node* next;
};
typedef struct node* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
List();
void AddNode(T addData);
void deleteNode(T delData);
void PrintList();
};
Where you use list you now define the type you want to use
List<int> intList;
say in a header file can I have a helper class fully defined and use it in the class file that includes the header. what is the correct way of doing it?
//HEader
class LinkedList() {
public:
LinkedList(int a);
private:
Node *root;
class Node {
int data;
Node *next;
};
};
//cpp file
#include "LinkedList"
LinkedList::LinkedList(int a) {
root = new Node();
root.data = a;
root->next = NULL;
}
when i try doing something like that it ends up saying Node is not a name of type in my header file.
That is totally fine. I made some fixes to your code.
LinkedList.h
class LinkedList
{
public:
LinkedList(int a);
private:
class Node {
public:
int data;
Node *next;
};
Node *root;
};
LinkedList.cpp
LinkedList::LinkedList(int a) {
root = new Node();
root->data = a;
root->next = NULL;
}
You tried to use Node before you even declared and defined it. Default access level in C++ classes is private, so you could not access private data members of Node in LinkedList constructor.