Rosetta Code talk:Add a Task: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 257: Line 257:
return 0;
return 0;
}
}
==

Revision as of 01:09, 1 June 2019

which implementation ?

STL has already defined list , though . One of the key aspects of Software Engineering is to avoid reinventing the wheel. Reusability is always preferred.

what's doubly list ??

Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer)

Generally, doubly linked list consumes more space for every node and therefore, causes more expansive basic operations such as insertion and deletion. However, we can easily manipulate the elements of the list since the list maintains pointers in both the directions (forward and backward).

In the first element of the list that is i.e. 13 stored at address 1. The head pointer points to the starting address 1. Since this is the first element being added to the list therefore the prev of the list contains null. The next node of the list resides at address 4 therefore the first node contains 4 in its next pointer.

We can traverse the list in this way until we find any node containing null or -1 in its next part.

My own C++ linkedlist implementation

   //- University of Delhi
   //- by Manish Natraj shohbby
   // - c++ list  
   #include <iostream>
   #include  <cstdlib>
   using namespace std;
   template <class T>
   struct node
   {
       T data;
       node * next;
       node * pre;
   };
   template <class T>
   class list
   {
   public:
       class Iterator
       {
       public:
           node<T>* it ;
           Iterator()
           {
               it = NULL;
           }
           void operator ++()
           {
               if(it!=NULL)
                   it = it->next;
           }
           void operator ++(int)
           {
               if(it!=NULL)
                   it = it->next;
           }
           void operator --()
           {
               if(it!=NULL)
                   it = it->pre;
           }
           void operator --(int)
           {
               if(it!=NULL)
                   it = it->pre;
           }
           T& operator * ()
           {
               return it->data;
           }
           bool operator==(const Iterator * x)
           {
               return (it==x.it);
           }
       };
       list()
       {
           node<T> * dummy = new node<T> ;
           head = tail = dummy;
           head->pre =NULL;
       }
       list(T value, int size)
       {
           node<T> * n ;
           node<T> * dummy = new node<T> ;
           head = tail = dummy;
           head->pre =NULL;
           for(int i=0 ; i<size ; i++)
           {
               n = new node<T>;
               n->data = value ;
               n->next = head;
               head->pre = n;
               head = n ;
           }
       }
       ~list()
       {
           node<T> * temp ;
           while (head!=tail)
           {
               temp = head ;
               head = head->next;
               delete temp;
           }
           delete tail;
       }
       void print()
       {
           node<T> * temp = head ;
           while (temp!=tail)
           {
               cout<<temp->data<<"  ";
               temp = temp->next;
           }
       }
       int size()
       {
           int count = 0;
           node<T> * temp = head ;
           while (temp!=tail)
           {
               count++;
               temp = temp->next;
           }
           return count ;
       }
       void insert(T value, Iterator p)
       {
           node<T> * temp = head ;
           node<T> * temp1 ;
           node<T> * n = new node<T> ;
           n->data = value;
           if(head == p.it)
           {
               n->next = head;
               n->pre = NULL;
               head->pre=n;
               head = n;
               return;
           }
           if(tail == p.it)
           {
               tail->data = value;
               tail->next = n;
               n->pre= tail;
               tail = n;
               return;
           }
           while (temp!=p.it)
           {
               temp1 = temp ;
               temp = temp->next;
               if(temp==tail)
               {
                   return;
               }
           }
           temp1->next = n ;
           n->pre= temp1;
           n->next = temp ;
           temp->pre=n;
       }
       void erase(Iterator p)
       {
           node<T> * temp = head ;
           node<T> * temp1 ;
           if(p.it == tail )
           {
               cout<<" no location found "<<endl;
               return;
           }
           else if (p.it == head )
           {
               head = head->next;
               head->pre = NULL ;
               delete temp;
               return;
           }
           while (temp!=p.it)
           {
               temp1 = temp ;
               temp = temp->next;
               if(temp == tail)
               {
                   cout<<" no location found "<<endl;
                   return;
               }
           }
           temp1->next = temp->next;
           temp->next->pre = temp1;
           delete temp;
       }
       list<T>& operator= (const list<T> & l)
       {
           node<T> * temp ;
           node<T> * temp2 = l.head ;
           while (head!=tail)
           {
               temp = head ;
               head = head->next;
               delete temp;
           }
           temp2 = l.tail->pre;
           while (temp2!=NULL)
           {
               insert(temp2->data, begin());
               temp2=temp2->pre;
           }
       }
       Iterator begin ()
       {
           Iterator i ;
           i.it =  head ;
           return i ;
       }
       Iterator end()
       {
           Iterator i ;
           i.it =  tail ;
           return i ;
       }
   private:
       node<T> * head ;
       node<T> * tail ;
   };


   int main()
   {
       list <int> l(3,5);
       list <int> l2(2,4);
       l.print();
       cout<<endl << "size = " << l.size();
       list<int>::Iterator it =  l.begin();
       ++it;
       l.insert(9,it);
       cout << " data = "<< *it << endl ;
       it=l.end();
       l.insert(4,it);
       cout << "data = "<< *it << endl ;
       cout<<endl;
       l.print();
       it=l.end();
       l.insert(7,it);
       cout<<endl;
       cout << " data = "<< *it << endl ;
       l.print();
       it=l.begin();
       l.erase(it);
       cout<<endl;
       l.print();
       it=l.begin();
       it++;
       l.erase(it);
       cout<<endl;
       l.print();
       l2 = l;
       cout<<endl;
       l2.print();
       return 0;
   }