implementing a dynamic stack with a linked list object

I have a class definition for Node, LinkedList and Stack (stack of ints). I want to implement the stack using a linked list object but need to learn how it is initialized within the dynamic stack class definition. I don't think I need a top pointer since LinkedList class has a head and tail pointer.

I was wondering in this case, do I need any private members for class Stack at all?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include "LinkedList.h"
// Specification file for the Stack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class Stack
{
	private:
	
	LinkedList list;//VS is telling me this is necessary to make list functions accessible in the 
                            //public member methods in this class definition of Stack.
	public:
	
		
	// Constructor
	Stack()
	{ 
		LinkedList::LinkedList();//call constructor of LinkedList.
	}

	// Destructor
	~Stack()
	{
		list.~LinkedList();//destructor for LinkedList.h
		
	}

	// Stack operations
	void push(int num)
	{
       //call the prepend member function of LL		
        list.prepend(num);
	}
	void pop(int &num)
	{
       //call the removeHead member function of LL		
        list.removeHead();
		//code to catch the removed value and return it. num=something
	}
	bool isEmpty() 
	{
       //call the empty member function of LL
		list.empty();
		//var to catch returning value if LL is empty and code to return it
	}
};
#endif 


Just calling constructor of LinkedList object in the Stack constructor.
Last edited on
list is already a member of Stack, so the default constructor is used automatically, the destructor too, so you don't need to mention them in the Stack constructor/destructor. So line 19 and 25 need to removed.
@kbw. Thanks for the correction. I also thought about setting up a LinkedList* stack and then using it to call stack->prepend(), stack->removeHead().
I'm not quite sure what you mean.

Loot at it this way. C has some built in types, int, char, float and so on. C++ allows you to create user defined types. Somewhere, you've found a type LinkedList and used that to implement a your own Stack.

Just because Stack is implemented with LinkedList, doesn't mean that anyone using Stack can see that. From the outside, there's no relationship between the two types. If you want a stack, use Stack stack;, but LinkedList* stack doesn't make a whole lot of sense.
Topic archived. No new replies allowed.