单链表的删除问题,为什么这个代码没有报错,但是main函数部分if下面的部分不执行?

main.cpp部分


#include<stdio.h>
#include "linklist.h"
int main()
{
    LinkNode *L;
    int n=10,i,e;

    ElemType a[]= {1,2,3,4,5,6,7,8,9,10};
    InitList(L);
    CreateListR(L,a,n);
    printf("L:");
    DispList(L);
    printf("请输入要删除的位置:\n");
    scanf("%d",&i);

    if(ListDelete(L,i,e)==1)
    {
         ListDelete(L,i,e);
        printf("删除完成后的链表为:\n");
        DispList(L);
        printf("删除的元素为:");
        printf("%d",e);
//

    }

    else
        printf("位置错误无法删除");


}

linklist.cpp部分

#include "linklist.h"                //声明单链表结点类型

void CreateListR(LinkNode *&L,ElemType a[],int n)
//尾插法建立单链表
{
    LinkNode *s,*r;
    L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
    L->next=NULL;
    r=L;                    //r始终指向终端结点,开始时指向头结点
    for (int i=0; i<n; i++)
    {
        s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点s
        s->data=a[i];
        r->next=s;            //将结点s插入结点r之后
        r=s;
    }
    r->next=NULL;            //终端结点next域置为NULL
}
void InitList(LinkNode *&L)
{
    L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
    L->next=NULL;
}
void DestroyList(LinkNode *&L)
{
    LinkNode *pre=L,*p=pre->next;
    while (p!=NULL)
    {
        free(pre);
        pre=p;
        p=pre->next;
    }
    free(pre);    //此时p为NULL,pre指向尾结点,释放它
}
bool ListEmpty(LinkNode *L)
{
    return(L->next==NULL);
}
int ListLength(LinkNode *L)
{
    LinkNode *p=L;
    int i=0;
    while (p->next!=NULL)
    {
        i++;
        p=p->next;
    }
    return(i);
}
void DispList(LinkNode *L)
{
    LinkNode *p=L->next;
    while (p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

bool ListDelete(LinkNode *&L,int i,ElemType &e)
{

    // 在带头结点的单链线性表L的第i个元素之前删除元素e
    /********** Begin **********/
    int j=0;
    LinkNode *p=L,*q;
    if(i<0)
        return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;

    }
    if (p=NULL)
        return false;
    else
    {
        q=p->next;
        if(q=NULL)
            return false;
        e=q->data;
        p->next=q->next;
        free(q);
        return true;

    }




    /********** End **********/







}

linklist.h部分


#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED

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

#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;        //指向后继结点
} LinkNode;
void InitList(LinkNode *&L);
void DispList(LinkNode *L);
bool ListInsert(LinkNode *&L,int i,ElemType e);
void CreateListR(LinkNode *&L,ElemType a[],int n);
bool ListDelete(LinkNode *&L,int i,ElemType &e);



#endif // LINKLIST_H_INCLUDED

为什么int main部分if部分输入大于0的数就不执行了?想要得到下面的测试结果,怎么修改代码?

img

改动处见注释,供参考:
main.cpp部分

#include<stdio.h>
#include "linklist.h"
int main()
{
    LinkNode *L;
    int n=10,i,e;

    ElemType a[]= {1,2,3,4,5,6,7,8,9,10};
    InitList(L);
    CreateListR(L,a,n);
    printf("L:");
    DispList(L);
    printf("请输入要删除的位置:\n");
    scanf("%d",&i);

    if(ListDelete(L,i,e)==1)
    {
        //ListDelete(L,i,e); //ListDelete(L,i,e);中文()修改
        printf("删除完成后的链表为:\n");
        DispList(L);
        printf("删除的元素为:");
        printf("%d\n",e);
    }
    else
        printf("位置错误无法删除");
    return 0;
}

linklist.cpp部分

#include "linklist.h"                //声明单链表结点类型

void CreateListR(LinkNode *&L,ElemType a[],int n)
//尾插法建立单链表
{
    LinkNode *s,*r;
    L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
    L->next=NULL;
    r=L;                    //r始终指向终端结点,开始时指向头结点
    for (int i=0; i<n; i++)
    {
        s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点s
        s->data=a[i];
        r->next=s;            //将结点s插入结点r之后
        r=s;
    }
    r->next=NULL;            //终端结点next域置为NULL
}
void InitList(LinkNode *&L)
{
    L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
    L->next=NULL;
}
void DestroyList(LinkNode *&L)
{
    LinkNode *pre=L,*p=pre->next;
    while (p!=NULL)
    {
        free(pre);
        pre=p;
        p=pre->next;
    }
    free(pre);    //此时p为NULL,pre指向尾结点,释放它
}
bool ListEmpty(LinkNode *L)
{
    return(L->next==NULL);
}
int ListLength(LinkNode *L)
{
    LinkNode *p=L;
    int i=0;
    while (p->next!=NULL)
    {
        i++;
        p=p->next;
    }
    return(i);
}
void DispList(LinkNode *L)
{
    LinkNode *p=L->next;
    while (p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

bool ListDelete(LinkNode *&L,int i,ElemType &e)
{

    // 在带头结点的单链线性表L的第i个元素之前删除元素e
    /********** Begin **********/
    int j=0;
    LinkNode *p=L,*q;
    if(i < 1)      //if(i<0) 修改
        return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
 
    }
    if (p==NULL)  //if (p=NULL)修改
        return false;
    else
    {
        q=p->next;
        if(q==NULL) //if(q=NULL)修改
            return false;
        e=q->data;
        p->next=q->next;
        free(q);
        return true;
    }
    /********** End **********/
}

linklist.h部分

#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED

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

#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;        //指向后继结点
} LinkNode;
void InitList(LinkNode *&L);
void DispList(LinkNode *L);
bool ListInsert(LinkNode *&L,int i,ElemType e);
void CreateListR(LinkNode *&L,ElemType a[],int n);
bool ListDelete(LinkNode *&L,int i,ElemType &e);

数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633