Datastrcutre LinkedList BASIC

时间:2017-03-13 05:47:43

标签: data-structures linked-list

    **## 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?

它在前面的链表列表中我已经正确地声明了所有内容并在c中实现但是我无法得出结果。

  

我正在研究基本的,当我尝试这个我无法得到答案我跟着我的代码Skool的视频,但没有为我工作

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;**  

1 个答案:

答案 0 :(得分:0)

用这个修改插入函数: - `

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;
    }
}

`