Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链表中字符串的问题。字段将被最后一个节点覆盖';s输入_C_String_Pointers_Linked List - Fatal编程技术网

C 链表中字符串的问题。字段将被最后一个节点覆盖';s输入

C 链表中字符串的问题。字段将被最后一个节点覆盖';s输入,c,string,pointers,linked-list,C,String,Pointers,Linked List,我一直在尝试创建一个相册目录,其中链接列表上的每个节点都有一个索引(代码)、艺术家的姓名和相册类型。然而,由于某种原因,每次我尝试打印列表时,它都会显示我为每个节点分配的正确索引,但对于每个项目,显示的艺术家和主题都是我为最后一个节点输入的。也就是说,如果我为一个节点输入了'1,Oasis和Definetely_Maybe',为第二个节点输入了'5,Aerosmith和Pump',当我运行print_list时,它会显示出来 Code: 1 Artist: Aerosmith Album:

我一直在尝试创建一个相册目录,其中链接列表上的每个节点都有一个索引(代码)、艺术家的姓名和相册类型。然而,由于某种原因,每次我尝试打印列表时,它都会显示我为每个节点分配的正确索引,但对于每个项目,显示的艺术家和主题都是我为最后一个节点输入的。也就是说,如果我为一个节点输入了'1,Oasis和Definetely_Maybe',为第二个节点输入了'5,Aerosmith和Pump',当我运行print_list时,它会显示出来

Code: 1

Artist: Aerosmith

Album: Pump

不知何故,它会用最后一个节点覆盖第一个节点的艺术家和专辑tytle。无论在结束运行之前输入了多少节点,都会发生这种情况

我知道这是非常新手,但我刚刚开始编程,非常感谢您的帮助。代码如下。非常感谢

 #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100

    typedef struct n {int code; char *artist; char *album; struct n* next;} Node;

    void start_list (Node **first);
    Node *create_node (int code, char *art, char *alb);
    void insert_list (Node **first, Node *next);
    void print_list (Node *p);
    void exit_list (Node **p);

    int main(int argc, char *argv[])
    {
      Node *first;
      Node *new;
      int n;
      char artist[MAX];
      char album[MAX];
      start_list(&prim);
      do{
        printf("Please enter a number for the next Node or 0 to exit the list: ");
        scanf("%d",&n);
        if(n==0)break;
        printf("\nNow enter the artist's name: ");
        scanf("%s",&artist);
        printf("\nType the album tytle now: ");
        scanf("%s",&album);
        printf("\n\nCode: %d ",n);
        printf("\nArtist: %s ",artist);
        printf("\nAlbum: %s \n",album);
        new=create_node(n,artist,album);
        insert_list(&first,new);
      }while(n!=0);
      print_list (prim);
      exit_list(&prim);
      system("PAUSE");  
      return 0;
    }

    void start_list (No **prim){
      *prim=NULL;
    }  

    Node *create_node (int code, char *art, char *alb){
      Node *new;
      new=(Node*)malloc(sizeof(Node)); 
      new->code=code;
      new->artist=art;
      new->album=alb;
      new->next=NULL;
      return new;
    }

    void insert_list (Node **first, Node *new){
      new->next=*first;
      *first=new;
    }

    void print_list (Node *p){
      Node *aux=p;
      while (aux!=NULL){
        printf("\n\nCode: %d ",aux->code);
        printf("\nArtist: %s ",aux->artist);
        printf("\nAlbum: %s \n",aux->album);
        aux=aux->next;
      }
    }

    void exit_list (Node **p){
      Node *aux=*p;
      while(aux!=NULL){
        *p=(*p)->next;
        free(aux);
        aux=*p;
      }
    }
