反转单向链表,C语言,运行出错

#include
#include

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

typedef struct LNode{
int node;
struct LNode *next;
} LNode,*LinkList;

LinkList Head_Node()
{
LinkList head;
head=(LinkList)malloc(sizeof(LNode));
if(head==NULL)
{
printf("空间分配失败\n");
return head;
}
head->next=NULL;
return head;
}

int CreateList(LinkList head)
{
int data;
char c;
LinkList p,q;
q=head;
printf("请输入数据:");
do
{
scanf("%d",&data);
c=getchar();
p=(LinkList)malloc(sizeof(LNode));
if(p==NULL)
{
printf("空间分配失败\n");
return -1;
}
p->node=data;
p->next=q->next;
q->next=p;
q=p;
}
while(c!='\n');
return 0;
}

LinkList Reverse(LinkList head)
{
LinkList p,q,r;
p=head;
q=head->next;
head->next=NULL;
if(q->next!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
q->next=p;
head->next=q;
return head;
}

void Output(LinkList head)
{
LinkList p;
p=head->next;
while(p)
{
printf("%d ",p->node);
p=p->next;
}
}

int main()
{
LinkList head;
head=Head_Node();
CreateList(head);
Reverse(head);
Output(head);
system("pause");
return 0;
}

 #include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

typedef struct LNode{
    int node;
    struct LNode *next;
} LNode,*LinkList;

LinkList Head_Node()
{
    LinkList head;
    head=(LinkList)malloc(sizeof(LNode));
    if(head==NULL)
    {
        printf("空间分配失败\n");
        return head;
    }
    head->node = -1;
    head->next=NULL;
    return head;
}

int CreateList(LinkList head)
{
    int data;
    char c;
    LinkList p,q;
    q=head;
    printf("请输入数据:");
    do
    {
        scanf("%d",&data);
        c=getchar();
        if(-1 == data)
            break;

        p=(LinkList)malloc(sizeof(LNode));
        if(p==NULL)
        {
            printf("空间分配失败\n");
            return -1;
        }
        p->node=data;
        p->next=q->next;
        q->next=p;
        q=p;
    }
    //while(c == 10);//如果是这样你希望以什么方式退出(这里的10代表'\n')?
    while(1);
    return 0;
}

//你的翻转函数有逻辑问题
LinkList Reverse(LinkList head)
{
    LinkList p,q,r;

    p = head->next;
    q = p->next;
    p->next = NULL;

    while(q->next!=NULL)
    {
        r=q->next;
        q->next=p;
        p=q;
        q=r;
    }
    q->next=p;
    head->next = q;

    return head;
}

void Output(LinkList head)
{
    LinkList p;
    p=head->next;
    while(p != NULL)
    {
        printf("%d ",p->node);
        p=p->next;
    }
    putchar(10);
}

int main()
{
    LinkList head;
    head=Head_Node();
    CreateList(head);
    Output(head);
    Reverse(head);
    printf("*********************************************************\n");
    Output(head);
    //system("pause");
    return 0;
}

Debug模式运行,然后在出错的地方加断点,看下内存,如果程序蹦了,看下堆栈调用

Reverse函数,反转应该用的是while循环,但你用的是if
改成while,但仍需注意头节点和尾节点的处理,没有处理到位,肯定会出错,建议在纸上画一下流程

代码太长,晕了,大致是哪个部分出问题了,请说明一下。

主要是你反转的时候应该注意一下内存的问题

给你写了个,你试试

    ListNode* reverseList(ListNode* head) {
         if(head==NULL || head->next==NULL)
            return head;
        ListNode* pre = NULL;
        ListNode* current = head;
        ListNode* post;

        ListNode* newhead;
        while(current!=NULL)
        {
            post = current->next;
            if(post==NULL)
            {
                newhead = current;
            }
            current->next = pre;
            pre = current;

            current = post;
        }

        return newhead;
    }