zoukankan      html  css  js  c++  java
  • 单链表逆转(递归指针实现)

    设有一个表头指针为h的单链表。试设计一个算法,通过遍历一趟链表,将链表中所有节点的链接方向逆转,如图2.27所示。要求逆转结果链表的表头指针h指向原链表的最后一个结点。

    #include <iostream>
    #include "SingleList.h"
    using namespace std;
    int main()
    {
        List<int> test;
        test.inputRear(0);
        test.output1();
        cout << "逆转链表后输出(已经将0赋值给原表头结点的值):" << endl;
        test.Reverse();
        test.output2();
        return 0;
    }
    
    //
    // Created by 莫道 on 2018/9/26.
    //
    
    #ifndef INC_2_17_SINGLELIST_H
    #define INC_2_17_SINGLELIST_H
    
    #include <iostream>
    using namespace std;
    template <class T>
    struct LinkNode
    {
        T data;
        LinkNode<T>* link;
        LinkNode(LinkNode<T>* ptr = NULL)
        {
            link = ptr;
        }
        LinkNode(const T& item, LinkNode<T>* ptr = NULL)
        {
            data = item;
            link = ptr;
        }
    };
    template <class T>
    class List
    {
    protected:
        LinkNode<T>* first;
    public:
        List()
        {
            first = new LinkNode<T>;
        }
        List(const T& x)
        {
            first = new LinkNode<T>(x);
        }
        List(List<T>& x)
        {
            first = new LinkNode<T>(x);
        }
        ~List()
        {
            makeEmpty();
        }
        void makeEmpty()
        {
            LinkNode<T>* q;
            while(first -> link != NULL)
            {
                q = first -> link;
                first -> link = q -> link;
                delete(q);
            }
        }
        void inputRear(T endTag)
        {
            LinkNode<T>* newNode, *last;
            T val;
            makeEmpty();
            cin >> val;
            last = first;
            while(val != endTag)
            {
                newNode = new LinkNode<T>(val);
                last -> link = newNode;
                last = newNode;
                cin >> val;
            }
        }
        void output1()
        {
            LinkNode<T> *current = first -> link;
            int count = 1;
            while (current != NULL)
            {
                cout << count++ << " : ";
                cout << current->data << endl;
                current = current->link;
            }
        }
        void output2()
        {
            LinkNode<T> *current = first;
            int count = 1;
            while (current != NULL)
            {
                cout << count++ << " : ";
                cout << current->data << endl;
                current = current->link;
            }
        }
        void Reverse()
        {
            LinkNode<T>* current = first;
            moveptr(current);
            current -> link = NULL;
            current -> data = 0;
        }
        void moveptr(LinkNode<T> *current)
        {
            if(current -> link -> link == NULL)
            {
                first = current -> link;
                current -> link -> link = current;
            }
            else
            {
                moveptr(current -> link);
                current -> link -> link = current;
            }
        }
    
        /*void Reverse()
        {
            LinkNode<T> *p, *q, *r;
            p = first;
            q = p->link;
            r = q->link;
            first->link = NULL;
            first->data = 0;
            while (r)
            {
                q->link = p;
                p = q;
                q = r;
                r = r->link;
            }
            q->link = p;
            first = q;
        }*/
    };
    #endif //INC_2_17_SINGLELIST_H
    
  • 相关阅读:
    无法识别的USB设备:跟这台计算机连接的一个USB设备运行不正常,WINDOWS无法识别
    优化大师修复IE右键
    毕业了,醉得一塌糊涂
    [转]关于CAD绘图过程中“旋转(Rotate)”命令的参照方式用法
    MDS 7.0 使用中的问题
    推荐一个3D台球游戏
    [推荐]零件公差、偏差查询软件
    铁路用热轧钢轨的截面尺寸
    删除windows隐藏的本地连接
    (转)网页加速的14条优化法则
  • 原文地址:https://www.cnblogs.com/chmod/p/15489943.html
Copyright © 2011-2022 走看看