Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 i want to find the second number from the last .and the answer that my program given to me is wrong
Posted
Updated 5-Apr-11 21:46pm
v3

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);
}
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 3:35am    
The code is so helpless, so pointing out the first non-fixable problem is quite enough, my 5.
I've done something similar, please see.

By the way, it happens second time here. Last time I failed to convince the patient to remove I/O from the similar class. It it is not apparent why, I don't know what to talk about...
--SA
OriginalGriff 6-Apr-11 3:58am    
Hi SA! Maybe it's a little early for him to think about code reuse: would you want to reuse this code? :laugh:
Congratulations on reaching the top of both leaderboards BTW - I said you would!
Sergey Alexandrovich Kryukov 6-Apr-11 4:06am    
Thank you.
I was thinking: what to answer you about re-use? Looks like you're right, to early, but... something's wrong.
You see, thinking about real reuse may be to earlier. But this is not lack of reuse, this is anti-reuse! Do you see the difference?
--SA
OriginalGriff 6-Apr-11 4:25am    
Oh I get it, trust me! I think I could re-use this code, but only in the "Hall of Shame" - which would be too cruel on beginners :laugh:
Sergey Alexandrovich Kryukov 6-Apr-11 16:15pm    
Of course I trust you on that. As to the rest... not that I can help it. See also my comment to the CPallini's comment on my Answer... :-)
Thank you.
--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
 
Share this answer
 
Comments
CPallini 6-Apr-11 4:36am    
You know, I agree with you (have my 5). However, here we've even more basic technical gaps to fill, in my opinion.
Sergey Alexandrovich Kryukov 6-Apr-11 15:46pm    
Thank you very much.
You are absolutely right about "even more basic technical gaps to fill". But -- are we able to fill then in the CodeProject format? Hardly, I'm afraid, so...
I simply used the principle "I don't have to eat the whole egg to tell its bad". Can I?

Thanks again.
--SA
CPallini 6-Apr-11 16:10pm    
Yes, you can.
Try this:
(please note you forgot the cleanup, i.e. deleting nodes in the list destructor, as C++ developer you HAVE TO provide cleanup)

C++
#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;
}
 
Share this answer
 
v2

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