#包括
#包括
#定义最大值100
typedef结构n{int code;char*艺术家;char*相册;结构n*下一步;}节点;
无效开始列表(节点**第一);
节点*创建_节点(int代码,char*art,char*alb);
作废插入列表(节点**第一,节点*下);
作废打印列表(节点*p);
无效退出列表(节点**p);
int main(int argc,char*argv[])
{
节点*第一;
节点*新建;
int n;
字符艺术家[MAX];
字符相册[MAX];
启动列表(&prim);
做{
printf(“请输入下一个节点的编号,或输入0以退出列表:”;
scanf(“%d”和“&n”);
如果(n==0)中断;
printf(“\n现在输入艺术家的姓名:”);
scanf(“%s”和“艺术家”);
printf(“\n现在键入相册类型:”;
scanf(“%s”和相册);
printf(“\n\n代码:%d”,n);
printf(“\n艺术家:%s”,艺术家);
printf(“\n文件夹:%s\n”,相册);
新建=创建_节点(n、艺术家、相册);
插入_列表(&第一,新);
}而(n!=0);
打印列表(prim);
退出列表(&prim);
系统(“暂停”);
返回0;
}
无效开始列表(编号**prim){
*prim=NULL;
}  
节点*创建_节点(int代码、char*art、char*alb){
节点*新建;
new=(Node*)malloc(sizeof(Node));
新建->编码=编码;
新->艺术家=艺术;
新建->相册=alb;
新建->下一步=空;
归还新的;
}
无效插入列表(节点**第一,节点*新){
新建->下一步=*第一步;
*第一个=新的;
}
无效打印列表(节点*p){
节点*aux=p;
while(aux!=NULL){
printf(“\n\n代码:%d”,辅助->代码);
printf(“\n艺术家:%s”,辅助->艺术家);
printf(“\n文件夹:%s\n”,辅助->相册);
aux=aux->next;
}
}
无效退出列表(节点**p){
节点*aux=*p;
while(aux!=NULL){
*p=(*p)->下一步;
免费(aux);
aux=*p;
}
}
在退出列表中,在
释放(aux)
之前释放它们


create_node函数将字符指针“艺术家和相册”设置为指向主函数中的艺术家和相册数组。每次迭代都只是重写存储在这些数组中的内容。因此,每个节点都指向相同的字符串,并且在流程结束时,最近输入的字符串位于数组中


您需要为每个新字符串分配(例如,使用malloc或使用堆的string函数)存储空间,以便字符串在调用之间保持不变并且不会被覆盖。

好吧,我以为在创建每个新节点时都使用了malloc。我如何准确地为每个字符串执行此操作?我还是塔克。但是谢谢你的回复!除了什么是我必须释放的?alb和艺术?免费(alb)和免费(art)?我的退出列表如下:void desalocar_lista(No**p){No*aux=*p;而(aux!=NULL){*p=(*p)->prox;free(aux);aux=*p;}}}}alb和art在那里未声明。我必须做什么?
 #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100

    typedef struct n {int code; char *artist; char *album; struct n* next;} Node;

    void start_list (Node **first);
    Node *create_node (int code, char *art, char *alb);
    void insert_list (Node **first, Node *next);
    void print_list (Node *p);
    void exit_list (Node **p);

    int main(int argc, char *argv[])
    {
      Node *first;
      Node *new;
      int n;
      char artist[MAX];
      char album[MAX];
      start_list(&prim);
      do{
        printf("Please enter a number for the next Node or 0 to exit the list: ");
        scanf("%d",&n);
        if(n==0)break;
        printf("\nNow enter the artist's name: ");
        scanf("%s",&artist);
        printf("\nType the album tytle now: ");
        scanf("%s",&album);
        printf("\n\nCode: %d ",n);
        printf("\nArtist: %s ",artist);
        printf("\nAlbum: %s \n",album);
        new=create_node(n,artist,album);
        insert_list(&first,new);
      }while(n!=0);
      print_list (prim);
      exit_list(&prim);
      system("PAUSE");  
      return 0;
    }

    void start_list (No **prim){
      *prim=NULL;
    }  

    Node *create_node (int code, char *art, char *alb){
      Node *new;
      new=(Node*)malloc(sizeof(Node)); 
      new->code=code;
      new->artist=art;
      new->album=alb;
      new->next=NULL;
      return new;
    }

    void insert_list (Node **first, Node *new){
      new->next=*first;
      *first=new;
    }

    void print_list (Node *p){
      Node *aux=p;
      while (aux!=NULL){
        printf("\n\nCode: %d ",aux->code);
        printf("\nArtist: %s ",aux->artist);
        printf("\nAlbum: %s \n",aux->album);
        aux=aux->next;
      }
    }

    void exit_list (Node **p){
      Node *aux=*p;
      while(aux!=NULL){
        *p=(*p)->next;
        free(aux);
        aux=*p;
      }
    }
  Node* first = NULL;
  new->artist = strdup(art);
  new->album = strdup(alb);
    free(aux->art);
    free(aux->alb);
    free(aux);