Hi all,
I am trying to implement a few of the basic data structures such as stack, queue, deque, singly/double linked lists. I would like to make these structures templated. For example,
1 2 3 4 5 6 7 8 9 10 11
|
template <typename T>
class Stack
{
public:
//member functions
private:
struct Node
{
};
};
|
I plan on creating each data structure in its own header file and implementing the member functions in the header file itself, because of the template. However, I would like to use class Node instead of a struct. Also, if I use a class Node for the underlying implementation, I would like to be able to place class Node outside of the declaration of class Stack. For example,
Stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef STACK_H
#define STACK_H
class Node
{
public:
//member functions
private:
//member variables
};
class Stack
{
public:
//member functions for class Stack
//Node* search(T item);
private:
Node *top;
};
#endif
|
The main questions I have are:
1. Should I just use a struct and keep the code fairly straightforward instead of having class Node on its own?
2. If I use a a class for the Nodes, should I place the declaration inside of class Stack or can I place it outside of class Stack? If I place it outside of class Stack, can a user of the class Stack gain access to the private data by creating an instance of the Node class and modify the class Stack. One example I can think of is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include "Stack.h"
int main()
{
Stack<int> stack;
for(int i = 1; i <= 10; i++)
stack.push(i);
Node *position;
position = search(5);
position->data += 5;
//Or something to this effect...
}
|
Can someone point me in the right direction as to how to go about this. I am sure that I will have further questions, however, these are the main ones for now. I would like to be able to search the list, however, I do not want to give a user of the class the ability to modify the data of the Stack class. Any and all help is greatly appreciated. Thank you for your time.