Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
#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()
{
	node *p =new node;
	p->next=last;
    last->data += 1;
		if( last ->data >= 10 )
			p->data += 1;
			cout<<"yay";
}

void linklist ::show()
{
	//int r = 2;
    cout << "number      " <<endl ;
    node *curPtr = first;
  
      /*while( curPtr ) {
            for(int i =0 ;i<3 ;i++){
                 cout << curPtr -> data << "     ";
            }
		         curPtr = curPtr -> next;
		         r ++;
            }*/
    while(curPtr)
    {
		
        cout<<curPtr->data;
        curPtr = curPtr->next;
    }
}

int main()
{
    linklist m;
    m.insert();
    m.show();
    m.add();
    m.show();
    return 0;
}
Posted
Updated 24-Mar-11 0:41am
v3
Comments
Sandeep Mewara 16-Mar-11 8:02am    
Not clear.
scu_sundy 16-Mar-11 9:28am    
Yes,confused
[no name] 16-Mar-11 8:55am    
Wrong inserting (linking).

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 (); if(first == NULL) { first = help ;first->next = NULL;last = first } else { help -> next = last ; help -> data = first -> data ;last=help; } cin >> first -> data ; }}

1 solution

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()
{
	/*node *d = new node ;
	last ->next = NULL ;
	
	d ->data =(last ->data) +1 ;

	if((d ->data) < 10 )
	{
		
		cout << last ->data << "yay" ;
	}*/
    last->data += 1;
}

void linklist ::show()
{
	//int r = 2;
    cout << "number      "  ;
    node *curPtr = first;
  
      /*while( curPtr ) {
            for(int i =0 ;i<3 ;i++){
                 cout << curPtr -> data << "     ";
            }
		         curPtr = curPtr -> next;
		         r ++;
            }*/
    while(curPtr)
    {
        cout<<curPtr->data<<endl;
        curPtr = curPtr->next;
    }
}

int main()
{
    linklist m;
    m.insert();
    m.show();
    m.add();
    m.show();
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900