C+双链表

#include "iostream"
using namespace std;
template <class Elem> class Link {
private:
  static Link<Elem>* freelist; // Head
public:
  Elem element;     // Value for this node
  Link *next;    // Pointer to next node 
  Link *prev;    // Pointer to previous 
  Link(const Elem& e, Link* prevp =NULL, Link* nextp =NULL)
   { 
 element=e;  prev=prevp;  next=nextp; 
  }
  Link(Link* prevp =NULL, Link* nextp =NULL)
  { 
 prev = prevp;  next = nextp; 
  }
  void* operator new(size_t);  // Overload
  void operator delete(void*); // Overload
};
template <class Elem>
Link<Elem>* Link<Elem>::freelist = NULL;


template <class Elem>   // Overload for new
void* Link<Elem>::operator new(size_t) {
  if (freelist == NULL) return ::new Link;
  Link<Elem>* temp = freelist; // Reuse
  freelist = freelist->next;
  return temp;         // Return the link
}


template <class Elem>   // Overload delete
void Link<Elem>::operator delete(void* ptr){
  ((Link<Elem>*)ptr)->next = freelist;
  freelist = (Link<Elem>*)ptr;
}


template<class Elem> class LList : public Link<Elem> { // Linked list class
private:
Link<Elem>* head;                 // Pointer to list header
Link<Elem>* tail;                 // Pointer to last Elem in list 
Link<Elem>* fence;                 // last elements on left side
int leftcnt; //size of left partition
int rightcnt; //size of right partition


private:
void init(){ //initialization routine
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}


void removeall(){//return link nodes to free store
while(head != NULL){
fence = head;
head = head->next;
delete fence;
}
}public:
// LList()       // Constructor
// { init(); }                         // Initialize
// ~LList(){ removeall();}
// void clear() { removeall(); init();}
bool insert(const Elem& item) {
fence->next = new Link<Elem>(item, fence, fence->next);  
        if (fence->next->next != NULL)
        fence->next->next->prev = fence->next;
        if (tail == fence)   // Appending new Elem
        tail = fence->next; //   so set tail
        rightcnt++;           // Added to right
        return true;
}


bool append(const Elem& item) // Insert Elem at tail of list
{
tail=tail->next=new Link<Elem>(item,tail,NULL);
rightcnt++;
return true;
}
bool remove(Elem& it) {              // Remove and return current Elem
if (fence->next == NULL) return false;
        it = fence->next->element;
        Link<Elem>* ltemp = fence->next;
        if (ltemp->next != NULL)
ltemp->next->prev = fence;
else tail = fence;         // Reset tail
fence->next = ltemp->next; // Remove  delete ltemp;    // Reclaim space
rightcnt--;      // Removed from right
return true;
}




void setStart()          // Set fence to first position
{ fence = head; rightcnt += leftcnt; leftcnt = 0; }


void setEnd()          // Set fence to end position
{ fence = tail; leftcnt += rightcnt; rightcnt = 0; }


void prev() { 
Link<Elem>* temp = head;
if(fence == head) return;     //no previous elem
while(temp->next != fence) temp = temp->next;
fence = temp;
leftcnt--;
rightcnt++;
}


void next()              // Move curr to next position
{ 
if(fence != tail)//don't move fence if right empty
{fence = fence->next; rightcnt--; leftcnt++;}
}


  // Return length of left and right partition, respectively
int  leftLength() const {         // Return current length of list
return leftcnt;
}
int  rightLength() const {         // Return current length of list
return rightcnt;
}
bool setPos(int pos) {       // Set curr to specified position
if((pos < 0) || (pos > rightcnt + leftcnt)) return false;
fence = head;
for(int i = 0; i < pos; i++) fence = fence->next;
return true;
}


bool getValue(Elem& it) const   // Return value of current Elem
{ 
if(rightLength() == 0) return false;
it = fence->next->element;
return true;
}
void print() const
{
Link<Elem>* temp = head;
cout << "<";
while(temp != fence){
cout << temp->next->element<<" ";
temp = temp->next;
}
cout << "|";
while(temp->next != NULL){
cout << temp->next->element << " ";
temp = temp->next;
}
cout << ">\n";
}
};




int main(int argc, char* argv[])
{
LList<int> lList;
lList.insert(8);
lList.insert(9);
lList.insert(3);
lList.insert(4);
lList.append(1);
lList.print();
return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值