i am writing a code for a stack within stack...or nested stack...whatever..
in the code..and the mainstack is a stack of substack, which in turn, is a stack of integer data..

i thought of using class template for both the stacks - mainstack and substack..but am confused with how to use it..
the code for the template is as under :

template <class T>
class stack
{
	private :
		struct node
		{
			T element;
			node *link;
		} *top;

	public :
		stack();
		~stack();
		void push (T ele);
		T pop();
		void view();
};

template <class T>
stack<T> :: stack()
{
	top = NULL;
}

template <class T>
stack<T> :: ~stack()
{
	node *destroy;
	while ( top != NULL)
	{
		destroy = top;
		top = top -> link;
		delete destroy;
	}
}

template <class T>
void stack<T> :: push (T ele)
{
	node *newnode;
	newnode = new node;
	if (newnode == NULL)
	{
		cout << "Stack overflow!" << endl;
		return;
	}
	newnode -> element = ele;
	newnode -> link = top;
	top = newnode;
}

template <class T>
T stack<T> :: pop()
{
	if (top == NULL)
	{
		cout << "Stack is empty." << endl;
		return NULL;
	}
	T popped;
	popped = top->element;
	node *temp;
	temp = top;
	top = top -> link;
	delete temp;
	return popped;
}

template <class T>
void stack<T> :: view()
{
	node *temp = top;
	while (temp != NULL)
	{
		cout << temp->element << endl;
		temp = temp -> link;
	}
}

however my problem is how do i define the mainstack in main()?
it may be something like this :

// declaring an object of "mainstack" type
stack <substack> S;

where substack is the substack..but then how do i define substack class??
should it be like this ??

// if i dont define a mainstack class
class substack
{
   private :
       stack <int> s;
   public :
       //overloaded operaters << and >> and constructors ...blah..blah...
};

but i think this is not a proper way to work with...

another way i thought to work is to define class mainstack something like this :

// if i dont define a substack class
class mainstack
{
    private :
        stack <int> s;
};

but then here the template wont work properly...i am much confused here...
anyone there to guide me..???? waiting for helpful replies....

I have a potential solution...though you may not like it (and I haven't fully implemented it, so there could be some leaks):

1) In your stack class, in the private field, in the node struct, change T element to be a pointer (i.e. T *element)

2) In the public field of the stack class, change your prototype for push to take T* as a parameter, and change the pop prototype to return T*.

3) In the push implementation, change the function header to accept T *ele as a parameter.

4) In the pop implementation, return type should be T*, change T popped to T* popped.

5) In the view implementation, dereference temp->element (i.e. *temp->element).

6) In main, you can set up your stacks as follows:

stack <int> *subs = new stack<int>;
stack <stack<int>> *mains = new stack<stack<int>>;

To push elements to the subs stack:

int a = 5;
int b = 10;

subs->push(&a);
subs->push(&b);

To push elements to the main stack:

mains->push(subs);

However, I didn't know how you wanted view to behave for main...To view the top stack in main you can do this:

mains->pop()->view();

Anyway, I didn't check all the implementations, so some things might need to be changed...and this may not be what you were looking for...and you might have to write a few extra things...dunno

I have a potential solution...though you may not like it (and I haven't fully implemented it, so there could be some leaks):

1) In your stack class, in the private field, in the node struct, change T element to be a pointer (i.e. T *element)

2) In the public field of the stack class, change your prototype for push to take T* as a parameter, and change the pop prototype to return T*.

3) In the push implementation, change the function header to accept T *ele as a parameter.

4) In the pop implementation, return type should be T*, change T popped to T* popped.

5) In the view implementation, dereference temp->element (i.e. *temp->element).

6) In main, you can set up your stacks as follows:

stack <int> *subs = new stack<int>;
stack <stack<int>> *mains = new stack<stack<int>>;

To push elements to the subs stack:

int a = 5;
int b = 10;

subs->push(&a);
subs->push(&b);

To push elements to the main stack:

mains->push(subs);

However, I didn't know how you wanted view to behave for main...To view the top stack in main you can do this:

mains->pop()->view();

Anyway, I didn't check all the implementations, so some things might need to be changed...and this may not be what you were looking for...and you might have to write a few extra things...dunno

hey yaaa..i got your point...but i would like to know the reason why you are emphasizing on using pointer here...

I tend to think with pointers because I started with C...and pointers are commonplace in C...and thats how I learned...lol. But my thoughts were this: the elements in a stack may in fact be objects themselves (i.e. their own stack structures), so I thought it would be easier to manipulate if you were pointing to the element instead.

I'm sure it can be done without pointers, but that I shall leave as an exercise instead for you.

Besides, pointers are a wonderful and powerful (or scary..) part of the language...if you aren't going to use them, switch to Java (or C# if you are a Windows-only programmer ;) ). lol (just kidding) :)

I tend to think with pointers because I started with C...and pointers are commonplace in C...and thats how I learned...lol. But my thoughts were this: the elements in a stack may in fact be objects themselves (i.e. their own stack structures), so I thought it would be easier to manipulate if you were pointing to the element instead.

I'm sure it can be done without pointers, but that I shall leave as an exercise instead for you.

Besides, pointers are a wonderful and powerful (or scary..) part of the language...if you aren't going to use them, switch to Java (or C# if you are a Windows-only programmer ;) ). lol (just kidding) :)

lolz...got your point...actually even i have upgraded from C to C++...so am used to pointers...but here i just wanted to try it out first with simple variables...and then to use pointers...nevertheless...thank u for your response... :) :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.