Reputation: 125
I want to create Node likes Arraylist in c++. When I create a method get(); it said about the error. I don't understand when I go to find the answer on the internet. Can You Please Help Me Find This Answer?
template<typename T>
struct Node{ //Node
T data;
Node<T> *next;
Node(){
next=NULL;
}
Node(T value){
data=value;
next=NULL;
}
};
template<typename T>
class LinkedList{ //class Node
public:
Node<T> *head;
Node<T> *tail;
void add(T value){ //method create newnode add to tail
Node<T> *newNode=new Node<T>(value);
if (head == NULL){
head=newNode;
tail=newNode;
}
else {
tail->next=newNode;
tail=newNode;
}
}
void PrintAll(string Name){ //method print all node
Node<T> *current;
int i=0;
current=head;
while (current != NULL ){
printf("%s[%d]=%d\n",Name.c_str(),i,current->data);
current=current->next;
i++;
}
}
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->next;
current=current->next;
count++;
}
}
};
error: invalid conversion from 'Node*' to 'int' [-fpermissive]| warning: control reaches end of non-void function [-Wreturn-type]|
Upvotes: 3
Views: 2280
Reputation: 310930
The structure Node
should be defined within the class LinkedList
and be a private member of the class.
The member function PrintAll
should be declared with the
qualifier const
(and the name of the function should start with a lower case letter).
The function get
shall throw an exception in case when the list contains less data than the specified index. Otherwise it shall return the value of the data member data
of the found node.
Here is a demonstrative program that shows how the list can be defined.
#include <iostream>
#include <string>
#include <stdexcept>
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node *next;
Node() : data( T() ), next( nullptr )
{
}
Node( const T &value ) : data( value ), next( nullptr )
{
}
};
Node *head = nullptr;
Node *tail = nullptr;
public:
void add( const T &value )
{
Node *newNode = new Node( value );
if ( head == nullptr )
{
head = tail = newNode;
}
else
{
tail = tail->next = newNode;
}
}
void printAll( const std::string &name = "" ) const
{
size_t i = 0;
for ( auto current = head; current != nullptr; current = current->next )
{
std::cout << name << i++ << ": " << current->data << '\n';
}
}
T get( size_t index ) const noexcept( false )
{
auto current = head;
while ( index != 0 && current != nullptr )
{
current = current->next;
--index;
}
if ( current == nullptr ) throw std::out_of_range( "Too big index" );
else return current->data;
}
};
int main()
{
LinkedList<int> list;
list.add( 1 );
list.add( 2 );
list.add( 3 );
list.printAll();
try
{
for ( size_t pos = 0; ; ++pos )
{
auto value = list.get( pos );
std::cout << "At position " << pos << " there is stored " << value << '\n';
}
}
catch ( const std::exception &ex )
{
std::cout << ex.what() << '\n';
}
}
The program output is
0: 1
1: 2
2: 3
At position 0 there is stored 1
At position 1 there is stored 2
At position 2 there is stored 3
Too big index
Of course you should append the definition of the list with a destructor, copy constructor and the copy assignment operator. In this case you have also explicitly to define the default constructor.
Upvotes: 0
Reputation: 970
Inside get()
you're returning a Node*
instead of T
, inside the if
to be precise.
You should probably do this:
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->data;
current=current->next;
count++;
}
}
You should also handle the case in which the index is not valid, throwing an exception is ok in these cases.
Upvotes: 2