鱼C论坛

 找回密码
 立即注册
查看: 1411|回复: 2

小白求助 关于链表逆序的问题

[复制链接]
发表于 2020-3-24 18:44:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我是照视频打的代码 前面的运行都成功 最后的逆序运行失败
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct Node
{
        int data;
        struct Node* next;
}SLIST;

//创建链表
SLIST* List_Create();
//遍历链表
int List_Print(SLIST* pHead);
//往x前面插入y
int List_Insert(SLIST* pHead, int x, int y);
//删除结点
int List_Delete(SLIST* pHead, int x);
//销毁链表
int List_Destory(SLIST* pHead);
//链表逆序
int List_Inverse(SLIST* pHead);

void main()
{
        SLIST* pHead = NULL;
        pHead = List_Create();

        List_Print(pHead);

        List_Insert(pHead, 4, 199);

        List_Print(pHead);

        List_Delete(pHead, 6);

        List_Print(pHead);

        List_Destory(pHead);

        List_Inverse(pHead);

        List_Print(pHead);

        system("pause");
}

SLIST* List_Create()
{
        SLIST* pHead = NULL;
        SLIST* pCur = NULL;

        int num = 0;
        while (num != -1)
        {
                printf("请输入num的值:");
                scanf("%d", &num);
                if (num == -1)
                {
                        break;
                }

                SLIST* pM = (SLIST*)malloc(sizeof(SLIST));
                if (pM == NULL)
                {
                        return NULL;
                }
                pM->data = num;
                pM->next = NULL;
                if (pHead == NULL)
                {
                        pHead = pCur = pM;
                }
                else
                {
                        pCur->next = pM;
                        pCur = pM;
                }
        }
        printf("num等于-1,存值结束\n");
        return pHead;
}

int List_Print(SLIST* pHead)
{
        SLIST* pCur = NULL;
        for (pCur = pHead; pCur; pCur = pCur->next)
        {
                printf("%d ", pCur->data);
        }
        printf("\n");
        return 0;
}

int List_Insert(SLIST* pHead, int x, int y)//链表是单向的,链表的位置保存在前驱结点中
{
        SLIST* pFre = pHead;
        SLIST* pCur = pHead->next;
        SLIST* pM = (SLIST*)malloc(sizeof(SLIST));
        if (pM == NULL)
        {
                return -1;
        }
        pM->data = y;
        pM->next = NULL;
        while (pCur)
        {
                if (pCur->data == x)
                {
                        break;
                }
                pFre = pCur;
                pCur = pCur->next;
        }
        pM->next = pFre->next;
        pFre->next = pM;
                
        return 0;
}

int List_Delete(SLIST* pHead, int x)
{
        SLIST* pCur = pHead->next;
        SLIST* pFre = pHead;
        while (pCur)
        {
                if (pCur->data == x)
                {
                        break;
                }
                pFre = pCur;
                pCur = pCur->next;
        }
        if (pCur != NULL)
        {
                pFre->next = pCur->next;
                free(pCur);
        }
        else
        {
                printf("不存在该元素\n");
                return -1;
        }
        return 0;
}

int List_Destory(SLIST* pHead)
{
        SLIST* tmp = pHead;
        while (tmp)
        {
                tmp = pHead->next;
                free(pHead);
                pHead = tmp;
        }
}

//逆序的时候 需要缓存下一个结点的位置  这里是逆序
int List_Inverse(SLIST* pHead)
{
        if (pHead == NULL || pHead->next == NULL ||pHead->next->next == NULL)
        {
                return -1;
        }
        SLIST* tmp = NULL;
        SLIST* pCur = pHead->next;
        SLIST* pFre = pHead;
        while (pCur)
        {
                tmp = pCur->next;
                pCur->next = pFre;
                pFre = pCur;
                pCur = tmp;
        }
        pHead->next->next = NULL;
        pHead->next = pFre;
        return 0;
}

逆序失败

逆序失败
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-25 08:58:33 | 显示全部楼层
 if (pHead == NULL || pHead->next == NULL ||pHead->next->next == NULL)
我觉得这句话有问题,这里应该是判断单链表为空链表或者单链表只有一个元素。但是pHead->next->next是有问题的。pHead->next指向的是下一个元素而不是下一个节点。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 15:35:27 | 显示全部楼层
哦哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-10 07:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表