繁体   English   中英

C中的指针和链接列表-程序的异常行为

[英]pointers and linked list in C- unexpected behavior of program

我最近开始使用C进行编程,并且已经从事链表程序一段时间了。 现在,该程序将具有一个配置文件,您可以在其中注册观看的电影,然后将其保存为.txt文件。 问题出在电影上。 当我尝试打印它时,字段将显示为空,好像我没有正确分配指针一样,但事实是该程序知道我在个人资料中放了一部电影。 我知道这是一个很难回答的问题,我们将不胜感激。 我将在此处显示Insertmovie函数,我认为可能是问题所在,以及moviecopy函数(我测试了该函数并且无法正常工作,尽管我怀疑自己在那里做错了什么):

int stacknewmovie (movie* p, list* l){
    if(!p || !l){
        return 0;
    }
    node* n;
    n=newnode();
    if(!n){
        return 0;
    }
    insertnodeinfo(n, p);
    n->next=NULL;
    if(l->first==NULL){
        l->first=n;
        return 1;
    }else{
        n->next=l->first;
        l->first=n;
        return 1;
    }
}

这是电影拷贝:

int moviecopy(movie* pel2,movie* pel1){
    if(!pel1 || !pel2){
        return NULL;
    }else{
        pel2=pel1;
        return 1;
    }
}

再次感谢您抽出宝贵的时间。 我不知道如何更好地展示我的问题,因为编译器甚至没有警告我任何事情。

node* insertnodeinfo(node* n, movie* p){
    if(!p || !n){
        return NULL;
    }else{
        moviecopy(n->info, p);
        return n;
    }
}
int stacknewmovie (movie* p, list* l){
if(!p || !l){
   return 0; 
}
 node* n;
 n=newnode();
if(!n){
  return 0;
}
insertnodeinfo(n, p);
n->next=NULL;
if(l->first==NULL){
    l->first=node;                 //problem here replace "node" by "n"??
    return 1;
}else{
    n->next=l->first;
    l->first=n;
    return 1;
}

就像我在注释中所做的那样,首先将“节点”替换为“ n”(我想您要写入“ n”而不是“ node”)

我认为这不是链接列表,因为:

n->next=l->first; //this made the list a circular linked list 
l->first=n;       //Here is the problem I think 

因为新节点总是成为列表中的第一个节点,所以这是您想要的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM