gpt4 book ai didi

c - 无法将数据添加到双向链表的 C 实现中 : Segmentation Fault

转载 作者:行者123 更新时间:2023-11-30 17:39:13 25 4
gpt4 key购买 nike

我有以下程序,我试图创建一个双向链表来存储有关 MP3 文件的数据。但是,当我编译并运行该程序时,我收到“段错误”。不确定该怎么办!有人可以帮忙吗?这是我的代码:

/*
* C Program to Implement a Doubly Linked List & provide Insertion, Deletion & Display Operations
*/
#include <stdio.h>
#include <stdlib.h>

struct MP3
{
struct MP3 *prev;
char *artist;
char *album;
char *song_title;
char *release_date;
int runtime;
struct MP3 *next;
}*h,*temp,*temp1,*temp2,*temp3;

void insert1();
void insert2();
void traversebeg();
void traverseend();
void delete();

int count = 0;

void main()
{
int ch;

h = NULL;
temp = temp1 = NULL;

printf("\n 1 - Insert at beginning");
printf("\n 2 - Insert at end");
printf("\n 3 - Delete ");
printf("\n 4 - Display from beginning");
printf("\n 5 - Display from end");
printf("\n 6 - Exit");

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
delete();
break;
case 4:
traversebeg();
break;
case 5:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend();
}
break;
case 6:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}

/* TO create an empty node */
void create()
{
int data;

temp =(struct MP3 *)malloc(1*sizeof(struct MP3));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter Artist Name to database : ");
scanf("%s", temp->artist);
printf("\n Enter Album Name to database : ");
scanf("%s", temp->album);
printf("\n Enter Song Name to database : ");
scanf("%s", temp->song_title);
printf("\n Enter Release Date to database : ");
scanf("%s", temp->release_date);
printf("\n Enter runtime to database : ");
scanf("%d", temp->runtime);
count++;
}

/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp3 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}

/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}


/* To delete an element */
void delete()
{
int i = 1, pos;

traversebeg();
printf("\n Enter position to be deleted : ");
scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))
{
printf("\n Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf("\n Error : Empty list no elements to delete");
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}

/* Traverse from beginning */
void traversebeg()
{
temp2 = h;

if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");

while (temp2->next != NULL)
{
printf(" %s \n", temp2->artist);
printf(" %s \n", temp2->album);
printf(" %s \n", temp2->song_title);
printf(" %s \n", temp2->release_date);
printf(" %d \n", temp2->runtime);
temp2 = temp2->next;
}
printf("\n %d Entries in the Database ", count);
}

/* To traverse from end */
void traverseend()
{
temp2 = h;
while (temp2 != temp3)
{
printf(" %s \n", temp2->artist);
printf(" %s \n", temp2->album);
printf(" %s \n", temp2->song_title);
printf(" %s \n", temp2->release_date);
printf(" %d \n", temp2->runtime);
temp2 = temp2->prev;
}
}

最佳答案

您还必须分配:

char *artist;
char *album;
char *song_title;
char *release_date;

当您分配 struct MP3 时,它不会同时分配内部参数。您可以像这样修改 create() 函数:

temp =(struct MP3 *)malloc(1*sizeof(struct MP3));
#define MAX_LEN 100
temp->artist =(char *)malloc(MAX_LEN*sizeof(char));
temp->album =(char *)malloc(MAX_LEN*sizeof(char));
temp->song_title =(char *)malloc(MAX_LEN*sizeof(char));
temp->release_date =(char *)malloc(MAX_LEN*sizeof(char));

其中 MAX_LEN 是所有这些字符串的最大长度。

scanf 中还缺少 &:temp->runtime 是一个整数,因此必须在前面加上 & 才能读取它。

scanf("%d", &(temp->runtime));

关于c - 无法将数据添加到双向链表的 C 实现中 : Segmentation Fault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com