C++实现单链表的反序

引言

将一个没有空结点作为头的单链表实现反转。其实质就是将结点的指针域指向反转。定义指向当前结点的指针,指向前一个结点的指针,指向当前结点的后一个结点的指针,这个过程中包含只有一个结点的单链表,那么反转之后还是它本身,只有两个结点的单链表,反转之后由第一个结点的指针指向第二个结点,第二个结点的指针域指向为空,变为第二个结点的指针指向第一个结点,第一个结点的指针域为空。当结点数大于二,便就是一系列结点指针指向的反转,最后记得将反转后的单链表的尾结点的指针指向空。

示例

下面是在vs2010上实现的单链表反序。

// reverseList.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

/***********************
功能:实现无头单链表的反序
包含的功能函数:
struct Node* createList(int n,int data)  创建n的结点的单链表,链表采用data++的形式增减每一个结点的值
struct Node* reverseList(struct Node **pList) 将但联编反序,大致的形式类似:1-》2-》3 反序后3-》2-》1
void printList(struct Node *pList)  输出;链表pList的结点元素
**********************/

struct Node
{
    int num;
    struct Node *next;
};

struct Node* createList(int n,int data)
{
    struct Node *ph = NULL;
    struct Node *pnow = NULL;
    struct Node *pLast = NULL;

    while (n >= 0)
    {
        struct Node *pNode = new struct Node;
        pNode->next = NULL;
        pNode->num = data++;
        if(ph == NULL)
        {
            ph = pNode;
        }
        pnow = pNode;
        if(pLast != NULL)
        {
            pLast->next = pnow;
        }
        
        pLast = pnow;

        --n;
    }
    return ph;

}

struct Node* reverseList(struct Node **pList)
{
    struct Node *pre = *pList;//指向前一个结点
    struct Node *pCur = NULL;//指向当前结点
    struct Node *pNext = NULL;//指向当前结点的下一个结点
    if(pre->next != NULL)
    {
        pCur = pre->next;
        if(pCur->next != NULL)//结点数最少为3个
        {
            pNext = pCur->next;
            pre->next = NULL;
            while(pCur)
            {
                pCur->next = pre;
                if(pNext)
                {
                    pre = pCur;
                    pCur = pNext;
                    pNext = pNext->next;
                }
                else
                {
                    break;
                }
            }
            return pCur;
        }
        else//只有两个结点
        {
            pCur->next = pre;
            pre->next = NULL;
            return pCur;
        }
    }
    else//只有一个结点
    {
        return pre;
    }
}

void printList(struct Node *pList)
{
    struct Node *ph = pList;
    while(ph != NULL)//注意这里是结点不为空ph != NULL,不是结点的指针指向不为空ph->next != NULL
    {
        cout<<ph->num<<"\t";
        ph = ph->next;
    }
    cout<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	
    struct Node *pList = createList(4,1);
    printList(pList);
    cout<<"================="<<endl;
    struct Node * ph = reverseList(&pList);
    printList(ph);

    system("pause");
    return 0;
}

程序的运行效果如下:
在这里插入图片描述
仅以记录。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肩上风骋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值