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的数就不执行了?想要得到下面的测试结果,怎么修改代码?