C++ 丢失指针
当我使用“1”命令插入一个新的结构a,这样我已经有一个或多个结构a链接到一个或多个结构时,我就失去了以前的结构a到它们的结构的链接。 例如: 命令1和命令3:2014和命令4 输出: 年份:2014年 matricola:1 现在命令1,然后命令3:2015和命令4 输出: 年份:2015年 matricola:2 年份:2014年 无S结构 我希望这个例子会有所帮助 代码如下:C++ 丢失指针,c++,c,pointers,struct,C++,C,Pointers,Struct,当我使用“1”命令插入一个新的结构a,这样我已经有一个或多个结构a链接到一个或多个结构时,我就失去了以前的结构a到它们的结构的链接。 例如: 命令1和命令3:2014和命令4 输出: 年份:2014年 matricola:1 现在命令1,然后命令3:2015和命令4 输出: 年份:2015年 matricola:2 年份:2014年 无S结构 我希望这个例子会有所帮助 代码如下: #include <stdio.h> #include <stdlib.h> struct
#include <stdio.h>
#include <stdlib.h>
struct S
{
int matr;
struct S* next;
};
struct A
{
int year;
struct A *nextA;
struct S *nextS;
};
int years = 2013;
int matricola=0;
void insA(struct A**T);
void printA(struct A*T);
void insS(struct A **T);
void printS(struct A *T);
int main()
{
struct A *T=NULL;
int cmd,sc=0;
while(1)
{
printf("\n command:");
sc=scanf("%d",&cmd);
while(sc==0)
{
printf("\nerror:");
fflush(stdin);
sc=scanf("%d",&cmd);
}
if(cmd==1)
{
insA(&T);
}
if(cmd==2)
{
printA(T);
}
if(cmd==3)
{
insS(&T);
}
if(cmd==4)
{
printS(T);
}
}
return 0;
}
void insA(struct A**T)
{
struct A *new_p=(struct A*)malloc(sizeof(struct A));
if(new_p==NULL)
{
printf("Errore");
exit(0);
}
years++;
new_p->nextA=NULL;
new_p->nextS=NULL;
if((*T)==NULL)
{
(*T)=new_p;
}
else
{
new_p->nextA=(*T);
(*T)=new_p;
}
new_p->year=years;
}
void printA(struct A *T)
{
struct A *tmp=T;
while(tmp!=NULL)
{
printf("\n%d",tmp->year);
tmp=tmp->nextA;
}
return;
}
void insS(struct A **T)
{
int search,sc=0;
struct S* new_p=(struct S*)malloc(sizeof(struct S));
if(new_p==NULL)
{
printf("error");
exit(0);
}
new_p->next=NULL;
printf("\nyear in which to insert:");
sc=scanf("%4d",&search);
while(sc==0 || search > years || search <= 2013)
{
printf("\nerror:");
fflush(stdin);
sc=scanf("%4d",&search);
}
struct A*tmp=*T;
while(tmp!=NULL)
{
if(tmp->year==search)
{
matricola++;
if(tmp->nextS==NULL)
{
tmp->nextS=new_p;
}
else
{
new_p->next=tmp->nextS;
tmp->nextS=new_p;
}
}
tmp=tmp->nextA;
}
new_p->matr=matricola;
return;
}
void printS(struct A *T)
{
struct A *tmp=T;
struct S *s=tmp->nextS;
while(tmp!=NULL)
{
printf("\nyear:%d",tmp->year);
if(s==NULL)
{
printf("\nno S struct");
return;
}
else
{
while(s!=NULL)
{
printf("\nmatricola:%d",s->matr);
s=s->next;
}
}
tmp=tmp->nextA;
}
}
这是我的第一篇帖子,所以我为所有错误道歉。在努力理解您想要做什么之后,我发现了您的问题,您必须更改打印功能
void printS(struct A *T) {
struct A *tmp=T;
truct S *s = tmp->nextS;
while(tmp != NULL) {
printf("\nyear:%d", tmp->year);
if(s == NULL) {
printf("\nno S struct");
return ;
} else {
while(s != NULL) {
printf("\nmatricola:%d",s->matr);
s = s->next;
}
}
tmp = tmp->nextA;
}
}
像这样
void printS(struct A *T) {
struct A *tmp=T;
while(tmp != NULL) {
struct S *s = tmp->nextS;
printf("\nyear:%d", tmp->year);
if(s == NULL) {
printf("\nno S struct");
return ;
} else {
while(s != NULL) {
printf("\nmatricola:%d",s->matr);
s = s->next;
}
}
tmp = tmp->nextA;
}
}
因为结构S*S=tmp->nextS;必须更新到您所在的实际结构A,因此如果您离开结构S*S=tmp->nextS,它必须在while循环中;在while循环之外,您将尝试打印从第一个结构A开始的结构列表,而不是从每个结构A开始的整个结构列表
注意:如我所说,尽量避免使用fflushstdin;因为如果参数不指向输出流,则行为是未定义的。如果代码正确缩进,则代码更易于阅读。你有一个可以为你做这件事的源格式化程序吗?如果你有一个if cmd==5 exit1;正确清理cource后。不建议使用fflushstdin,它将与stdout一起使用。当您单步执行代码时,调试器会告诉您什么?您应该清楚使用哪种语言编写代码。是C还是C++?谢谢,现在工作得很好。你说得对,我应该澄清这个计划的目的。