Linked List

Good morning and I thank you in advance for the help. I am have major problems with the List function getNewNode and also the Node for the first and last. It says forbid declaration of Node with no type. I add the Node.h and also friend List class. I am majorly confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef NODE_H
#define NODE_H

class Node
{
    friend class Link;

    public:
        Node( int & );
        int getData() const;

    private:
        int info;
        Node *nextPtr;
};


#endif // NODE_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include "Node.h"

using namespace std;


Node::Node( int &value )
: info( 0 ), nextPtr( 0 )
{
    info = value;
}

int Node::getData() const
{
    return info;
}



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
#ifndef LINK_H
#define LINK_H
#include "Node.h"

class Link
{
    public:
        Link();
        ~Link();
        Node getNewNode( int & );
        void insertAtFront( int & );
        void insertAtBack( int &);
        bool removeFromBack( int & );
        bool removeFromFront( int & );
        bool isEmpty() const;
        void print() const;
        void selectMenu( Link & );

    private:
        Node *firstPtr;
        Node *lastPtr;
};

#endif // LINK_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include "Node.h"
#include "Link.h"
using namespace std;


Link::Link()
: firstPtr( 0 ), lastPtr( 0 )
{}

Link::~Link()
{
    if( !isEmpty())
    {
        Node *currentPtr = firstPtr;
        Node *tempPtr;

        while( currentPtr != 0 )
        {
            tempPtr = currentPtr;
            cout << tempPtr->getData() << "\n" ;
            currentPtr = currentPtr->nextPtr;
            delete tempPtr;
        }
    }
    cout << "All nodes destroyed\n" << endl;
}

Node Link::getNewNode( int &value )
{
    return new Node( value );
}

bool Link::isEmpty() const
{
    return firstPtr == 0;
}

void Link::insertAtFront( int &value )
{
    Node *newPtr = getNewNode( value );

    if( isEmpty() )
        firstPtr = lastPtr = newPtr;
    else
    {
        newPtr->nextPtr = firstPtr;
        firstPtr = newPtr;
    }
}

void Link::insertAtBack( int &value )
{
    Node *newPtr = getNewNode( value );

    if( isEmpty() )
        firstPtr = lastPtr = newPtr;
    else
    {
        lastPtr->nextPtr = newPtr;
        lastPtr = newPtr;
    }
}

bool Link::removeFromFront( int &value )
{
    if( isEmpty())
        return false;
    else
    {
        Node *tempPtr = firstPtr;

        if( firstPtr == lastPtr )
            firstPtr = lastPtr = 0;
        else
            firstPtr = firstPtr->nextPtr;

        value = tempPtr->info;
        delete tempPtr;
        return true;
    }
}

bool Link::removeFromBack( int &value )
{
    if( isEmpty())
        return false;
    else
    {
        Node *tempPtr = lastPtr;

        if( firstPtr == lastPtr )
            firstPtr = lastPtr = 0;
        else
        {
            Node *currentPtr = firstPtr;

            while (currentPtr->nextPtr != lastPtr )
                currentPtr = currentPtr->nextPtr;

            lastPtr = currentPtr;
            currentPtr->nextPtr = 0;
        }
        value = tempPtr->info;
        delete tempPtr;
        return true;
    }
}

void Link::print() const
{
    if( isEmpty() )
    {
        cout << "The linked list is completely empty" << endl;
        return;
    }

    Node *currentPtr = firstPtr;

    cout << "The current integers are as follows: ";

    while( currentPtr !=0 )
    {
        cout << currentPtr->getData() <<" \n";
        currentPtr = currentPtr->nextPtr;
    }
}

void Link::selectMenu( Link &CurrentList )
{
    int option = 0;
    int value = 0;

    cout << " Enter an integer that corresponds with the correct action wanting to perform:\n"
        << "1 - insert at the front\n"
        << "2 - insert at the end\n"
        << "3 - delete from the front\n"
        << "4 - delete from the end\n"
        << "5 - end linked list programn";

    cin >> option;

    while( option < 5 )
    {
    switch( option )
    {
        case 1:
            cout << "Enter an integer" ;
            cin >> value;

            CurrentList.insertAtFront( value );
            CurrentList.print();
            break;

        case 2:
            cout << "Enter an integer" ;
            cin >> value;

            CurrentList.insertAtBack( value );
            CurrentList.print();
            break;

        case 3:
            if( CurrentList.removeFromFront( value ))
            cout << value << " removed\n";

            CurrentList.print();
            break;

        case 4:
        if( CurrentList.removeFromBack( value ))
            cout << value << " removed\n";

            CurrentList.print();
            break;

    }
    }
    cout << "complete" << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Link.h"
#include "Node.h"
using namespace std;

int main()
{
    cout << "This program will attempt to collect and return a linked list" << endl;

    Link myNumbers;
    myNumbers.selectMenu( myNumbers );


}
Last edited on
With regard to Node does not name a type error.
This is because you have a circular include header problem ( link.h includes node.h which includes link.h and nothing ends up getting included).

As the Node class declaration in Node.h only declares the Link class
as a friend, then we do not need to include link.h

What I am trying to say is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef NODE_H
#define NODE_H
//#include "Link.h" //get rid of this line from Node.h

class Node
{
    friend class Link;

    public:
        Node( int );
        int getData() const;

    private:
        int info;
        Node *nextPtr;
};


#endif // NODE_H  





There are a whole lot of errors in the link.h and link.cpp files.
Most of your definitions in Link.cpp do not match the prototypes given in the class declaration in link.h.
There are also other error like missing brackets.
There are too many to mention - and I leave you to sort them out.

I am sorry for getting sloppy with the code. I started concentrating on the error with the friend class that I disregarded the attention needed for the rest of the code. A great lesson was learned. Thanks for the help. I hope you do not mind me asking one more question.

I finally got it to work but I am a misunderstand this:

Node *getNewNode( int & );

Node *Link::getNewNode( int &value )
{
return new Node( value );
}


Does this mean that I will return a pointer to a new Node( value ). That is what I believe but I am unsure if that is correct. When I remove the * I get a conversion error from Node* to non-scalar type node request. Are you able to explain the error a little. I know how to fix it, but I really would like to know what is the background of it. Thank you so much and have a great weekend.
Topic archived. No new replies allowed.