• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++实现的简单的双向链表

OC/C/C++ 水墨上仙 1096次浏览

C++双向链表代码,使用了oop技术

//An example of a simple double linked list using OOP techniques
#include <iostream>
using namespace std;
 
struct Node
{
  double value;
  Node *N,*P;
  Node(double y)
  {
      value = y;
      N = P = NULL;
  }
};
 
class doubleLinkedList
{
  Node *front;
  Node *back;
  public:
  doubleLinkedList()
  {  front = NULL; back = NULL; }
  ~doubleLinkedList(){ destroyList();}
  void appendNodeFront(double x);
  void appendNodeBack(double x);
  void dispNodesForward();
  void dispNodesReverse();
  void destroyList();
};
 
void doubleLinkedList::appendNodeFront(double x)
  {
        Node *n = new Node(x);
        if( front == NULL)
        {
            front = n;
            back = n;
        }
        else
        {
            front->P = n;
            n->N = front;
            front = n;
        }
  }
  void doubleLinkedList::appendNodeBack(double x)
  {
        Node *n = new Node(x);
        if( back == NULL)
        {
            front = n;
            back = n;
        }
        else
        {
            back->N = n;
            n->P = back;
            back = n;
        }
 
  }
 
  void doubleLinkedList::dispNodesForward()
  {
      Node *temp = front;
      cout << "\n\nNodes in forward order:" << endl;
      while(temp != NULL)
      {
         cout << temp->value << "   " ;
         temp = temp->N;
      }
  }
  void doubleLinkedList::dispNodesReverse()
  {
      Node *temp = back;
      cout << "\n\nNodes in reverse order :" << endl;
      while(temp != NULL)
      {
         cout << temp->value << "   " ;
         temp = temp->P;
      }
  }
void doubleLinkedList::destroyList()
{
    Node *T = back;
    while(T != NULL)
    {
        Node *T2 = T;
        T = T->P;
        delete T2;
    }
    front = NULL;
    back = NULL;
}
int main()
{
    doubleLinkedList *list = new doubleLinkedList();
    //append nodes to front of the list
    for( int i = 1 ; i < 4 ; i++)
    list->appendNodeFront(i*1.1);
 
    list->dispNodesForward();
    list->dispNodesReverse();
 
    //append nodes to back of the list
    for( int i = 1 ; i < 4 ; i++)
    list->appendNodeBack(11.0 - (1.1 * i));
    cout << endl << endl;
    list->dispNodesForward();
    list->dispNodesReverse();
 
    cout << endl << endl;
    delete list;
    return 0;
}
 
/* Program's output
Nodes in forward order:
3.3   2.2   1.1
 
Nodes in reverse order :
1.1   2.2   3.3
 
 
 
Nodes in forward order:
3.3   2.2   1.1   9.9   8.8   7.7
 
Nodes in reverse order :
7.7   8.8   9.9   1.1   2.2   3.3
*/


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++实现的简单的双向链表
喜欢 (0)
加载中……