How to use list with class C++ - c++

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;

Related

how to write constructor prototype for a struct in a class

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
}

Using decorator design pattern to create a doubly linked list by decorating a singly linked list

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.

C++: Accessing class properties in header implementation file

I am trying to implement the doubly linkedlist data structure, so I created a class that has a private property node of type Node, when I try to access this property from a function implementation using this keyword the application fails. I need help
Header file for LinkedList.hpp
#include <stdio.h>
template<class T>
class LinkedList{
private :
struct Node{
T value;
Node* next;
Node* prev;
}node;
public :
LinkedList();
LinkedList(T item);
void add(T item);
// void get();
// void insert();
// void remove();
};
Below is the implementation of the header file.
#include "LinkedList.hpp"
template<class T>
LinkedList<T>::LinkedList(){
}
template<class T>
LinkedList<T>::LinkedList(T item){
}
template <class T>
void LinkedList<T>::add(T item){
Node* node = new Node;
node->value = item;
node->prev = NULL;
//Where the error is being generated
node->next = this.node;
};
The error returned says:
/Users/mac/Documents/LinkedList/LinkedList/LinkedList.cpp:27:22: Member reference base type 'LinkedList<T> *' is not a structure or union
this is a pointer, as pointed out by the error message.
Use:
this->node

problems inheriting from a c++ template class

So I'm trying to work out how inheritance works when templates are in the mix. Most compilers really don't seem to have this figured out yet, so I'm having a little syntax difficulty. All the weird includes in SkipNode.h are from trying to get eclipse to stop yelling at me. I'm getting a syntax error when trying to declare the constructor in SkipNode.h, so any help here would be useful.
Here is node.h
#ifndef NODE_H_
#define NODE_H_
template<class T>
class Node
{
public:
Node(Node<T>* next, Node<T>* prev, T item);
virtual ~Node();
Node* getPrev() { return prev;};
Node* getNext() { return next;};
Node* getItem() { return item;};
void setItem(T item){Node<T>::item = item;};
void setNext(Node* next){Node<T>::next = next;};
void setPrev(Node* prev){Node<T>::prev = prev;};
private:
Node* next;
Node* prev;
T item;
};
Here is SkipNode.h, where skipnode inherits from Node.
#include "Node.h"
#include "Node.cpp"
#include "SkipNode.h"
#include "SkipNode.cpp"
template <class T>
class SkipNode: public Node
{
public:
SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item) : Node(next, prev, item);
virtual ~SkipNode();
Node* getChild(){return child;};
void setChild(Node* child){SkipNode::child = child;};
private:
Node *child;
};
#endif /* SKIPNODE_H_ */
Node is a template, you should pass in template parameter
template <class T>
class SkipNode: public Node<T>
// ^^^
Also you need to provide SkipNode constructor definition as you have provided member iniatilizer list.
update:
SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item)
: Node(next, prev, item);
To:
SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item)
: Node(next, prev, item)
{
}
You're missing an #endif in node.h and you need to provide the template parameter to your base class:
template <class T>
class SkipNode : public Node< T >
....
This level of indirection exists to allow inheritance from a template class by a class that has different (or zero) template parameters. For instance:
class Foo : public Node< int >

Node Class inside the LinkedList class

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.