Stack using SingleLinkedList

So the task is to implement stack using single linked list. But without defining the actual node(the only private member: Single_List list and public functions). Single_List were implemented by me before and have private head pointer(requirement of teacher). So I made all of the reqiered functions exept for int& top() that returns pointer to the data of head.
Here is header files for list and stack
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
  class Single_List
{
private:
	Single_Node* head;
	Single_Node* tail;
public:
	friend void print(Single_List& list);
	Single_List();
	Single_List(int a);
	~Single_List();
	bool is_empty();
	void add_front(int a);
	int remove_front();
	bool find(int a);
	int get_nth(int n);
	int size();
};


#include "Single_List.h"
class Stack
{
private:
	Single_List* list;
public:
	Stack();
	~Stack();
	void push(int);
	void pop();
	int& top();
	int size();
	bool is_empty();
};


Can I get access to head data without changing Single_List code? Thanks.
Last edited on
You could use Single_List::get_nth and pass it 0. Make sure that the size isn't 0 first.
But it's not going to be a reference. Single_List::get_nth(int) returning just int, so
1
2
3
Stack s;
s.push(0);
s.top() = 2;

will not change anything
Why top should return a reference?
Changing the top element of stack is a simple pop(); push(2)
While not efficient, you can do it this way (while keeping your list code the same) is to just use your size() function in your list class as an iterator, then perform loop operations in your stack class to get to the head of your list class.
Last edited on
If Stack::top() needs to return a reference then you'll have to change something in Single_List(). As booradley60 suggested, I think you should change get_nth() to return a reference.

Also, why does Stack contain a pointer to Single_List? It seems easier for it to contain an instance of Single_List instead. Otehrwise you have to worry about memory management.
Still looking for advice. Can someone explain why
1
2
3
4
5
6
7
8
9
10
11
12
13
int& Stack::top()
{
	int* value = new int;
	*value = list.get_nth(0);
	return *value;
}
int main()
{
	Stack s;
	s.push(0);
	s.top() = 2;
	return 0;
}

doesn't work?
Last edited on
Because you're creating a new integer on the heap and assigning a value to it. You're changing nothing about the stack itself.

However, what's wrong with keskiverto's suggestion? If you want to replace the value on top of the stack, pop the stack then push the new value. That's the interface that the stack exposes. It isn't supposed to let you fiddle with the internals directly.
Okay. What about this?
1
2
3
4
5
6
7
8
int& Stack::top()
{
	int* value = new int;
	*value=list.get_nth(0);
	pop();
	push(*value);
	return *value;
}

I know returning reference is inconvinient but that's the requirement.
s.top() = 2; have to work
I don't think it's possible to implement int& Stack::top() if you are using Single_List as the implementation. You must change Single_List for this to work. All you have to do is change get_nth() to return a reference instead of a value.
You could perhaps do:
1
2
3
StackReference Stack::top() {
  return StackReference( list );
}

But then you have to create class StackReference that behaves (i.e. can be used exactly) like int&.

Updating the Single_List is much easier.
Topic archived. No new replies allowed.