
//C++ Program for Singly Linked List and Insert at Beggining

#include <iostream>
#include <stdlib.h>
using namespace std;
void InsertFront(int x);
void Insertnth(int pos, int x);
void Print();

struct Node
{
    int data;
    struct Node *next;
};
Node *head = new Node();

// Insert data at the beggining of the list
void InsertFront(int y)
{

    Node *newnode = new Node();
    newnode->data = y;
    newnode->next = head;
    head = newnode;
}

// Traversing and Print the data of the List
void Print()
{

    Node *temp = head;
    cout << "List is: " << endl;
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
}

// Insert data at nth postition in List
void Insertnth(int pos, int x)
{
    int i;

    Node *point = new Node();
    point = head->next;

    Node *newnode = new Node();
    newnode->data = x;

    for (i = 0; i <= pos; i++)
    {
        point = point->next;
    }
    newnode->next = point;
    point = head->next;

    for (i = 0; i < pos; i++)
    {
        point = point->next;
    }
    point->next = newnode;
    point = head->next;

    // while (head != NULL)
    // {
    //     cout << point->data;
    //     point = point->next;
    //     cout << point->next;
    // }
    Print();
}

int main()
{
    int i, x, n, pos, y;
    head = NULL;
    cout << ("How many numbers\n");
    cin >> n;
    cout << ("Enter numbers\n");
    for (i = 0; i < n; i++)
    {
        cin >> y;
        InsertFront(y);
        Print();
    }
    cout << endl;
    Insertnth(2, 69);

    return 0;
}