Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
using namespace std;
class SLLIST;
class node
{
	private: int ele;
			 node *addrs;
	public: friend class SLLIST;
};
class SLLIST
{
	private: node *head;
	public: 
			SLLIST()
			{	head='\0';
			}
			void insert_beg();
			void insert_end();
			void display();
			bool search(int);
};
void SLLIST::insert_beg()
{
	node *temp=new node();
	cout<<"enter element:"<<endl;
	cin>>temp->ele;
	temp->addrs='\0';
 
	if (head=='\0') head=temp;
	else
	{	node *t=new node();
		t=head;
		temp->addrs=t;
	}
}
void SLLIST::insert_end()
{	
	node *temp=new node();
	cout<<"enter element:"<<endl;
	cin>>temp->ele;
	temp->addrs='\0';
 
	if (head='\0')	head=temp;
	else
	{
		node *t=new node();
		t=head;
		while(t->addrs!='\0')	t=t->addrs;
		t->addrs=temp;
	}
}
void SLLIST::display()
{
	node *t=new node();
	t=head;
	if (head=='\0')	cout<<"list is empty"<<endl;
	else
	{	while(t->addrs!='\0')	
			{	cout<<t->ele<<endl;
				t=t->addrs;
			}
	         cout<<t->ele<<endl;
	}
}
bool SLLIST::search(int key)
{
	node *t=new node();
	t=head;
	if(head=='\0')	return false;
	else if((t->addrs=='\0')&&(t->ele==key))	return true;
	else
	{
		while(t->addrs!='\0')
		{	if(key==t->ele)	return true;
			else t=t->addrs;
		}
		return false;
	}
}
int main() {
	SLLIST s;
	s.insert_beg();

	s.insert_beg();
	s.insert_end();
	char ch=s.search(56);
        if (ch=="true")    cout<<"key found";
        else cout<<"key not found";

        s.display();
        return 0;
}
Posted
Updated 13-Sep-14 1:21am
v3
Comments
[no name] 13-Sep-14 7:16am    
And what would you expect us to do with this? Your code is incomplete, unformatted, no explanation for your problem, no example input, no description of your output. Learn to use the debugger, that is a valuable skill for you to acquire.
Theja_3895 13-Sep-14 7:23am    
i am a beginner and hope i get suggestions to correct myself.thanks anyway!
[no name] 13-Sep-14 7:30am    
Get yourself a decent book on programming and work through it. We can't teach you programming over the internet one code dump at a time. And learn to read FAQs before posting.

You need to go back to your notes and have a good read - then have a good look at your code and think about what you are trying to do.

Your search does nothing, or at least, nothing useful - because you don't bother with the result it returns, it doesn't matter what it does return.

But then - you don't insert anything useful either, and your insert routine(s) don't look like they should work either.

Start with the debugger: put a breakpoint on the first line in main and step through your code - work out before your execute a line what you expect it to do, then check that is exactly what it did afterwards. If it didn't do that, why not?

I'm not going to fix this for you, certainly not yet: there are too many problems and too little thought gone into it - and you need to learn how to use the debugger to find out what you did wrong anyway.

Give it a try: it's not difficult, but it's a powerful skill, and one which is well worth learning - it will save you a lot of time later.
 
Share this answer
 
 
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