在链接列表中搜索号码 [英] search a number in linklist

查看:61
本文介绍了在链接列表中搜索号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个示例:

There is a sample:

// searchlist.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

class node
{
	friend class linklist ;
	int data ;
	node *next ;
};

class linklist
{
public:
	linklist();
	void insert();
	void serach();
private:
	node *last;
};

 linklist::linklist()
{
	last = NULL ;
 }


 void linklist::insert()
{
	node *p = new node();
	p-> data = NULL ;

		if(last == NULL)
		{
			last = p;
		}
		else
		{
			p -> next = last -> next  ;
			last -> next = p ;
		}
	
	
}
void linklist ::serach()
{
	int n ;
	cout << "Enter the place of the number that you want ?" << endl ;
	cin >> n ;

	int m ;
	cout << "plz enter the numbers of numbers :" << endl ;
	cin >> m ;

	node *cur = last ;
	cur -> next = NULL ;
	while(cur!=NULL )
	{
		for(int i = m ;  n > 0 ; i-- )
		{
			cout << cur->next << "yay" ;
			
		}
	}
}


int main()
{
	int m , o ;
	cout << "plz enter the numbers of numbers :" << endl ;
	cin >> m ;
	cout << "plz enter the numbers : " << endl ;
	for(int i = 0 ; i < m ; i++ )
	cin>>o;
	linklist h;
	h.insert();
	h.serach();

	return 0;
}


1 2 3 4 5 6我想从最后一个开始查找第二个数字,而我的程序给我的答案是错误的


1 2 3 4 5 6 i want to find the second number from the last .and the answer that my program given to me is wrong

推荐答案

我不是惊讶.
您的程序有很多错误.当我键入此示例时,一个例子突然出现:
I''m not surprised.
There is so much wrong with your program. One example that leaps out as I am typing this:
for(int i = m ;  n > 0 ; i-- )
{
    cout << cur->next << "yay" ;
    exit(0);
}

我很高兴知道循环是无关紧要的-您在第一次迭代时退出了应用程序-但是,如果没有,那么"n"会发生什么变化?是什么使"cur"停止以一遍又一遍又一遍地显示同一行?
似乎很难提起这是在另一个无法结束的循环中.

回到开始.检查您的代码,然后通过调试器运行它.我认为您必须先了解自己已完成的所有工作,并查看它是否起作用以及为什么起作用,然后再继续进行操作.

I am so glad that the loop is irrelevant - you exit the application on the first iteration - but if you didn''t, what changes "n"? What moves "cur" to stop it showing the same line, over and over, and over?
That this is inside another loop that can''t end either hardly seems mentioning.

Go back to the beginning. Check your code, and run it through the debugger. I think you have to look at everything you have done, and see if it works, and why it works, before you start moving on.


在讨论代码中的任何问题之前:请删除所有来自类linklist的I/O并相应地修改其接口,否则没有讨论的主题.您不是在开发纯列表类,而是由于此I/O而无法使用的类.有没有听说过关注点分离?

—SA
Before discussing any problems with your code: remove all I/O from the class linklist and modify its interface accordingly, otherwise there is no subject for discussion. You''re not developing pure list class but something that cannot be used due to this I/O. Ever heard of separation of concerns?

—SA


尝试一下:
(请注意,您忘记了清理工作,即作为必须提供清理工作的C++开发人员,删除了列表析构函数中的节点)

Try this:
(please note you forgot the cleanup, i.e. deleting nodes in the list destructor, as C++ developer you HAVE TO provide cleanup)

#include "stdafx.h"
#include<iostream>
using namespace std;
class node
{
    friend class linklist ;
    int data ;
    node *next ;
};
class linklist
{
public:
    linklist();
  ~linklist();
    void insert();
    void serach();
private:
    node *last;
};
linklist::linklist()
{
    last = NULL ;
 }
// cleanup
linklist::~linklist()
{
  node * pcur, * pnext;
  pcur = last;
  while ( pcur )
  {
    pnext = pcur->next;
    delete pcur;
    pcur = pnext;
  }
}

void linklist::insert()
{
  node *p;

    int m ;
    cout << "plz enter the numbers of numbers :" << endl ;
    cin >> m ;

    cout << "plz enter the numbers : " << endl ;
  for(int i = 0 ; i < m ; i++)
    {
    p = new node();
    p->next = NULL;
        cin >> p->data ;
    if(last == NULL)
      {
          last = p;
      }
      else
      {
          p->next = last->next  ;
          last->next = p ;
      }
    }
}
void linklist ::serach()
{
    int n ;
    cout << "Enter the place of the number that you want ?" << endl ;
    cin >> n ;
    node *cur = last ;
    while( n-- )
    {
        if ( cur == NULL) return;
    cur = cur->next;
    }
  cout << "found: " << cur->data << endl;
}

int main()
{
    linklist m;
    m.insert();
    m.serach();
    return 0;
}


这篇关于在链接列表中搜索号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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