C语言单链表的逆序,出现了错误,
//创建链表和遍历链表都没有问题,当程序运行到单链表逆序也就是OPP函数时崩溃,求告诉原因,不胜感激#include<stdio.h>#include<stdlib.h>...
//创建链表和遍历链表都没有问题,当程序运行到单链表逆序也就是OPP函数时崩溃,求告诉原因,不胜感激
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef struct student{
char name[MAX];
int numb;
struct student *pNext;
}NODE,*PNODE;
PNODE crateList();
PNODE opp(PNODE pList);
void traverseList(PNODE pList);
int main(void){
PNODE pList;
pList=crateList();
traverseList(pList);
pList=opp(pList);
traverseList(pList);
return(0);
}
PNODE crateList(){
PNODE pHead=NULL,pCurrent,pPre;
char input[MAX];
puts("enter the name");
while(gets(input)!=NULL&&input[0]!='\0'){
pCurrent=(PNODE)malloc(sizeof(NODE));
if(pCurrent==NULL)
return(0);
if(pHead==NULL)
pHead=pCurrent;
else
pPre->pNext=pCurrent;
pCurrent->pNext=NULL;
strcpy(pCurrent->name,input);
puts("enter the number:");
scanf("%d",&pCurrent->numb);
pPre=pCurrent;
puts("enter the next one or enter to leave");
while(getchar()!='\n')
continue;
}
return(pHead);
}
void traverseList(PNODE pList){
PNODE pCurrent;
pCurrent=pList;
if(pCurrent==NULL)
puts("no data enter!");
else
while(pCurrent!=NULL){
printf("the name is %s,and number is %d\n",pCurrent->name,pCurrent->numb);
pCurrent=pCurrent->pNext;
}
}
PNODE opp(PNODE pList){
PNODE pFirst,pMid,pLast,pCurrent;
if(!pList&&pList->pNext==NULL){
puts("no data to convert!");
return(0);
}
pFirst=pList;
pMid=pFirst->pNext;
pLast=pMid;
while(pMid){
pFirst->pNext=NULL;
pMid->pNext=pFirst;
pFirst=pMid;
pMid=pLast;
pLast=pMid->pNext;
}
} 展开
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef struct student{
char name[MAX];
int numb;
struct student *pNext;
}NODE,*PNODE;
PNODE crateList();
PNODE opp(PNODE pList);
void traverseList(PNODE pList);
int main(void){
PNODE pList;
pList=crateList();
traverseList(pList);
pList=opp(pList);
traverseList(pList);
return(0);
}
PNODE crateList(){
PNODE pHead=NULL,pCurrent,pPre;
char input[MAX];
puts("enter the name");
while(gets(input)!=NULL&&input[0]!='\0'){
pCurrent=(PNODE)malloc(sizeof(NODE));
if(pCurrent==NULL)
return(0);
if(pHead==NULL)
pHead=pCurrent;
else
pPre->pNext=pCurrent;
pCurrent->pNext=NULL;
strcpy(pCurrent->name,input);
puts("enter the number:");
scanf("%d",&pCurrent->numb);
pPre=pCurrent;
puts("enter the next one or enter to leave");
while(getchar()!='\n')
continue;
}
return(pHead);
}
void traverseList(PNODE pList){
PNODE pCurrent;
pCurrent=pList;
if(pCurrent==NULL)
puts("no data enter!");
else
while(pCurrent!=NULL){
printf("the name is %s,and number is %d\n",pCurrent->name,pCurrent->numb);
pCurrent=pCurrent->pNext;
}
}
PNODE opp(PNODE pList){
PNODE pFirst,pMid,pLast,pCurrent;
if(!pList&&pList->pNext==NULL){
puts("no data to convert!");
return(0);
}
pFirst=pList;
pMid=pFirst->pNext;
pLast=pMid;
while(pMid){
pFirst->pNext=NULL;
pMid->pNext=pFirst;
pFirst=pMid;
pMid=pLast;
pLast=pMid->pNext;
}
} 展开
展开全部
//分析opp函数
PNODE opp(PNODE pList){
PNODE pFirst,pMid,pLast,pCurrent;
if(!pList&&pList->pNext==NULL){//应改为if(!pList||pList->pNext==NULL)
//&&表示两个条件都要同时满足,||表示只
//满足一个,关键是使用&&,当pList为NULL
//时,仍然会判断pList->pNext这会导致
//访问空指针。
puts("no data to convert!");
return(0);
}
pFirst=pList;
pMid=pFirst->pNext;
pLast=pMid; //应该是pLast=pMid->pNext;这行可以放入循环开头
while(pMid){
pFirst->pNext=NULL;//每次循环都会导致pFirst->pNext置为空指针
//这应该不是期望的操作。因pFirst指针
//会随循环而向下跳转,这是链首节点
//的处理,应该放到循环外
pMid->pNext=pFirst;
pFirst=pMid;
pMid=pLast;
pLast=pMid->pNext;//在无法确认pMid是否为NULL时,直接使用
//pMid->pNext是不安全的。可以放到循环
//开头
}
}
思路没问题,但存在代码输入错误,赋值的时机错误,调整后为:
PNODE opp(PNODE pList){
PNODE pFirst,pMid,pLast,pCurrent;
if(!pList||pList->pNext==NULL){
puts("no data to convert!");
return(0);
}
pFirst=pList;
pMid=pFirst->pNext;
pFirst->pNext=NULL;
while(pMid){
pLast=pMid->pNext;
pMid->pNext=pFirst;
pFirst=pMid;
pCurrent=pMid;
pMid=pLast;
}
return pCurrent;
}
2016-09-08
展开全部
PNODE opp(PNODE pList){
PNODE pFirst,pMid,pLast,pCurrent;
if(!pList&&pList->pNext==NULL){
// puts("no data to convert!"); 个人认为没必要输出这种信息
return(pList);
}
pFirst=pList;
pMid=pFirst->pNext;
pFirst->pNext=NULL; // 不能每次都赋值成空,只有原来第一个要这样
pLast=pMid->pNext;
pMid->pNext=pFirst; // 要把第一个节点连接上
while(pLast){ // 后面就顺次向后移动指针
pFirst=pMid;
pMid=pLast;
pLast=pMid->pNext;
pMid->pNext=pFirst; // 并把以前的链接在这后面
}
return pMid;
}
这样就没问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询