Rosetta Code talk:Add a Task

From Rosetta Code

I've been asked numerous times about how to create a task. I've never had a good answer. This page represents a draft of my best idea of it.

  • Please examine, edit, refine, review and debate it.
  • Go through our existing set of tasks and use this page as a benchmark for gauging task quality. If the task seems of good quality (i.e. matches the prerequisites), despite not matching this page well, then the page likely requires modification.
  • Build a list (somewhere, anywhere, wikicode or semantic query, be as practical or creative as necessary) where we can see our existing task set and see how well the task description matches this page. --Michael Mol 17:41, 20 September 2010 (UTC)

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.

Other Algorithms

There needs to be a mention of the use of 'other' algorithms when the task allows - for example using regexps instead of an example using string search; or some other technique when brute-force search is the current solution method. We usually welcome them -sometimes on their own - other times in addition to other methods of solution. (Various Knapsack problem solutions have better methods than brute-force search for example). --Paddy3118 15:59, 8 October 2010 (UTC)

Suggest creating a "what to expect" section, noting common variations and the like, noting that the task author is likely to be surprised by creative (but not necessarilly inappropriate) solutions. "other algorithms" would fall under that, pretty much as a single bullet point. --Michael Mol 16:24, 8 October 2010 (UTC)

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.

C++ linkedlist implementation by me

== //- 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;
   }
==