Linked List

I have to create a linked list, a stack, and a main to print out Hello World backwards. I am running into some problems with my code.

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  Here is my Linked List
#include <iostream>
#include <cstddef>

using namespace std;

template <typename T>
class Node{
	private:
		T value;
		Node *next;

	public:

		Node();
		Node (T data);
		T remove();
		T getValue();
		Node* getNext();
		void setValue(T data);
		void setNext(Node *nextNode);

};

template <typename T>
Node<T>::Node(){

    next = NULL;

}

template <typename T>
Node<T>::Node(T data){

    value = data;
    next = NULL;


}

template <typename T>
T Node<T>::getValue(){

    return value;


}

template <typename T>
Node<T>* Node<T>::getNext(){

    return next;

}

template <typename T>
void Node<T>::setValue(T data){

    value = data;

}

template <typename T>
void Node<T>::setNext(Node *nextNode){

    next = nextNode;

}

template <typename T> class MyLinkedList{
    private:
        Node<T> head;
        int size;

    public:
        MyLinkedList();
        void add(T val);
        T remove();
        void printContent();
        int getSize();
        bool empty();
};

template <typename T>
MyLinkedList<T>::MyLinkedList(){

    size = 0;
}

template <typename T>
void MyLinkedList<T>::add(T val){

     Node<T> current = head;
     Node<T> add = new Node<T>(val);


        while (current.getNext() != NULL){
               current = *(current.getNext());
        }

        current.setNext(new Node<T>(val));

}


template <typename T>
T MyLinkedList<T>::remove(){

     Node<T> current = head;

        while (current.getNext() != NULL){

            if( (current.getNext())->getNext() == NULL)
                break;
            else
               current = *(current.getNext());
        }
    Node<T> last = *(current.getNext());
    current.setNext(NULL);
    T value = last.getValue();
    delete &last;
    return value;
}

template <typename T>
void MyLinkedList<T>::printContent(){

    Node<T> current = head;
    while (current.getNext() != NULL){
            cout << current.getData() << " ";
               current = &(current.getNext());
        }
}

template <typename T>
int MyLinkedList<T>::getSize(){

    return size;
}

template <typename T>
bool MyLinkedList<T>::empty(){

    if ( size == 0)
            return true;
    else
        return false;
}


Here is MyStack

#include <iostream>
#include <cstddef>


using namespace std;


template <typename T> class MyStack{
        private:
            MyLinkedList<T> list;

        public:
            MyStack();
            T pop();
            void push(T value);
            bool isEmpty();
};

template <typename T>
MyStack<T>::MyStack(){

    list = new MyLinkedList<T>();
}
template <typename T>
T MyStack<T>::pop(){

    return list.remove();

}

template <typename T>
void MyStack<T>::push(T value){

    list.add(value);

}

template <typename T>
bool MyStack<T>::isEmpty(){

    return list.empty();

}

And my Main

#include <iostream>
#include "MyStack.cpp"
using namespace std;
int main()
{
    MyStack<char> stack;
    char a[] = {'H','E','L','L','O','W','O','R','L','D'};
    for (int i = 0; i < 10; i++){
        stack.push(a[i]);
    }
    for (int i = 0; i < 10; i++){

        cout << stack.pop();
    }

   return 0;
}


Thanks for your time
On line 162, I get the error 'MyLinkedList' does not name a type. On line 174, i get the errors: 'list' was not declared in this scope, expected type-specifier before 'MylinkedList' and expected ';' before 'MyLinkedList.' On line 179, 186 and 193, I get 'list' was not declared in this scope.
Why are you #including a .cpp file in main?

You need to #include the proper header file to use your MyLinkedList class.

P
Ahh, I see. What type of header file would let me use my list class?
After removing the .cpp file in main, I am only getting errors saying MyStack was not declared in this scope. Sorry if this is basic stuff but the computer science program at my school is ass. I still have the same errors in MyLinkedList
Last edited on
Post the complete error messages, all of them, exactly as they appear in your development environment. Those messages have important information embedded within them to aid in locating and fixing the problems.

