#include <stdio.h>
#include<stdlib.h>

typedef struct Node{
    int data;
    struct Node* next;
}Node;

Node* createNode(int val){
    Node* n=(Node *)malloc(sizeof(Node));
    n->data=val;
    n->next=NULL;
    
    return n;
}

void insertAtEnd(Node** head,int val){
    Node* newNode=createNode(val);
    if(*head==NULL){
        *head=newNode;
        (*head)->next=*head;
        return;
    }
    Node* temp=*head;
    while(temp->next!=*head){
        temp=temp->next;
    }
    temp->next=newNode;
    newNode->next=*head;
}

void insertAtBeginning(Node** head,int val){
    Node* newNode=createNode(val);
    Node* tail=NULL;
    if(*head==NULL){
        *head=newNode;
        (*head)->next=*head;
        *tail=head;
        return;
    }
    newNode->next=*head;
    (*tail)->next=newNode;
    *head=newNode;
}

void display(Node* head){
    Node* temp=head;
    do{
        printf("%d->",temp->data);
        temp=temp->next;  
    }while(temp!=head);
    
}

int main() {
    Node* head=NULL;
    insertAtBeginning(&head,10);
    insertAtBeginning(&head,20);
    insertAtBeginning(&head,30);
    insertAtBeginning(&head,40);
    display(head);
    return 0;
}