#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
};
struct node * root=NULL;
void append();
void addbegin();
void display();
int length();
void insert();
void delet();
void delend();
void deletnth();
void reverselist();
int len;
int main()
{
    int ch;
    while(1)
    {
        printf("1.append element\n");
        printf("2.length of list\n");
        printf("3.display element\n");
        printf("4.exit\n");
        printf("5.addbegin\n");
        printf("6.insert\n");
        printf("7.delet at begining\n");
        printf("8.Delete at the End\n");
        printf("9.delete element at nth position\n");
        printf("10.reverse link list\n");
        printf("Enter your choice\n");
        scanf("%d",&ch);
           switch(ch)
           {
          case 1:
            append();
            break;
          case 2:
            len =length();
             printf("length of List is ::%d\n",len);
            break;
          case 3:
            display();
                break;
          case 4:
            exit(1);
          case 5:
            addbegin();
            break;
          case 6:
            insert();
            break;
          case 7:
            delet();
            break;
          case 8:
            delend();
            break;
          case 9:
            deletnth();
            break;
          case 10:
            reverselist();
            break;
          default:
            printf("Invalid choice\n");
           }
    }
}
void append()
{
    struct node * temp;
    temp=(struct node*)malloc(sizeof(struct node));
    printf("Enter node data\n");
    scanf("%d",&temp->data);
    temp->next=NULL;
    if(root==NULL)
        root=temp;
    else
    {
        struct node * p;
        p=root;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=temp;
    }
}
int length()
{
struct node *p;
p=root;
int count =0;
while(p!=NULL)
{
    count++;
    p=p->next;
}
return count;
}
void display()
{
  struct node * temp;
  temp=root;
  if(temp==NULL)
        printf("List is Empty\n");
  else
  {
      while(temp!=NULL)
      {
          printf("%d-->",temp->data);
          temp=temp->next;
      }
      printf("NULL\n");
  }
}
void addbegin()
{
    struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter node data to add begin\n");
scanf("%d",&temp->data);
temp->next=NULL;
if(root==NULL)
    root=temp;
else
{
    temp->next=root;
    root=temp;
}
}

void insert()
{
    int loc,count=0;
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
    printf("Enter Data\n");
    scanf("%d",&temp->data);
    temp->next=NULL;
    struct node* p,*z;
    z=root;
    p=root;
printf("Enter location where you want to insert node \n");
scanf("%d",&loc);
while(z!=NULL)
{
    count++;
    z=z->next;
}
if(root==NULL)
    root=temp;
else if(loc>length())
    printf("insertion not possible\n");
else
{
    int i;
    for(i=0;i<loc-1;i++)
    {
        p=p->next;
    }
    temp->next=p->next;
    p->next=temp;
}
}
void delet()
{
    struct node * temp;
    if(root==NULL)
        printf("delete is not possible\n");
    else
    {
        temp=root->next;
        root=temp;
    }
}
void delend()
{
    int count=0,i;
    struct node *p;
    struct node *t;
    p=root->next;
    if(root==NULL)
        printf("NO element present in the list so you cant delete element\n");
    else
    {
        while(p->next!=NULL)
        {
            t=p;
            p=p->next;

        }
        free(t->next);
        t->next=NULL;
    }

}
void deletnth()
{
    int i=1,loc;
    struct node *temp=root;
    printf("Enter location where you want to delete Node\n");
    scanf("%d",&loc);
    if(loc>length())
        printf("invalid location\n");
    else if(loc==1)
    {
        root=temp->next;
        temp->next=NULL;
        free(temp);
    }
    else
    {
        while(i<loc-1)
        {
            temp=temp->next;
            i++;


        }
        struct node *p=temp->next;
        temp->next=p->next;
        p->next=NULL;
        free(p) ;

    }
}
void reverselist()
{
    int i,j,k,temp,len;
    len=length();
    struct node * p,*q;
    p=q=root;
    i=0;
    j=len-1;
    while(i<j)
    {
        k=0;
        while(k<j)
        {
            q=q->next;
            k++;
        }
        temp=p->data;
        p->data=q->data;
        q->data=temp;
        p=p->next;
        q=root;
        i++;j--;
    }
}