我试图在单个链表中存储多个数据项。我曾尝试使用 2 个数据项,程序没有错误,因为插入数据也可以正常工作,但程序在打印输出时停止。我不知道代码有什么问题。 任何帮助将不胜感激。
#include<stdio.h>
#include<stdlib.h>
struct node{
float a;
float b;
struct node *next;
};
struct node *head;
void insert(float x,float y){
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->a = x;
temp->b = y;
temp->next = head;
head= temp;
}
void print(){
struct node *temp;
temp= head;
printf("\n the linked list is :");
while(head!=NULL){
printf("data is : %f %f",temp->a,temp->b);
temp=temp->next;
}
}
int main()
{
int n,i,x,y;
head = NULL;
struct node *temp;
printf("\n enter the number of data to enter:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n enter x-cordinate: \n");
scanf("%f",&x);
printf("\n eter y-cordinate: \n");
scanf("%f",&y);
insert(x,y);
}
print();
return 0;
}
最佳答案
你在 print()
函数中有错误
while (head!=NULL)
应该是
while (temp!=NULL)
关于c - 在单个链表中存储多个数据项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31032787/