c++ - I wrote this program to reverse the elements in a linked list, after compiling this program this is showing error in the reverse (). why? -
i wrote program reverse elements in linked list, after compiling program showing error in reverse (). why?
error showing :- ||in function ‘void reverse()’:| |40|error: no match ‘operator=’ (operand types ‘node’ , ‘long int’)| |40|note: candidate is:|
#include<iostream> using namespace std; struct node { int data; node* next; }; node* head; void insert(int data) { node* newnode = new node(); // create new node newnode->data = data; newnode->next = null; if(head == null ){ //if head null, means first node head = newnode; //so update value of head }else{ node* temp = head; while(temp->next!=null) { temp = temp->next; } temp->next = newnode; } } void print() { node* temp = head; while(temp!=null) { cout<<" "<<temp->data<<" "; temp = temp->next; } cout<<endl; } void reverse() { node* current; node* prev; node* nextaddress; current = head; prev = null; while(current!=null) { nextaddress = current->next; current->next = prev; prev = current; current = nextaddress; } head = prev; } int main() { head = null; int a, n; cout<<"\n enter number of elements stored list :"; cin>>n; cout<<"\n enter elements :"; for(int i=1; i<=n; i++) { cin>>a; insert(a); } print(); cout<<"\n reverse of linkedlist :"; reverse(); print(); return 0; }
this why every coding standard says 1 variable per line.
what wrote:
node* current , prev, nextaddress; current = head; prev = null;
what meant was:
node* current , * prev, * nextaddress; // ^^ ^^ without star node objects. current = head; prev = null;
what should have typed was:
node* current = head; node* prev = nullptr; node* nextaddress;
hey not take more space.
Comments
Post a Comment