Click here to Skip to main content
16,017,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// nomi az akhar.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 search();
	void show();

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::search()
{
	//chand ta adad begire n omin adad az akharo prinnt kone
	int n , m ;
	cout <<"plz enter the numbers of numbers :" << endl ;
	cin >> n;
	cout <<"plz enter number of gereh :" << endl ;
	cin >> m;
	node *p = first ;
	//while ( p != NULL )
	//{
		if ( n > m)
		{
			p = p -> next ;
		}
		else
		{
			cout <<"yay"<< p->next-> data ;	
		}
		
//	}
	cout <<"yay"<<p;
}

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<<endl;
        curPtr = curPtr->next;
	
	}
}
int main()
{
	
    linklist m;
    m.insert();
    m.show();
	m.search();
	
	return 0;
}


this program must search the number that user write (from the last to first)
for example
1 2 3 4 5 6
the user want the second number
the the program print for user 5.
Posted
Comments
Harrison H 24-Mar-11 19:27pm    
This is very similar to a linked list question posted a couple of weeks ago. Also, it uses the EXACT same codebase (even the "plz" in the question). Where are you guys getting this crappy code to work from? This MUST be a student assignment, but I'd be baffled if the teacher gave you this code to work from.

Since you seem stuck, I'll give you a basic idea where to start

Change your insert method to accept an int, and perform insertion from outside.
C++
class linklist
{
    ...
    void insert(int val);
}

void outerfunction()
{
    for (size_t i = 0; i < max; ++i)
    {
        int aValue;
        cin >> aValue;
        mylinklist.insert(aValue);
    }
}


Create access functions to your data. This can be done in a number of ways:
1. Access by index
C++
int getAt(size_t index) const; // For read-only access
int& getAt(size_t index); // for read/write access

2. Expose pointers to your internal structure (node), but encapsulate it well.
Either implement something like iterators a la stl, or create your own type like POSITION as used by the MFC framework. The latter might be easier to implement if you're new to C++. All you have to do is to cast you node pointers to a POSITION type, and only expose that type from your list. To access the data you can accept such a POSIION value in a method, and type cast it back to a node pointer.

C++
typedef void* POSITION; // No-one outside should know what this is
POSITION getHead() { return first; }
int getAt(POSITION p) const { return ((node*) p)->data; } // Cast to node*, and access data

void next(POSITION &p) const { p = ((node*) p)->next; }

Don't forget to do NULL checks on parameters.

Even though the iterator implementation is harder, you will have the benefit of using standard algorithms such as searching and sorting, without the need of implementing them yourself.

Now, your show() function can then be moved out of the class, and only use the access functions you have created.

Also, no-one outside your linklist class needs to know about the internals.
Move the declaration of node to inside the linklist class and make it protected.

If you do all this properly, you will have no problem making it a template class, which can hold other data types. Not just ints.

Hope this will get you in the right direction.
 
Share this answer
 
v2
To start with: remove I/O operations from you list class. Ever heard of Separation of Concerns?
Such universal type should be completely abstracted from anything which is not the list operations.

Sorry I'm not answering your Question about errors in search. In the present form, your class simply does not deserve consideration. Remove I/O first, then design proper set of the class operations, all the method signatures. Only when it's done you can worry about implementation detail.

—SA
 
Share this answer
 
Comments
yay hello 25-Mar-11 2:51am    
sorry , i do not know what do you mean for remove I/O operations ?
you mean i must do something in insert ?
Sergey Alexandrovich Kryukov 25-Mar-11 3:08am    
What is "cin", "cout", I/O or not? Remove it from interface of list.
--SA
yay hello 25-Mar-11 3:17am    
I must write them in the main?
Sergey Alexandrovich Kryukov 25-Mar-11 12:27pm    
Whenever you need for testing. Thing about your library. Separate user code from the library. You could not use your list in Windows application, Service -- nowhere (in good console app, too). Why writing such things, even if this is a school exercise or something?
--Sa

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