Wednesday 25 April 2012

Hello Friends,
Here is a program of linked list in c++ using classes,functions and pointers.
Here, i have created different functions for different functionalities like insertion at first position,at last position and at nth position.
Same with Deletion as well.
Also you can sort the elements.
Here,the program contains different functions of each operation so that the functin can easily be understood.
Here is the some description of the program.

This is the class which is used to store the element and make a pointer to the next node.
It is defined public so that it can be accessed by the ohter classes.
class node{
   public:
       int info;
        node *next;
       };


This LinkL class is used to create the new nodes(instance of Class node) to store the new element.This class contains the function of  all the useful operations which can be applied on the Linklist
class LinkL{
    node *head;    //node refer the first node
    int counter;  //tracks the no. of element in he list
    public:

    LinkL()
    {
        head=NULL;   //defines the initial values to the  1st element
        counter=0;
    }
void insertFirst(int info);  //Inserting the new node at 1st position.
int insertN(int pos,int info); //Inserting the new node at Nth  position.
void insertlast(int info); //Inserting the new node at last position.
void printlist(); //prints the whole list.
void deletefirst(); //delete the first node.
void deletelast(); //delete the last node.
int deleteN(int pos); //delete the nth node
void SortList(); //sorts the list.
    };
  
  
  
How do we initialize the memory of new node??
 Its simple,just create the object of the node class(bcoz node is gonna store the element ) and use the new function to allocate memory.
 node *temp;
 temp=new node();

 or
 node *temp=new node();
 Both are the correct ways.


ImPoRTANT TO LEarn:
If you want to insert any node ,then you need no remember 3 foll steps.
1.)create new node and assign the value of info to it'
 temp1=new node();
 temp1->info=info;
2)update the next node of the new node to the next node of the previous node.
 temp1->next=temp->next;
3.)upsate the next node of the previous node to the new node.
 temp->next=temp1;

 
