Solved programming problem

Insert Linked List Where every value insert at the First Index in C.

Source code

#include <stdio.h>

#include <stdlib.h>

struct student

{

    int data;

    int *next;

};

typedef struct student node;

int print(node *i)

{

    printf("Your Array: 
");

    while(i!=NULL)

    {

        printf("%d ",i->data);

        i=i->next;

    }printf("
");

    return;

}

int main()

{

    node *start,*temp;

    int flag,i;

    start =NULL;

    do  ///print all element until NULL

    {

        printf("Do you Want to Create a New Node?(1/0)
");

        scanf("%d",&flag);

        if(flag !=1) break;

        temp = (node *)malloc(sizeof(node));

        printf("Please Enter First Value: ");

        scanf("%d",&temp->data);

        temp->next = NULL; ///save the data next to NULL

        if(start==NULL)

        {

            start = temp;

        }

        else

        {

             temp -> next = start; /// save start point address as 2nd address

             start=temp;  ///replace the start value with new value

        }

    }

    while(flag==1);

    print(start);

    return 0;

}

Output

Do you Want to Create a New Node?(1/0)

1

Please Enter First Value:

4

Do you Want to Create a New Node?(1/0)

1

Please Enter First Value:

6

Do you Want to Create a New Node?(1/0)

1

Please Enter First Value:

7

Do you Want to Create a New Node?(1/0)

0

Your Array:

7 6 4

Join the discussion

Add your comment