// test.cpp : Defines the entry point for the console application.
//

#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()
	{
		data = 0;
		next = NULL;
	}
};

Node *head = NULL;// Node();

// Insert data at the beggining of the list
void InsertFront(int y)
{
	Insertnth(0, y);
}

// 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)
{
	if (head == NULL || pos < -1)
		return;
	
	if(pos > 0)
	{
		Node* tmpNode = head;
		for (int i = 0; i < pos; i++)
		{
			if (i == pos - 1 || tmpNode->next == NULL)
				break;

			tmpNode = tmpNode->next;
		}

		Node* newNode = new Node;
		newNode->data = x;
		newNode->next = tmpNode->next;
		tmpNode->next = newNode;
	}
	else
	{
		Node* newNode = new Node;
		newNode->data = x;
		newNode->next = head;
		head = newNode;
	}
}

int main()
{
    int i, x, n, pos, y;
	head = new Node;
	head->data = 0;
	curlHead = head;

    cout << ("How many numbers\n");
    cin >> n;
    cout << ("Enter numbers\n");
    for (i = 0; i < n; i++)
    {
        cin >> y;
        InsertFront(y);
    }

	Print();
    cout << endl;

	cout << ("Enter pos\n");
	cin >> y;
    Insertnth(y, 69);
	Print();
	cout << endl;

	do 
	{
		Node* tmpNode = head;
		head = head->next;
		delete tmpNode;
	} while (head != NULL);
	
    return 0;
}

