c - 在单个链表中存储多个数据项

标签 c singly-linked-list

我试图在单个链表中存储多个数据项。我曾尝试使用 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/

相关文章:

c - 如何以无人身份运行程序?

c - 编辑控件显示正方形而不是返回

c - 将双指针和指针传递给函数时出现段错误

c++ - 跑完定制迭代器的末尾

python - 如何在Python中将一维C数组绘制为曲面

c - DialogBox 未正确呈现?

c - stdio.h 函数 fopen() "w"模式下的错误场景

c++ - C++链表数据结构中删除挂起

algorithm - 面试题: Merge two sorted singly linked lists without creating new nodes

c++ - 单链表 C++ 上的归并排序