1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Linked list search

Discussion in 'C++' started by appleLady, Apr 6, 2011.

  1. appleLady

    appleLady New Member

    Joined:
    Apr 6, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<iostream>
    
    
    using namespace std;
    
    typedef struct node
    {
    	int data;	// will store information
    	node *next;	// the reference to the next node
    };
    
    
    int main()
    {
    	node *head = NULL;	//empty linked list
    	int info = 0, node_number = 0,  counter = 0, value=0;
    	char choice;
    
    	do{
    		cout<<"***********************************************" << endl;
    		cout<<"*   1.Insert at first                         *" << endl;
    		cout<<"*   2.Insert at last                          *" << endl;
    		cout<<"*   3.Insert after specified number of node   *" << endl;
    		cout<<"*   4.Delete at first node                    *" << endl;
    		cout<<"*   5.Delete at last node                     *" << endl;
    		cout<<"*   6.Delete specified number of node         *" << endl;
    		cout<<"*   7.Display data                            *" << endl;
            cout<<"*   8.Search                                  *" << endl;
    		cout<<"*   0.Exit                                    *" << endl;
    		cout<<"***********************************************";
    
    		cout<<"\nPlease enter your choice: ";
    		cin>>choice;
    
    		switch(choice)
    		{
                case '0':{
                            return 0;
                            break;
                          }
    
    			case '1':{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
    						cout<<"Enter any number: ";
    						cin>>info;					
    						node *temp;	
                           	temp = (node*)malloc(sizeof(node));	
    						temp->data = info;					
    						temp->next=head;					
    						head = temp;						
    						
    						cout<<"You have successfully insert " <<info<<endl;
    						cout<<"Now "<<info<< " is stored at node " <<++counter<<endl;
                            cout<<"Here is your list : " <<endl;
    					    for(temp = head ; temp!=NULL ; temp = temp->next) 
    	                    {
    		                          cout<<temp->data<<" ";
                            }
    
    	                    cout<<endl;
                            cout<<endl;		
    	                    cin.get();
    						break;
    					}
    
    			case '2':{
    						if(head==NULL)
    						{
    							cout<<"Enter any number:";
    							cin>>info;						
    							node *temp;						
    							temp = (node*)malloc(sizeof(node));	
    							temp->data = info;				
    							temp->next = NULL;				
    							head = temp;				
    							counter++;
    						}
    
    						else
    						{
    							cout<<"Enter any number:";
    							cin>>info;						
    							node *temp1;					
    							temp1=(node*)malloc(sizeof(node));	
    							temp1 = head;					
    							while(temp1->next!=NULL)			
    								temp1 = temp1->next;			
    
    							node *temp;					
    							temp = (node*)malloc(sizeof(node));
    							temp->data = info;			
    							temp->next = NULL;				
    							temp1->next = temp;				
    							
    							cout<<"You have successfully insert " <<info<<endl;
    						    cout<<"Now "<<info<< " is stored at node " <<++counter<<endl;
                                cout<<"Here is your list : " <<endl;
    					        for(temp = head ; temp!=NULL ; temp = temp->next) 
    	                        {
    		                          cout<<temp->data<<" ";
                                }
    
    	                        cout<<endl;
                                cout<<endl;		
    	                        cin.get();
    							break;
    						}
    					}
    
    
    			case '3':{
    						if(head==NULL)
    						{
    							cout<<"The Linked List is empty"<<endl;
    							break;
    						}
    
    						cout<<"Enter any number:";
    						cin>>info;						    
    						cout<<"Enter the node number:";
    						cin>>node_number;						
    
    						node *temp1;							
    						temp1 = (node*)malloc(sizeof(node));	
    						temp1 = head;
    
    
    						for( int i = 1 ; i < node_number ; i++ )
    						{
    							temp1 = temp1->next;				
    
    							if( temp1 == NULL )
    							{
    								cout<<node_number<<" node is not exist"<< endl;
    								break;
    							}
    						}
    
    						 node *temp;						    
    						 temp = (node*)malloc(sizeof(node));	
    						 temp->data = info;					
    						 temp->next = temp1->next;			
    						 temp1->next = temp;			
    						 counter++;
    						 
    						 cout<<"You have successfully insert " <<info<<endl;
    						 cout<<"Now "<<info<< " is stored at node " <<++counter<<endl;
                             cout<<"Here is your list : " <<endl;
    					     for(temp = head ; temp!=NULL ; temp = temp->next) 
    	                     {
                                  cout<<temp->data<<" ";
                             }
    
    	                    cout<<endl;
                            cout<<endl;		
    	                    cin.get();
                            break;
    					}
    
    
    			case '4':{
    						if(head==NULL)
    						{
    							cout<<"The Linked List is empty"<<endl;
    							break;
    						}
    						else	
    							if(head->next==NULL)
    							{
    								head = NULL;
    								cout<<"The first node of the Linked List is deleted"<<endl;
    								cout<<"The Linked List is empty"<<endl;
    								counter--;
    								break;
    							}
    
    						node *temp;							
    						temp = (node*)malloc(sizeof(node));	
    						temp = head;						
    						head = temp->next;					
    						free(temp);
    						cout<<"The first node of the Linked List is deleted"<<endl;
    						counter--;
    						
    						cout<<"You have successfully delete " <<info<<endl;
                            cout<<"Here is your list now: " <<endl;
    					    for(temp = head ; temp!=NULL ; temp = temp->next) 
    	                    {
    		                          cout<<temp->data<<" ";
                            }
    
    	                    cout<<endl;
                            cout<<endl;		
    	                    cin.get();
    						break;
    					}
    
    		   case '5':{
    						if(head==NULL)
    						{
    							cout<<"The Linked List is empty"<<endl;
    							break;
    						}
    						else	 
    							if(head->next==NULL)
    							{
    								head = NULL;
    								cout<<"The last node of the Linked List is deleted"<<endl;
    								cout<<"The Linked List is empty"<<endl;
    								counter--;
    								break;
    							}
    
    
    						node *temp1;							
    						temp1 = (node*)malloc(sizeof(node));	
    						temp1 = head;							
    
    						node *old_temp;							
    						old_temp = (node*)malloc(sizeof(node));	
    
    						while(temp1->next!=NULL)			
    						{
    							old_temp = temp1;			
    							temp1 = temp1->next;			
    						}
    
    						old_temp->next = NULL;			 
    
    						free(temp1);
    						cout<<"The last node of the Linked List is deleted"<<endl;
    						counter--;
    						
    						cout<<"You have successfully delete " <<info<<endl;
                            cout<<"Here is your list now: " <<endl;
    					    for(temp1 = head ; temp1!=NULL ; temp1 = temp1->next) 
    	                    {
    		                          cout<<temp1->data<<" ";
                            }
    
    	                    cout<<endl;
                            cout<<endl;		
    	                    cin.get();
    						break;
    					}
    
    
    		 case '6':{
    						if(head==NULL)
    						{
    							cout<<"The Linked List is empty"<<endl;
    							break;
    						}
    						else	
    							if(head->next==NULL)
    							{
    								head = NULL;
    								cout<<"The last node of the Linked List is deleted"<<endl;
    								cout<<"The Linked List is empty"<<endl;
    								counter--;
    								break;
    							}
    
    						cout<<"ENTER THE NODE NUMBER:";
    						cin>>node_number;						
    
    						if(node_number > counter)
    						{
    							cout<<"No such node is exist";
    							break;
    						}
    
    						node *temp1;							
    						temp1 = (node*)malloc(sizeof(node));	
    						temp1 = head;						
    						node *old_temp;							
    						old_temp = (node*)malloc(sizeof(node));	
    						old_temp = temp1;						
    
    						if( node_number == 1 )
    						{
    							head = temp1->next;					
    							free(temp1);
    							counter--;
    							cout<<node_number<<" node of the Linked List is deleted"<<endl;
    							break;
    						}
    
    						for( int i = 1 ; i < node_number ; i++ )
    						{
    
    							old_temp = temp1;				
    							temp1 = temp1->next;				
    
    						}
    
    						old_temp->next = temp1->next;		 
    						free(temp1);
    
    						counter--;
    						cout<<node_number<<" node of the Linked List is deleted"<<endl;
    						
    						cout<<"You have successfully delete " <<info<<endl;
                            cout<<"Here is your list now: " <<endl;
    					    for(temp1 = head ; temp1!=NULL ; temp1 = temp1->next) 
    	                    {
    		                          cout<<temp1->data<<" ";
                            }
    
    	                    cout<<endl;
                            cout<<endl;		
    	                    cin.get();
                            break;
    
    					 }
    
    
    		case'7':{
    					node *temp1;							
    					temp1 = (node*)malloc(sizeof(node));	
    
    					node *temp2;							
    					temp2 = (node*)malloc(sizeof(node));	
    
    					int temp = 0;							
    
    					for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
    					{
    						for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next )
    						{
    							if( temp1->data > temp2->data )
    							{
    								temp = temp1->data;
    								temp1->data = temp2->data;
    								temp2->data = temp;
    							 }
    						 }
    				   }
    
    					cout<<"Lists of your number "<<endl;
    					for(temp1 = head ; temp1!=NULL ; temp1 = temp1->next) 
    	             {
    		               cout<<temp1->data<<" ";
                     }
    
    	             cout<<endl;		
    	             cin.get();
    					break;
    
    				}
    				
            case '8':{
                      node *temp1;
                      temp1 = (node*)malloc(sizeof(node));
                      temp1 = head;
                      bool find = false;
                      
                      cout<<"Enter the value ";
                      cin>>value;
                      
                      while(temp1!=NULL)
                      {
                          if(temp1->data == value)
                          find = true;
                          temp1 = temp1->next;
                      }
                      
                      return find;
            
                      break;
                      }
                         
    
    		}
    
    
    	 }while(choice!='0');
    
    
      
        
    	return 0;
    }
    
    the code can compile without problem. but why did my "search" didnt work? did i miss something. i need to search the value entered. Example, search 10, the smallest value and the largest value. Please help.
     
  2. mthushara

    mthushara New Member

    Joined:
    Aug 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    //Please change your case 8 as follows.

    Code:
    case '8':{
                      node *temp1;
                      temp1 = (node*)malloc(sizeof(node));
                      temp1 = head;
                      bool find = false;
                      
                      cout<<"Enter the value ";
                      cin>>value;
                      
                      while(temp1!=NULL)
                      {
                          if(temp1->data == value)
    					  {
    							find = true;
    							break;
    					  }
                          temp1 = temp1->next;
                      }
                      
                      //return find;
    				  if(find)
    				  {
    						cout<<"Found your number "<<value<<endl;
    				  }
    				  else
    				  {
    						cout<<"Couldn't find your number "<<value<<endl;
    				  }
    				  //
    
            
                      break;
                      }
     
  3. appleLady

    appleLady New Member

    Joined:
    Apr 6, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thanks!! now i need to create code for searching smallest & largest number! (^.^)v
     

Share This Page