C++は双方向チェーンの実現コードを構成します。


双方向チェーンを構築して、足りないところを指摘してください。 

// DoubleLinkedList.cpp : 。
// , , , , size
#include "stdafx.h"
#include <iostream>
using namespace std;
//
template<class T>
struct NODE
{
 NODE<T>* pre;
 T data;
 NODE<T>* next;
};
//
template<class T>
class DoubleLinkedList
{
public:
 DoubleLinkedList()
 {
  NODE<T>* q = new NODE<T>;
  if (q == NULL)
  {
   cout << "Fail to malloc the head node." << endl;
   return;
  }
  phead = q;
  phead->pre = NULL;
  phead->data = NULL;
  phead->next = NULL;

  T i;
  cout << "Please input several integer number, input ctrl+z to the end: " << endl;
  while (cin >> i)
  {
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Fail to malloc a new node." << endl;
    return;
   }
   p->data = i;
   q->next = p;
   p->pre = q;
   p->next = NULL;
   q = q->next;
  }
 }

 //
 int size()
 {
  NODE<T>* p = phead->next;
  int count(0);
  while (p != NULL)
  {
   count++;
   p = p->next;
  }
  return count;
 }

 // DoubleLinkedLIst
 void print_elements()
 {
  NODE<T>* p = phead->next;
  while (p != NULL)
  {
   cout << p->data << " ";
   p = p->next;
  }
  cout << endl;
 }
 // DoubleLinkedList ,
 void insert_element(int i, T e)
 {
   if (i <= this->size())
  {
   NODE<T>* m = phead;
   for (int j = 1; j < i; j ++)
   {
    m = m->next;
   }
   NODE<T>* n = m->next;
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Failed to malloc the node." << endl;
   }
   m->next = p;
   p->pre = m;
   p->data = e;
   p->next = n;
   n->pre = p;
  }
  else if (i == (this->size()+1))
  {
   NODE<T>* m = phead;
   for (int j = 1; j < i; j++)
   {
    m = m->next;
   }
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Failed to malloc the node." << endl;
   }
   m->next = p;
   p->pre = m;
   p->data = e;
   p->next = NULL;
  }
  else
  {
   cout << "Please input the position number equals or smaller than " << size()+1 << endl;
  }
 }

 // DoubleLinkedList ,
 void insert_element(T e)
 {
  NODE<T>* m = phead;
  for (int j = 1; j <= size(); j++)
  {
   m = m->next;
  }
  NODE<T>* p = new NODE<T>;
  if (p == NULL)
  {
   cout << "Failed to malloc the node." << endl;
  }
  m->next = p;
  p->pre = m;
  p->data = e;
  p->next = NULL;
 }

 // DoubleLinkedList
 void delete_element(int i)
 {
  NODE<T>* p = phead;
  for (int j = 0; j < i; j ++)
  {
   p = p->next;
   if (p == NULL)
   {// list
    cout << "The size of the list is " << size() << " ,Please input the right number." << endl;
    return;
   }
  }
  if(p->next != NULL)
  {//
   NODE<T>* m = p->pre;
   NODE<T>* n = p->next;
   m->next = n;
   n->pre = m;
   delete p;
  }
  else
  {//
   NODE<T>* m = p->pre;
   m->next = NULL;
   delete p;
  }
 }
private:
 NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
 //
  DoubleLinkedList<int> mylist;
 mylist.print_elements();
 cout << "The size of the double linked list is : " << mylist.size() << endl;
 mylist.insert_element(1, 50);
 mylist.print_elements();
 mylist.insert_element(6, 80);
 mylist.print_elements();
 mylist.insert_element(250);
 mylist.print_elements();
 mylist.delete_element(7);
 mylist.print_elements();
 return 0;
}&nbsp;