I was solving this problem on Linked List on hacker rank : https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/
And got correct answer for the given code . Most of the code was prewritten ; I only had to complete the function insertNodeAtHead function (enclosed between the three stars) :
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;
node = node->next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
// Complete the insertNodeAtHead function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
***SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
SinglyLinkedListNode *nnode;
nnode = new SinglyLinkedListNode(data);
if(llist !=NULL)
nnode->next=llist;
return llist=nnode;
}***
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
SinglyLinkedList* llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
SinglyLinkedListNode* llist_head = insertNodeAtHead(llist->head, llist_item);
llist->head = llist_head;
}
print_singly_linked_list(llist->head, "\n", fout);
fout << "\n";
free_singly_linked_list(llist->head);
fout.close();
return 0;
}
I had to complete the insertNodeAtHead() function only . Rest all was already there. But when I tried to do it without using pointer nnode object( my identifier) of SinglyLinkedListNode class type(their predefined class) I get compilation error:
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode * next;
SinglyLinkedListNode(int node_data) {
this - > data = node_data;
this - > next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode * head;
SinglyLinkedListNode * tail;
SinglyLinkedList() {
this - > head = nullptr;
this - > tail = nullptr;
}
};
void print_singly_linked_list(SinglyLinkedListNode * node, string sep, ofstream & fout) {
while (node) {
fout << node - > data;
node = node - > next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode * node) {
while (node) {
SinglyLinkedListNode * temp = node;
node = node - > next;
free(temp);
}
}
// Complete the insertNodeAtHead function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
***SinglyLinkedListNode * insertNodeAtHead(SinglyLinkedListNode * llist, int data) {
SinglyLinkedListNode nnode;
nnode = new SinglyLinkedListNode(data);
if (llist != NULL)
nnode.next = llist;
return llist = & nnode;
}***
int main() {
ofstream fout(getenv("OUTPUT_PATH"));
SinglyLinkedList * llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits < streamsize > ::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits < streamsize > ::max(), '\n');
SinglyLinkedListNode * llist_head = insertNodeAtHead(llist - > head, llist_item);
llist - > head = llist_head;
}
print_singly_linked_list(llist - > head, "\n", fout);
fout << "\n";
free_singly_linked_list(llist - > head);
fout.close();
return 0;
}
ERROR AFTER COMPILATION:
solution.cc: In function ‘SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode*, int)’: solution.cc:63:24: error: no matching function for call to ‘SinglyLinkedListNode::SinglyLinkedListNode()’ SinglyLinkedListNode nnode; ^~~~~ solution.cc:10:9: note: candidate: ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’ SinglyLinkedListNode(int node_data) { ^~~~~~~~~~~~~~~~~~~~ solution.cc:10:9: note: candidate expects 1 argument, 0 provided solution.cc:5:7: note: candidate: ‘constexpr SinglyLinkedListNode::SinglyLinkedListNode(const SinglyLinkedListNode&)’ class SinglyLinkedListNode { ^~~~~~~~~~~~~~~~~~~~ solution.cc:5:7: note: candidate expects 1 argument, 0 provided solution.cc:5:7: note: candidate: ‘constexpr SinglyLinkedListNode::SinglyLinkedListNode(SinglyLinkedListNode&&)’ solution.cc:5:7: note: candidate expects 1 argument, 0 provided solution.cc:64:40: error: ambiguous overload for ‘operator=’ (operand types are ‘SinglyLinkedListNode’ and ‘SinglyLinkedListNode*’) nnode = new SinglyLinkedListNode(data); ^ solution.cc:5:7: note: candidate: ‘SinglyLinkedListNode& SinglyLinkedListNode::operator=(const SinglyLinkedListNode&)’ class SinglyLinkedListNode { ^~~~~~~~~~~~~~~~~~~~ solution.cc:5:7: note: conversion of argument 1 would be ill-formed: solution.cc:64:11: error: invalid user-defined conversion from ‘SinglyLinkedListNode*’ to ‘const SinglyLinkedListNode&’ [-fpermissive] nnode = new SinglyLinkedListNode(data); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.cc:10:9: note: candidate is: ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’ SinglyLinkedListNode(int node_data) { ^~~~~~~~~~~~~~~~~~~~ solution.cc:10:9: note: conversion of argument 1 would be ill-formed: solution.cc:64:11: error: invalid conversion from ‘SinglyLinkedListNode*’ to ‘int’ [-fpermissive]
nnode = new SinglyLinkedListNode(data); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.cc:64:11: error: invalid conversion from ‘SinglyLinkedListNode*’ to ‘int’ [-fpermissive] solution.cc:10:34: note: initializing argument 1 of ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’ SinglyLinkedListNode(int node_data) { ~~~~^~~~~~~~~ solution.cc:5:7: note: candidate: ‘SinglyLinkedListNode& SinglyLinkedListNode::operator=(SinglyLinkedListNode&&)’ class SinglyLinkedListNode { ^~~~~~~~~~~~~~~~~~~~ solution.cc:5:7: note: conversion of argument 1 would be ill-formed: solution.cc:64:11: error: invalid user-defined conversion from ‘SinglyLinkedListNode*’ to ‘SinglyLinkedListNode&&’ [-fpermissive] nnode = new SinglyLinkedListNode(data); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.cc:10:9: note: candidate is: ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’ SinglyLinkedListNode(int node_data) { ^~~~~~~~~~~~~~~~~~~~ solution.cc:10:9: note: conversion of argument 1 would be ill-formed: solution.cc:64:11: error: invalid conversion from ‘SinglyLinkedListNode*’ to ‘int’ [-fpermissive]
nnode = new SinglyLinkedListNode(data); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.cc:64:11: error: invalid conversion from ‘SinglyLinkedListNode*’ to ‘int’ [-fpermissive] solution.cc:10:34: note: initializing argument 1 of ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’ SinglyLinkedListNode(int node_data) { ~~~~^~~~~~~~~ solution.cc:64:40: error: conversion to non-const reference type ‘class SinglyLinkedListNode&&’ from rvalue of type ‘SinglyLinkedListNode’ [-fpermissive] nnode = new SinglyLinkedListNode(data);
Exit Status 255
I firmly believed that it should have worked but it didn't happen so. Since the logic was same , the only difference was I didn't use pointer to create the object. Since I am new to C++ I am unable to figure out why is this happening. Please give an insight.