1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#ifndef LIST_H
#define LIST_H
#include <iostream>
template <class T>
class List
{
private:
struct Node
{
Node *prev;
T elem;
Node *next;
Node(Node *aprev, const T& aelem, Node *anext):
prev(aprev),elem(aelem), next(anext)
{
}
};
/*class Iter
{
friend class List;
Iter():p(0)
{
}
Iter(Node *ap):p(ap)
{
}
const Iter& operator++()
{
p=p->next;
}
const Iter& operator--()
{
p=p->prev;
}
bool operator==(const Iter& other)
{
return p==other.p;
}
bool operator!=(const Iter& other)
{
return p!=other.p;
}
Iter begin()
{
return Iter(first);
}
Iter end()
{
return Iter(last);
}
Iter insert()
{
Node *t=new Node();
}
};*/
public:
int length;
void push_back(const T& Ilem);
void push_front(const T& Ilem);
void pop_back();
void pop_front();
void insert(int position, const T& Ilem);
void reverse(int begpos, int endpos);
int size();
T data(int begn);
Node *first;
Node *last;
Node *p;
List();
~List();
};
template <class T>
List<T>::List()
{
first = new Node(0, T(), 0);
last = first->next;
length=0;
}
template <class T>
void List<T>::push_back(const T& Ilem)
{
last=new Node(last, Ilem, 0);
last=last->next;
length++;
}
template <class T>
void List<T>::push_front(const T& Ilem)
{
first=new Node(0, Ilem, first);
first=first->prev;
length++;
}
template <class T>
void List<T>::pop_back()
{
if(length==0)
{
std::cout<<"List is empty! Unable to delete from back.";
}
else
{
Node *tmp = last;
last=last->prev;
delete tmp;
length--;
}
}
template <class T>
void List<T>::pop_front()
{
if(length==0)
{
std::cout<<"List is empty! Unable to delete from front.";
}
else
{
Node *tmp = first;
first=first->next;
delete tmp;
length--;
}
}
template <class T>
List<T>::~List()
{
Node *p = first;
while(p!=0)
{
Node *t=p;
p=p->next;
delete t;
}
}
template <class T>
int List<T>::size()
{
return (*this).length;
}
template <class T>
T List<T>::data(int begn)
{
if(begn==1)
{
return (*this).first->elem;
}
else
{
return (*this).last->elem;
}
}
#endif
|