- Code DLL.
Source Files:
dll.h file.
#ifndef __DLL_H__ #define __DLL_H__ class DLL; class Node { int _data; Node* _prev; Node* _next; Node(int data, Node* prev = (Node*)0, Node* next = (Node*)0); friend class DLL; }; class DLL { Node* _head; Node* _curr; Node* _tail; DLL(DLL& D); // prevent copying DLL& operator=(DLL& D); public: DLL(); virtual ~DLL(); void append(int data); // add after tail int remove(); // remove the current goes next if possible void insert(int data); // insert data before current int visit(); // returns data of current bool goHead(); bool goTail(); bool goNext(); bool goPrev(); bool isEmpty(); }; #endif
dll.cpp file.
#include "dll.h" Node::Node(int data, Node* prev, Node* next) { _data = data; _prev = prev; _next = next; } DLL::DLL() { _head = (Node*)0; _curr = (Node*)0; _tail = (Node*)0; } DLL::~DLL() { while(!isEmpty()) { remove(); } } void DLL::append(int data) { if(isEmpty()) { Node* newNode = new Node(data); _head = newNode; _curr = newNode; _tail = newNode; } else { Node* newNode = new Node(data); newNode->_prev = _curr; _curr->_next = newNode; _tail = newNode; _curr = newNode; } } int DLL::remove() { int data = 0; if(!isEmpty()) { Node* ToDel = _curr; data = _curr->_data; if(_curr == _head && _curr == _tail) { _head = (Node*)0; _curr = (Node*)0; _tail = (Node*)0; delete ToDel; } else if(_curr == _head) { _curr->_next->_prev = _curr->_prev; _head = _curr->_next; _curr = _head; delete ToDel; } else if(_curr == _tail) { _curr->_prev->_next = _curr->_next; _tail = _curr->_prev; _curr = _tail; delete ToDel; } else { _curr->_next->_prev = _curr->_prev; _curr->_prev->_next = _curr->_next; _curr = _curr->_next; delete ToDel; } } return data; } void DLL::insert(int data) { Node* newNode = new Node(data); if(isEmpty()) { _head = newNode; _curr = newNode; _tail = newNode; } else if(_curr == _head) { newNode->_next = _head; _head->_prev = newNode; _head = newNode; _curr = newNode; } else { newNode->_prev = _curr->_prev; newNode->_next = _curr; _curr->_prev->_next = newNode; _curr->_prev = newNode; _curr = newNode; } } int DLL::visit() { return _curr->_data; } bool DLL::goHead() { _curr = _head; return true; } bool DLL::goTail() { _curr = _tail; return true; } bool DLL::goNext() { if(_curr != _tail) { _curr = _curr->_next; return true; } else { return false; } } bool DLL::goPrev() { if(_curr != _head) { _curr = _curr->_prev; return true; } else { return false; } } bool DLL::isEmpty() { return !_head && !_curr && !_tail; }
dlltester.cpp file.
// provided by professor. #include "dll.h" #include <iostream> using namespace std; void printList(DLL& d){ d.goHead(); } ostream& operator<<(ostream& os, DLL& d){ int cur; bool done = false; for(cur=0;d.goPrev();cur++); // findout where is current do{ os<<d.visit(); done = !d.goNext(); if(!done){ os<<", "; } }while(!done); for(d.goHead();cur;d.goNext(), cur--); // set current to what it was before return os; } ostream& operator>>(ostream& os, DLL& d){ // printing in reverse!!!!! int cur; bool done = false; for(cur=0;d.goNext();cur++); // findout where is current do{ os<<d.visit(); done = !d.goPrev(); if(!done){ os<<", "; } }while(!done); for(d.goTail();cur;d.goPrev(), cur--); // set current to what it was before return os; } int main(){ DLL d; for(int i=0;i<10;i++){ d.append(i+1); } cout<<d<<endl; d.goHead(); d.insert(1000); d.goNext(); d.insert(2000); cout<<d<<endl; cout>>d<<endl; cout<<d.remove()<<endl; cout<<d<<endl; return 0; }
No comments:
Post a Comment