What type of header file would let me use my list class?

The header file that defines and implements the class. If you don't understand basic header files you're probably going to have major problems understanding template classes. Maybe you need to review some of the basics before you try to tackle templates?

Last edited on
In function 'int main()':|
|6|error: 'MyStack' was not declared in this scope|
|6|error: expected primary-expression before 'char'|
|6|error: expected ';' before 'char'|
|9|error: 'stack' was not declared in this scope|
|13|error: 'stack' was not declared in this scope|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


MyStack:

|10|error: 'MyLinkedList' does not name a type|
|In constructor 'MyStack<T>::MyStack()':|
|22|error: 'list' was not declared in this scope|
|22|error: expected type-specifier before 'MyLinkedList'|
|22|error: expected ';' before 'MyLinkedList'|
||In member function 'T MyStack<T>::pop()':|
|27|error: 'list' was not declared in this scope|
||In member function 'void MyStack<T>::push(T)':|
|34|error: 'list' was not declared in this scope|
||In member function 'bool MyStack<T>::isEmpty()':|
|41|error: 'list' was not declared in this scope|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Last edited on
|6|error: 'MyStack' was not declared in this scope|

Did you #include the header file that defines your MyStack class?

10|error: 'MyLinkedList' does not name a type|

Did you #include the header file that defines the MyLinkedList class?

For my main, my header file is now
1
2
3
4
#include 
<iostream>
#ifndef MyStack_h
#endif  MyStack_h 


and for my MyStack
1
2
3
4
5
#include <iostream>
#include <cstddef>
#ifndef MyLinkedList_H
#endif  MyLinkedList_H


error messages now read:
Main:
|4|warning: extra tokens at end of #endif directive [enabled by default]|
||In function 'int main()':|
|8|error: 'MyStack' was not declared in this scope|
|8|error: expected primary-expression before 'char'|
|8|error: expected ';' before 'char'|
|11|error: 'stack' was not declared in this scope|
|15|error: 'stack' was not declared in this scope|
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

MyStack:

5|warning: extra tokens at end of #endif directive [enabled by default]|
|13|error: 'MyLinkedList' does not name a type|
||In constructor 'MyStack<T>::MyStack()':|
|25|error: 'list' was not declared in this scope|
|25|error: expected type-specifier before 'MyLinkedList'|
|25|error: expected ';' before 'MyLinkedList'|
||In member function 'T MyStack<T>::pop()':|
|30|error: 'list' was not declared in this scope|
||In member function 'void MyStack<T>::push(T)':|
|37|error: 'list' was not declared in this scope|
||In member function 'bool MyStack<T>::isEmpty()':|
|44|error: 'list' was not declared in this scope|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Last edited on
||In instantiation of 'MyStack<T>::MyStack() [with T = char]':|
|6|required from here|
|23|error: no match for 'operator=' in '((MyStack<char>*)this)->MyStack<char>::list = (operator new(12u), (<statement>, ((MyLinkedList<char>*)<anonymous>)))'|
23|note: candidate is:|
|61|note: MyLinkedList<char>& MyLinkedList<char>::operator=(const MyLinkedList<char>&)|
|61|note: no known conversion for argument 1 from 'MyLinkedList<char>*' to 'const MyLinkedList<char>&'|


||In instantiation of 'void MyLinkedList<T>::add(T) [with T = char]':|
|35|required from 'void MyStack<T>::push(T) [with T = char]'|
|9|required from here|
|85|error: invalid conversion from 'Node<char>*' to 'char' [-fpermissive]|
|31|error: initializing argument 1 of 'Node<T>::Node(T) [with T = char]' [-fpermissive]|
||=== Build failed: 3 error(s), 5 warning(s) (0 minute(s), 0 second(s)) ===|
Topic archived. No new replies allowed.