问题描述
我是数据结构的新手,通过 udemy 和在线课程学习,最近我写了一个链表来删除一个数据为 int 的特定节点,但后来当我使用数据作为字符串实现它时,我不断收到分段错误。请检查并让我知道我需要如何继续...
#include <stdio.h>
#include<stdlib.h>
struct Node
{
char *data;
struct Node* next;
};
int main()
{
int n;
int i;
char *temp;
char* string;
struct Node* start;
struct Node *newNode1;
start = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the number of the nodes::\n");
scanf("%d",&n);
printf("Enter the strings inside each Node:\n");
scanf("%s",temp);
//First node must be a point to start variable
struct Node* temp1,*temp2;
temp1=start;
temp1->data = temp;
for(i=2;i<=n;++i)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
scanf("%s",temp);
newNode->data = temp;
temp1->next = newNode;
temp1=temp1->next;
temp1=start;
}
printf("Enter the specific Node you wanted to delete:\n");
scanf("%s",temp);
while(temp1->next!=NULL)
{
if(temp1->data==temp)
{
break;
}
else
{
temp2=temp1;
temp1=temp1->next;
}
}
temp2->next = temp1->next;
printf("Now the temp2 and temp1 values are %p and %p",temp1,temp2);
free(temp1);
/*display*/
printf("The elements are:\n");
temp1 =(struct Node*) malloc(sizeof(struct Node));
temp1=start;
while(temp1->next!=NULL)
{
printf("%s->",temp1->data);
temp1=temp1->next;
}
printf("%s->NULL",temp1->data);
return 0;
}
如果有人能解释为什么我会出现分段错误,以及我如何解决这个错误,以便得到一个链接列表作为马铃薯->tamato->brinjal->apple->grape 作为输出。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)