Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct Double_node{
- int val;
- Double_node * next;
- Double_node * prev;
- };
- class Double_list{
- private:
- Double_node *tail;
- public:
- Double_list();
- Double_list(int);
- ~Double_list();
- bool empty();
- void push_front(int);
- void pop_front();
- void push_back(int);
- void pop_back();
- void clear();
- int& front();
- int& back();
- int size();
- void print();
- };
- Double_list::Double_list()
- {
- tail=0;
- }
- Double_list::Double_list(int a)
- {
- Double_node *p = new Double_node;
- p->val=0;
- p->prev=tail;
- p->next=tail;
- }
- bool Double_list::empty()
- {
- if(tail->next==tail->prev)
- return true;
- else return false;
- }
- void Double_list::push_front(int a)
- {
- Double_node *p= new Double_node;
- p->val = a;
- p->prev = tail;
- p->next = tail->next;
- }
- void Double_list::pop_front()
- {
- if(tail->next!=tail->prev)
- {
- Double_node *node = tail->next->next;
- delete tail->next;
- tail->next = node;
- }
- }
- void Double_list::clear()
- {
- while(tail->next!= tail->prev)
- {
- Double_node *node = tail->next->next;
- delete tail->next;
- tail->next = node;
- }
- }
- int& Double_list::front()
- {
- return tail->next->val;
- }
- int Double_list::size()
- {
- Double_node *p=tail->next;
- Double_node *t=tail;
- int k=0;
- while(p->next != t)
- {
- p=p->next;
- k++;
- }
- return k;
- }
- void Double_list::print()
- {
- Double_node *p=tail->next;
- while(p != tail)
- {
- cout<<p->val<<" ";
- Double_node *node = p->next;
- p = node;
- }
- cout<<"\n";
- }
- Double_list::~Double_list()
- {
- while(tail->prev != tail->next )
- {
- Double_node *node = tail->next->next;
- delete tail->next;
- tail->next = node;
- }
- }
- int main()
- {
- Double_list();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement