Hi what i understood is you want to implement a linked list and want to add 1 to the last element in list.I am giving a new algorithm(you can use Google for efficient algorithm) but i modified the code as below,Still if there are any bugs(i did not tested it thoroughly) , I think you can solve it
#include "stdafx.h"
#include<iostream>
using namespace std;
class node
{
friend class linklist ;
int data ;
node *next ;
};
class linklist
{
public:
linklist() ;
void insert() ;
void show();
void add();
private:
node *last ;
node *first ;
};
linklist::linklist()
{
last = first = NULL ;
}
void linklist ::insert()
{
int n ;
cout <<"plz enter the numbers of numbers :" << endl ;
cin >> n;
cout << "enter the numbers :" << endl ;
for(int i =0 ; i < n ; i++)
{
node *help = new node ();
cin >> help -> data ;
help->next = NULL;
if(first == NULL)
{
first = help ;
last = help;
}
else
{
node* temp = first;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = help;
last = help;
}
}
}
void linklist ::add()
{
last->data += 1;
}
void linklist ::show()
{
cout << "number " ;
node *curPtr = first;
while(curPtr)
{
cout<<curPtr->data<<endl;
curPtr = curPtr->next;
}
}
int main()
{
linklist m;
m.insert();
m.show();
m.add();
m.show();
return 0;
}