Tuesday, 8 October 2013

insert at start and end in Link list by head and tale method with clases in c++

In this program user can insert data at start and end in link list by using head and tale method.user can also display and search data in link list.It is complete program with Menu.

 

#include<iostream>
using namespace std;

class Node{
public:
      int data;
      Node *nextptr;
};
      
class Linklist {
      Node *head;
      Node *tale;
              
      public:
              Linklist();
              Node* insertAtStart(int element);
              Node* insertAtEnd(int element);
             ~Linklist();
              void print();
              bool search(int element);
              Node* First_Record(int element);
               friend class Node;
};

Linklist::Linklist()
{
   head=NULL;
}



Node* Linklist::First_Record(int element)
{
    
    Node *ptr;
    ptr=new Node;
    ptr->data=element;
    ptr->nextptr=NULL;
    head=tale=ptr;
    return ptr;
}

Node* Linklist::insertAtStart(int element)
{
    if(head==NULL)
    {
        
        return First_Record(element);
    }
    Node *ptr;
    ptr=new Node;
    ptr->data=element;
    ptr->nextptr=head;
    head=ptr;
    return ptr;
    
}

Node* Linklist::insertAtEnd(int element)     
{
  Node *ptr;
    if(head==NULL)
        return First_Record(element);
    ptr=new Node;
    ptr->data=element;
    ptr->nextptr=NULL;
    tale->nextptr = ptr;
    tale= ptr;
    return ptr;
}

void Linklist::print()
{
     Node *temp=head;
  if(head!=NULL)
  {
    while(temp!=NULL)
    {
      cout<<temp->data<<" "<<endl;
        temp=temp->nextptr;                 
    }              
  } 
  else
  cout<<"Ther is no data in list\n"<<endl;    
}
 Linklist::~Linklist()
{
     Node *temp;
     
  if(head!=NULL)
  {
      
    while(head!=NULL)
    {
      temp=head;
        head=head->nextptr; 

        delete temp;
    }              
  }    
}
bool Linklist::search(int element)
{
     Node *temp;
   
     if(head!=NULL)
     {
         temp=head;
                   while( temp!=NULL)
                       {
                            if(temp->data==element)
                      return true;
                          temp=temp->nextptr;
                   
                   
                   }
                   return false;
                   }
                   else 
                   return false;
}

int main()
{
    int choice=0;
    int data;
    Linklist object;
 cout<<"enter your choice"<<endl; 
 while(1)
 {
cout<<"\nMenu:\n1: insert at start"<<endl; 
cout<<"2: insert at end"<<endl;
cout<<"3: print elements"<<endl;
cout<<"4: search a elelment"<<endl;
cout<<"5: delete linlist"<<endl;
cout<<"6: exit"<<endl;
cout<<"enter your choice"<<endl; 
cin>>choice;
switch(choice)
{
case 1:
    cout<<"enter number"<<endl;
    cin>>data;
    object.insertAtStart(data);
    break;
    case 2:
        cout<<"enter number"<<endl;
        cin>>data;
        object.insertAtEnd(data);

        break;
        
            case 3:
cout<<"element are"<<endl;

        object.print();
        break;
        case 4:
        cout<<"enter number"<<endl;
        cin>>data;
        if(object.search(data))
        {
            cout<<"number found in the list";
        }
        else
            cout<<"number not found in the list";

        break;
        case 5:
        
            object.~Linklist();
            break;
            
                case 6:
                exit(1);
                    break;
                default:
                    cout<<"invalid choice"<<endl;
                    break;
}

 }
system("pause");
}
                   

0 comments:

Post a Comment