Rosetta Code talk:Add a Task: Difference between revisions

Undo revision 356418 by Yasser Mohammed (talk) Not the place
(Undo revision 356418 by Yasser Mohammed (talk) Not the place)
 
(11 intermediate revisions by 3 users not shown)
Line 22:
 
:::Just for another way to think about it, maybe we can say that Cateories are DB tables where the keys are the page names. Then Properties would be tables where the keys are the page name and an additional value. --[[User:Mwn3d|Mwn3d]] 22:27, 9 November 2010 (UTC)
 
== 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;
}
==
10,327

edits