Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node
{
   int data; // Change data type from char to int
   struct Node *next;
};

/* Given a reference (pointer to pointer) to the head of a list and
an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
    /* 1. allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for the new node

    /* 2. put in the data */
    new_node->data = new_data;

    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);

    /* 4. move the head to point to the new node */
    (*head_ref) = new_node;
}

// This function prints contents of linked list starting from head
void printList(struct Node *node)
{
    while (node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    /* Start code segment to read input: Do not touch */
    int num_of_inputarray;
    int i = 0;
    int input_integer;
    scanf("%d", &num_of_inputarray);
    for (i = 0; i < num_of_inputarray; i++)
    {
        scanf("%d", &input_integer);
        push(&head, input_integer);
    }
    /* End code segment to read input: Do not touch */

    /*
        Print the linked List
    */

    printList(head);

    return 0;
}