首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinkedList BASIC

LinkedList BASIC
EN

Stack Overflow用户
提问于 2017-03-13 05:47:42
回答 1查看 30关注 0票数 0
代码语言:javascript
复制
    **## GETTING COMPILE TIME ERROR ##
        #include<stdio.h>
        #include<stdlib.h>

        struct Node
        {
            int data;
            struct Node* next;
        };

        struct Node* head;

        void insertFirst(int value)
        {
            Node* temp = (Node*)malloc(sizeof(struct Node));
            temp -> data = value;
            temp -> next = head;
            head = temp;
        }
        void display()
        {
            struct Node* temp1 = head;
            while(temp1!=NULL)
            {
                printf("%d",temp1->data);
                temp1 = temp1->next;
            }

        }
        int main()
        {
            head = NULL;
            int numbers,i, dat;
            printf("How many numbers u want to insert?");
            scanf("%d", &numbers);
            for(i = 0; i < numbers; i++)
            {
                printf("\nEnter the number:");
                scanf("%d",&dat);
                insertFirst(dat);
                displayAll();

            }

        }



    /////////////////////////////////////////////////////////////////////
How to solve this error?

它在linkedlist上,在前面插入,我已经正确声明了一切,并在c中实现了,但是我不能想出结果。

我正在学习基本知识,当我尝试这个问题时,我找不到答案,我在代码Skool上看了一段视频,但对我没有用。

代码语言:javascript
复制
Compile time Error:

    main.c:12:24: error: expected expression before ')' token                                                                      
         Node* temp = (Node*)malloc(sizeof(struct Node));                                                                          
                            ^                                                                                                      
    main.c:13:10: error: request for member 'data' in something not a structure or union                                           
         temp -> data = value;                                                                                                     
              ^                                                                                                                    
    main.c:14:10: error: request for member 'next' in something not a structure or union                                           
         temp -> next = head;                                                                                                      
              ^                                                                                                                    
    main.c:15:10: warning: assignment from incompatible pointer type [enabled by default]                                          
         head = temp;**  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-13 06:28:45

用以下方法修改insert函数:

代码语言:javascript
复制
void insert(int num)
{
    if(head==NULL)
    {
        struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
        temp -> data = num;
        temp -> next = NULL;
        head = temp;
    }
    else
    {
        struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node));
        temp -> data = num; 
        temp -> next = head;
        head = temp;
    }
}

`

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42757301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档