单链表逆转(递归指针实现)

设有一个表头指针为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

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

modao233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值