Read-only access to top element of linked list

This is a fairly straight-forward problem, and it seems like it should be simple, but I'm not sure how to implement it. I'm probably getting stuck on syntax. If someone could just give me a HINT, I'd appreciate it.

Function is on line 23:
stack.cpp
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
#include "node.h"
#include "stack.h"

using namespace std;


void Stack::push(int n){
	//Node * ptemp = new Node(value);
    //head.insert(Node(value))
	head.insert(new Node(n));
}

int Stack::pop(){
	Node* ptr_obsolete_node = head.pnext;
    head.remove_next();
	delete ptr_obsolete_node;
}

int& Stack::top(){
    return head.pnext->data;
}

int Stack::top() const{
    
}

size_t Stack::size() const{
  return head.size() - 1;
}

bool Stack::is_empty() const{
  return size() == 0;
}

void Stack::reverse(){

}


node.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef NODE_H
#define NODE_H

// The node class for singly-linked list of integers

#include <cassert>
#include <iostream>
using namespace std;

class Node
{
    friend void print_list( Node const& head );
public:
    Node* pnext;
    int data;

    // Constructor taking initial value:
    Node( int value = 0 )
    : pnext( NULL ), data( value )
    {
    }

    // Copy constructor:
    Node( Node const& other )
    : pnext( NULL ), data( other.data )
    {
    }

    // Insert new node in front:
    void insert( Node* newNode )
    {
        newNode->pnext = pnext;
        pnext = newNode;
    }

    // Remove node in front:
    void remove_next()
    {
        if ( pnext == NULL ) return;
        Node* obsolete = pnext;
        this->pnext = obsolete->pnext;
        // Make node "single":
        obsolete->pnext = NULL;
    }

    // Remove other node in front:
    bool remove( Node* other )
    {
        Node* previous = this;
        Node* iter = pnext;
        while ( iter != other ) {
            if ( iter == NULL )
                return false;
            previous = iter;
            iter = iter->pnext;
        }
        previous->pnext = iter->pnext;
        // Make node "single" after successful removal:
        other->pnext = NULL;
        return true;
    }

    // Measure distance to other node:
    int distance( Node const* other ) const
    {
        int hops = 1;
        Node const* iter = pnext;
        while ( iter != other ) {
            if ( iter == NULL ) {
                // Don't count this last hop:
                return hops - 1;
            }
            iter = iter->pnext;
            ++hops;
        }
        return hops;
    }
    // Calculate number of nodes in the list:
    size_t size() const
    {
        return distance( NULL );
    }
};

void print_list( Node const& head );
#endif //NODE_H 


stack.h
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
#if (!defined(STACK_H))
#define STACK_H
#include "node.h"

class Stack {
	Node head;
public:

    // Push new element to the stack:
    void push( int value );

    // Pop element from the stack:
    int pop();

    // Writeable access to element on top of the stack:
    int& top();

    // Read-only access to element on top of the stack:
    int top() const;

    // Return stack size:
    size_t size() const;

    // Is stack empty?
    bool is_empty() const;

    // reverse the stack
    void reverse();

};
#endif 


main
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
#include <cassert>

#include "Node.h"
#include "Stack.h"

int main()
{
    Stack st;
    assert( st.size() == 0 );

    st.push( 123 );
    assert( st.size() == 1 );
    assert( st.top() == 123 );

    st.top() = 321;
    assert( st.size() == 1 );
    assert( st.top() == 321 );

    st.push( 456 );
    assert( st.size() == 2 );
    assert( st.top() == 456 );

    st.top() = 654;
    assert( st.size() == 2 );
    assert( st.top() == 654 );

    st.pop();
    assert( st.size() == 1 );
    assert( st.top() == 321 );

    st.pop();
    assert( st.size() == 0 );

    return 0;
}
Last edited on
You sure you want to try to overload function names?
It was in the homework guidelines for it to be overloaded. Wait, was that the hint?
Last edited on
Have you run the program? If so, what was the output?
I did some test output. The overloaded top() isn't called in main, but when I made a call with my previous definition, it didn't output what I wanted it to. I should output the same as the other version. The only difference is the return type. I'm trying to figure out what to write for the definition to make this happen.
Last edited on
The only difference is the return type.

You can't overload a function by changing only its return value.

What do you want to achieve by that? Shouldn't the overload return a int & too? If you return an int, you are not granting read-only access to the top, you are returning a copy of the value (which is the same for ints, but surely different for non-trivial objects).
Ah, I thought that was also considered overloading. Regardless, that's the way the program needs to be written. Returning a copy of the value in this case isn't "access" but it is still read-only.

These function prototypes are taken straight from here: http://www.c-jump.com/CIS62/hw1_3.htm
The problem is not the name, it can't be done. But you are not doing it, don't worry.

And, as I said, for a stack of int the functions do the same, just remember that if you use other types, it may not be.
For read only access return a const reference from a const function. Otherwise a normal reference:

1
2
3
4
5
6
7
8
9
class Stack
{

public:

    int& top() { return head.pnext->data; }
    const int& top() const { return head.pnext->data; } // allows read-only access to a const Stack

};
Is there any way to do it matching these prototypes?
1
2
3
4
5
// Writeable access to element on top of the stack:
    int& top();

    // Read-only access to element on top of the stack:
    int top() const;


I have this, but I guess it isn't technically access. Either my professor's wording is off, or his code is.
1
2
3
4
int Stack::top() const{
    int x = head.pnext->data;
    return x;
}
Last edited on
Have you read our answers?

1
2
int & top() { return head.pnext->data; }
int top() const { return head.pnext->data; }
Didn't know it was that simple. Thanks. I assumed that the definition determined the accessibility, not the return type.
The context of the call determines the accessibility.

1
2
3
4
Stack s;
const Stack cs;
s.top(); // int & Stack::top()
cs.top(); // int Stack::top() const 
Topic archived. No new replies allowed.