I was trying out this singly linked list program in c++ using class ,I am getting error as " no match for operator operand types are 'Node' and 'int'.I didnt quite understand this as this is my first time that I am dealing with classes and linked list..I want to input number of nodes by the user and then create and object of the class and call the methods.
#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node * head;
Node()
{
data=0;
next=NULL;
head=NULL;
}
void insert_at_end(int x)
{
Node * newNode=new Node();
newNode->data=x;
newNode->next=NULL;
//head=NULL;
if(head==NULL)
{
head=newNode;
}
else{
Node*temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newNode;
}
}
void print()
{
cout<<"List is";
Node *temp=head;
while(temp->next!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
};
//Node * head=NULL;
int main()
{
int n;
cout<<"enter number of nodes to be inserted";
cin>>n;
//Node n1;
for(int i=0;i<n;i++)
{
Node n1;
int x;
cout<<"enter number to be inserted";
cin>>x;
n1[i].insert_at_end(x);
n1[i].print();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire