美文网首页
C++实现的简单的双向链表的代码

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

作者: 程序猿族 | 来源:发表于2019-01-25 09:07 被阅读0次

    下面的资料是关于C++实现的简单的双向链表的内容。

    #include <iostream>

    using namespace std;

    struct Node

    {

      double value;

      Node(double y)

      {

          value = y;

          N = P = NULL;

      }

    };

    class doubleLinkedList

    {

      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)

      {

            if( front == NULL)

            {

                front = n;

                back = n;

            }

            else

            {

                front->P = n;

                n->N = front;

                front = n;

            }

      }

      void doubleLinkedList::appendNodeBack(double x)

      {

            if( back == NULL)

            {

                front = n;

                back = n;

            }

            else

            {

                back->N = n;

                n->P = back;

                back = n;

            }

      }

      void doubleLinkedList::dispNodesForward()

      {

          cout << "nnNodes in forward order:" << endl;

          while(temp != NULL)

          {

            cout << temp->value << "  " ;

            temp = temp->N;

          }

      }

      void doubleLinkedList::dispNodesReverse()

      {

          cout << "nnNodes in reverse order :" << endl;

          while(temp != NULL)

          {

            cout << temp->value << "  " ;

            temp = temp->P;

          }

      }

    void doubleLinkedList::destroyList()

    {

        while(T != NULL)

        {

            T = T->P;

            delete T2;

        }

        front = NULL;

        back = NULL;

    }

    int main()

    {

        for( int i = 1 ; i < 4 ; i++)

        list->dispNodesForward();

        list->dispNodesReverse();

        for( int i = 1 ; i < 4 ; i++)

        cout << endl << endl;

        list->dispNodesForward();

        list->dispNodesReverse();

        cout << endl << endl;

        delete list;

        return 0;

    }

    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++实现的简单的双向链表的代码

          本文链接:https://www.haomeiwen.com/subject/ceapjqtx.html