Untitled

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

// A linked list node
struct Node
{
   int data;
   struct Node *next;
};


/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_data)
{
	/* 1. allocate node */
	struct Node* new_node = (struct Node*) malloc(sizeof(char));

	struct Node *last = *head_ref; 
    int return_val = 0; 

	
	new_node->data = new_data;
    new_node->next = NULL;
	
	if (*head_ref == NULL)
	{
	    *head_ref = new_node;
	    return;
	}

    while (last->next != NULL)
        last = last->next;
   
    last->next = 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);
        append(&head, input_integer);

    }
    /* End code segment to read input: Do not touch */

    /*
        Print the linked List
    */
    
    printList(head);

    return 0;
}