For eg;
'a b c temp d e f' is your prevois list and you want to insert the node 'temp1' after 'temp'(a b c temp temp1 d e f'),den you need to execute foll steps:
  temp1=new node();
 temp1->info=info;
 temp1->next=temp->next;
 temp->next=temp1;

If you want to delete any node,then you need to remenber the you should alwayz have the track of two nodes so that you can update the next pointer of previous to the next poointer of next node.
If you want to delete the 'temp' from list( 'a b c temp1 temp d e f) ,den you need to execute foll code.
    temp1->next=temp->next;
  
  

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

 // Defined a class named NODE used to store the  values of list.
class node{
   public:
       int info;
        node *next;
       };

//Link List class contains all the functions/operations for list.
    class LinkL{
    node *head;
    int counter;
    public:

    LinkL()
    {
        head=NULL;
        counter=0;
    }
void insertFirst(int info);  //Inserting the new node at 1st position.
int insertN(int pos,int info); //Inserting the new node at Nth  position.
void insertlast(int info); //Inserting the new node at last position.
void printlist(); //prints the whole list.
void deletefirst(); //delete the first node.
void deletelast(); //delete the last node.
int deleteN(int pos); //delete the nth node
void SortList(); //sorts the list.
    };
  
  
    void LinkL::insertFirst(int info){
    
        node *temp;
        temp=new node();
        temp->info=info;   //assining value to temp.
        temp->next=head; //updating the next pointer(it is necessary to update the next pointer so that your list can be made continues.) ,here temp of next is pointing to head
        head=temp;     //now head points to temp
        counter++;  //incrementint the size.
    
    }
  
    int LinkL::insertN(int pos,int info)
    {
    node *temp =new node();
    temp=head; //initialize first noe to head.
        for(int i=1;i<pos-1;i++)  //updating the link of node no the postion where we want to insert the node.
        temp=temp->next;
        if(temp==NULL) //function will return 0 if it doesnt fount the specific location
        {cout<<"No position found..!!!!"; return 0;}
       else if(pos==1){insertFirst(info); //if the inserting position is forst,then we will call the insertfirst function.
           return 1;}
       else {
       node *temp1;
        temp1=new node();
        temp1->info=info;
        temp1->next=temp->next;
        temp->next=temp1;
    
    counter++;
    return 1;
       }
    }
  
    void LinkL::printlist()
    {
        node *temp=head;
        cout<<"Length is :"<<counter<<endl;
        while(temp!=NULL){
        cout<<" " <<temp->info;
        temp=temp->next;        }
    }
  
    void LinkL ::insertlast(int info)
    {
        node *temp=head;
        while(temp->next!=NULL)
        temp=temp->next;
        node *temp1=new node();;
        temp1->info=info;
        temp1->next=NULL;
        temp->next=temp1;
        counter++;
    }
  
    void LinkL ::deletefirst()
    {
        node *temp=new node();;
        temp=head;
        head=temp->next;
        cout<<"first node of link is deleted.....";
        delete temp;
        counter--;
    
    }
 
    void LinkL ::deletelast()
    {
        node *temp=new node();;
        temp=head;
        node *old_temp=new node();
        while(temp->next!=NULL){
        old_temp=temp;
        temp=temp->next;
        }
        old_temp->next=NULL;
        cout<<"last node of link is deleted.....";
        delete temp;
        counter--;
    
    }
    void LinkL::SortList(){
    node *temp=new node();
    node *temp1=new node();
    temp=head;
    temp1=head;
    for(int i=0;temp!=NULL;i++){
    temp1=temp->next;
    for(int j=1;temp1!=NULL;j++)
    {
  
    if(temp->info>temp1->info)
        {
        int t;
        t=temp1->info;
        temp1->info=temp->info;
        temp->info=t;
      
        temp1=temp1->next;
        }
        else
        temp1=temp1->next;
        }
    temp=temp->next;
    }
  
  
}
    int LinkL::deleteN(int pos)
    { node *temp=new node();
    node *temp1=new node();
    temp=head;
    pos=pos-1;
    if (pos>counter){return 0;}
    else{
    while(pos!=0){
    temp1=temp;
    temp=temp->next;
    pos--;
    }
    temp1->next=temp->next;
    cout<<"Node "<<temp->info<<" deleted";
    delete temp;
    counter--;
    return 1;
    }
    }
  
void main()
{
    clrscr();
    LinkL l;
    int i;
  
    char y='y';
      
    char ch;

    do{
        cout<<"\n\n";
        cout<<"0.Quit\n";
        cout<<"1.Insert at first\n";
        cout<<"2.Traverse\n";
        cout<<"3.Insert at last\n";
        cout<<"4.Insert At given position\n";
        cout<<"5.Delete at first node\n";
        cout<<"6.Delete at last node\n";
        cout<<"7.Delete specified number of node\n";
        cout<<"8.Sort nodes\n";

        cout<<"Enter your choice: ";
        cin>>ch;
      switch(ch) {
    case '0' : break;
    case '1' :{
    cout<<"Enter the element to be entered..!!!";
    cin>>i;
            l.insertFirst(i);
   l.printlist();
    break;
    }
    case '2': {
        l.printlist();
        break;
    }
    case '3':{
    cout<<"Enter the element to be entered in LAST..!!!";
    cin>>i;
            l.insertlast(i);
    l.printlist();
    break;
    }
    case '4': {
    int pos;
    cout<<"Enter the position where node to be inserted...";
    cin>>pos;
    cout<<"Enter the data";
    cin>>i;
    int f=l.insertN(pos,i);
    if(f==1){l.printlist();}
    else cout<<"Enter the valid position..";
  
    break;
    }
    case '5' :
    {
        l.deletefirst();
        l.printlist();
        break;
    }
    case '6' : {
        l.deletelast();
        l.printlist();
        break;}
 
  
    case '7' :
    {
        int pos;
    cout<<"Enter the position where node to be deleted...";
    cin>>pos;
  
    int f=l.deleteN(pos);
    if(f==1)
    {cout<<"\nlist is ";
    l.printlist();}
    else
    cout<<"Enter valid value less than the size";
        break;
        }
      
        case '8' :
        {
        cout<<"Sorted list is ";
            l.SortList();
            l.printlist();
            break;
        }
      }
    }while(ch!='0');
     getch();
}

1 comment:

  1. If you have any doubt regarding the program,den coment here.

    ReplyDelete