链接列表-我需要一个程序,当我添加数字时为我打印:129 + 1 = 130 [英] linklist - i need a program that when i add numbers print for me : 129+1=130

查看:96
本文介绍了链接列表-我需要一个程序,当我添加数字时为我打印:129 + 1 = 130的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

推荐答案

您好,我了解到您想要实现一个链表,并希望在列表的最后一个元素中添加1.一种新算法(可以将Google用于高效算法),但我修改了以下代码,仍然存在任何错误(我没有对其进行彻底测试),我想您可以解决

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;
}


这篇关于链接列表-我需要一个程序,当我添加数字时为我打印:129 + 1 = 130的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