有关无终止输出的原因
下面是我写的一个程序,但是输出结果却是无终止输出(答案正确)
想请教大侠们,为啥会出现这样的情况,又该怎样避免。并告诉我我的程序该如何该才能正常输出结果
/* Note:Your choice is C IDE */
#include <stdio.h>
#include <malloc.h>
#define NULL 0
typedef struct Listnode
{
int data;
struct Listnode *next;
}List;
/*建立一个单链表,表中元素值递增有序排列*/
struct Listnode *creat(void){/*创建一个单链表*/
struct Listnode *pCurson,*postion,*head;/*定义head为头指针,pCurson为活动指针,positon为定位指针*/
pCurson=postion=head=(struct Listnode*)malloc(sizeof(struct Listnode));/*建立一个节点*/
printf( "please put in data: ");
scanf( "%d ",&pCurson-> data);
while(pCurson-> data!=NULL)/*如果输入的值非空,则继续输入,直到输入值为空*/
{
pCurson=(struct Listnode*)malloc(sizeof(struct Listnode));
printf( "please put in data: ");
scanf( "%d ",&pCurson-> data);
postion-> next=pCurson;/*将pCurson向下移动一位*/
postion=pCurson;/*将postion重新定位到pCurson所指向的地址*/
}
postion-> next=NULL;/*如果输入值为空,则postion所指向的下一个地址为空*/
return(head);/*结束返回头结点*/
}
/*将单链表中大于mink,小于maxk的值删除*/
void dele(List *head,int mink,int maxk){
List *pCurson,*postion;
pCurson=postion=head;/*将pCurson和postion指向头指针*/
while(pCurson-> data> =maxk){
printf( "not any data will be deleted!\n ");/*如果单链表中值大于等于maxk,则不删除任何数据*/
}
while(pCurson-> data <=mink){/*如果单链表中最小值小于等于MINK,则依次向下移动,直到遇到大于mink的值为止*/
postion=pCurson;/*将postion重定位到pCurson所指向的地址*/
pCurson=pCurson-> next;/*将pCurson向下移动一位地址*/
}
if(pCurson-> data> mink){
while(pCurson-> data <maxk){/*如果存在值大于mink,小于maxk,则删除该值,并释放结点*/
postion-> next=pCurson-> next;
printf( "delete:%d\n ",pCurson-> data);
free(pCurson);
pCurson=postion;
pCurson=pCurson-> next;
}
}
}
/*输出单链表数据*/
void show(struct Listnode *head){
List *pCurson;
pCurson=head;
do
{
printf( "data=%5d\n ",pCurson-> data);
pCurson=pCurson-> next;
}
while(pCurson-> next!=NULL);
}
void main(){
List *pCurson;
int mink,maxk;
pCurson=creat();
show(pCurson);/*显示单链表*/
printf( "maxk is bigger than mink\n ");/*开始输出mink,maxk值*/
printf( "please input mink!\n ");
printf( "mink= ");
scanf( "%d ",&mink);
printf( "please input maxk!\n ");
printf( "maxk= ");
scanf( "%d ",&maxk);
while(mink==maxk){/*如果mink等于maxk的话,报错并重新输入*/
printf( "ERROR! ");
printf( "maxk is bigger than mink\n ");
printf( "please input mink!\n ");
printf( "mink= ");
scanf( "%d ",&mink);
printf( "please input maxk!\n ");
printf( "maxk= ");
scanf( "%d ",&maxk);
}
while(mink!=0&&maxk!=0){
dele(pCurson,mink,maxk);
show(pCurson);
}
}
[解决办法]
既然dele函数就是遍历整个链表删除不符合要求的节点的,为什么还要
while(mink!=0&&maxk!=0){
dele(pCurson,mink,maxk);
show(pCurson);
}
????
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
typedef struct Listnode
{
int data;
struct Listnode *next;
}List;
/*建立一个单链表,表中元素值递增有序排列*/
struct Listnode *creat(void){/*创建一个单链表*/
struct Listnode *pCurson,*postion,*head; /*定义head为头指针,pCurson为活动指针,positon为定位指针*/
pCurson=postion=head=(struct Listnode*)malloc(sizeof(struct Listnode));/*建立一个节点*/
printf( "please put in data: ");
scanf( "%d ",&pCurson-> data);
while(pCurson-> data!=0)/*如果输入的值非空,则继续输入,直到输入值为空*/
{
pCurson=(struct Listnode*)malloc(sizeof(struct Listnode));
printf( "please put in data: ");
scanf( "%d ",&pCurson-> data);
postion-> next=pCurson;/*将pCurson向下移动一位*/
postion=pCurson;/*将postion重新定位到pCurson所指向的地址*/
}
postion-> next=NULL;/*如果输入值为空,则postion所指向的下一个地址为空*/
return(head);/*结束返回头结点*/
}
/*将单链表中大于mink,小于maxk的值删除*/
void dele(List **head,int mink,int maxk){
List *pCurson,*postion;
pCurson=postion=*head;/*将pCurson和postion指向头指针*/
while(pCurson){/*如果存在值大于mink,小于maxk,则删除该值,并释放结点*/
if(pCurson-> data> mink&&pCurson-> data <maxk)
{
if(*head==pCurson) //头节点不符合要求
{
*head=pCurson-> next; //头节点下移
postion=pCurson;
}
else
postion-> next=pCurson-> next;
printf( "delete:%d\n ",pCurson-> data);
free(pCurson);
pCurson=postion;
}else
{
postion=pCurson;
pCurson=pCurson-> next;
}
}
}
/*输出单链表数据*/
void show(struct Listnode *head){
List *pCurson;
pCurson=head;
while(pCurson!=NULL)
{
printf( "data=%5d\n ",pCurson-> data);
pCurson=pCurson-> next;
};
}
int main(){
List *pCurson;
int mink,maxk;
pCurson=creat();
show(pCurson); /*显示单链表*/
printf( "maxk is bigger than mink\n ");/*开始输出mink,maxk值*/
printf( "please input mink!\n ");
printf( "mink= ");
scanf( "%d ",&mink);
printf( "please input maxk!\n ");
printf( "maxk= ");
scanf( "%d ",&maxk);
while(mink==maxk){/*如果mink等于maxk的话,报错并重新输入*/
printf( "ERROR! ");
printf( "maxk is bigger than mink\n ");
printf( "please input mink!\n ");
printf( "mink= ");
scanf( "%d ",&mink);
printf( "please input maxk!\n ");
printf( "maxk= ");
scanf( "%d ",&maxk);
}
dele(&pCurson,mink,maxk);
show(pCurson);
}