Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int value;
- Node * next;
- Node * prev;
- Node(): value(NULL), next(NULL), prev(NULL) {}
- Node(int v): value(v), next(NULL), prev(NULL) {}
- friend ostream & operator<<(ostream & os, const Node * n) {
- os<<n->value;
- return os;
- }
- friend istream & operator>>(istream & is, Node & n) {
- is>>n.value;
- return is;
- }
- };
- class List {
- Node * head;
- Node * tail;
- public:
- List(): head(NULL), tail(NULL) {}
- Node * begin()const {return head;}
- Node * end()const {return tail;}
- void pushfront(int value) {
- if (head==NULL) {head=tail=new Node(value);}
- else {
- head->prev=new Node(value);
- head->prev->next=head;
- head=head->prev;
- }
- }
- void pushback(int value) {
- if (tail==NULL) {head=tail=new Node(value);}
- else {
- tail->next=new Node(value);
- tail->next->prev=tail;
- tail=tail->next;
- }
- }
- void removefront() {
- if (head==NULL) return;
- Node * temp=head;
- head=head->next;
- head->prev=NULL;
- delete temp;
- }
- void removeback() {
- if (head==NULL) return;
- Node * temp=tail;
- tail=tail->prev;
- tail->next=NULL;
- delete temp;
- }
- /* void insert(int value, unsigned pos) {
- Node * temp=head;
- unsigned po=0;
- if (pos==0) {
- Node * p=new Node(value);
- head=p;
- head->next=temp;
- temp->prev=head;
- }
- else {
- while (po<pos) {
- temp=temp->next;
- po++;
- }
- Node * p=new Node(value);
- temp->prev->next=p;
- p->prev=temp->prev;
- p->next=temp;
- }
- }*/
- bool empty()const {return (head==NULL);}
- unsigned size() {
- unsigned size=1;
- Node * temp=head;
- while (temp->next!=NULL) {
- size++;
- temp=temp->next;
- }
- return size;
- }
- friend ostream & operator<<(ostream & os, const List & l) {
- if (l.head!=NULL) {
- Node * temp=l.head;
- cout<<temp;
- while (temp->next!=NULL) {
- cout<<temp->next;
- temp=temp->next;
- }
- }
- else cout<<"NULL";
- return os;
- }
- class Iter {
- Node * curr;
- friend List;
- public:
- Iter(): curr(NULL) {}
- Iter(const List & l): curr(l.begin()) {}
- Iter(const Iter & it): curr(it.curr) {}
- Iter & operator=(const Iter & it) {
- if(this!=&it) curr=it.curr;
- return *this;
- }
- Iter & operator++() {curr=curr->next;}
- Iter & operator--() {curr=curr->prev;}
- int & operator*()const {return ((Node*)curr)->value;}
- void operator*(int v) {((Node*)curr)->value=v;}
- Node * current()const {return curr;}
- };
- void insert(int value, Iter it) {
- if (head==NULL) head=new Node(value);
- else {}
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement