C++ exercises and solutions: add element to doubly linked list


C++ OOP: Doubly Linked List

Add element to doubly linked list

Step 2: Add an element to the doubly linkedlist

To add an element to the doubly linkedlist, you need to consider 4 things:

1. When the list is empty, to add a new element to the list, you only let the pfirst and plast pointers point to the new item.

C Add an item to the empty doubly list  



2.If the new element is to be added to the beginning of  the list, you will need to let the next link of the new item points to the pfirst, set its previous link to NULL, and then update the pfirst to point to the new item.

C++ Add a new element at the beginning of the doubly linked list  

 3. If the new element is to be added to the middle position of the list, you need to let a pointer move to the position that the new element will be placed in. Then exchange the links as shown below:

C++ Add a new item in the middle of the doubly linkedlist  

4. If the new element is to be added to the last of the list, you need to let the next link of the plast point to the new element, and the previous link of the new element point to the plast. Then update the plast to point the new element.

C++ Add the new item to the last of the ddoubly linked list  

//C code to add a new element to the doubly linkedlist

template <class Type>

void LinkedList<Type>::insertNode (const Type &elem,int pos)

{

  ListElem<Type> *item = new ListElem<Type>(elem);//new element to be inserted

  bool inserted=false;

  //empty list

  if(pfirst==NULL && plast==NULL){

    item->prev=item->next=NULL;

    pfirst=item;

    plast=item;

    inserted=true;

}

//insert new item at the beginning of the list

else if(pos==1)

  {

     item->next=pfirst;

     item->prev=NULL;

     pfirst=item;

     inserted=true;

   }

//insert new item between items

else if(pos>1 && pos<=countitem()){

     ListElem<Type> *ta;

     ta=pfirst;

     for(int t=1;t<pos;t=t+1){ta=ta->next;} //move to the insertion point

     item->next=ta;

     item->prev=ta->prev;

     ta->prev->next=item;

     ta->prev=item;

     inserted=true;

   }

//insert new item at the end of the list

else if(pos==countitem()+1){

    item->next=NULL; //The next link of the item is null.

    plast->next=item;

    item->prev=plast;

    plast=item;

    inserted=true;

   } 

//show message if position is not valid

 else cout<<"Invalid position! Position must be between 1 and "<<countitem()+2<<"\n";

 if(inserted!=false) cout<<"Inserted\n"; 

}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.