Monday 22 September 2014

Introduction to Socket Programming.

Prerequisites :

network5

Before you start learning socket programming make sure you already have a certain basic knowledge to network such as understand what is IP address, TCP, UDP.

Introduction to Client-Server communication

Server
  • passively waits for and responds to clients
  • passive socket

Client


  • initiates the communication
  • must know the address and the port of the server
  • active socket


The server and client both are software but not hardware. It means what is happening on the top is there are two different software executed. To be more precise, the server and client are two different processes with different jobs. 

Understand sockets

Imagine a socket as a seaport that allows a ship to unload and gather shipping, whereas socket is the place where a computer gathers and puts data into the internet.
network3



Socket Types

When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain. There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format.


The address of a socket in the Unix domain is a character string which is basically an entry in the file system.


The address of a socket in the Internet domain consists of the Internet address of the host machine (every computer on the Internet has a unique 32 bit address, often referred to as its IP address). In addition, each socket needs a port number on that host. Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available.


There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communciations protocol. Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.



Sockets - Procedures

Primitives      Meaning

Socket                Create New Communication End Point                      
Bind                    Attach A Local  Address To Socket
Listen                  Announce Willingness to Accept Connections
Accept                Block A Caller until Connection Request Arrives
Connect             Actively Attempt to establish a Connection.
Send                  Send some data over Connection.
Receive              Receive data over the connection
Close                  Close the connection.


The flow chart below shows the interaction between client and server In Case Of Stream(TCP). Every process on the flow chart is needed and it acts a very important roles on network connection.

network4

Sunday 21 September 2014

Binary Search Tree(Insertion,traversal,deletion)- Input the names of cities and insert them in to tree.Traverse the tree and also delete elements from the tree.

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

struct node{
char city[20];
struct node *left;
struct node *right;
struct node *parent;
};


void display(struct node *);
struct node * delete(struct node *root,char del[20]);

struct node * insert(char city[20],struct node *root){
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *prev = (struct node *)malloc(sizeof(struct node));
struct node *new = (struct node *)malloc(sizeof(struct node));
strcpy(new->city,city);
new->left = NULL;
new->right = NULL;
if(root->parent == NULL){
                        //printf("inside root if\n");
                        root = new;
                        new->parent = root;
        }
else{
int right=0;
int left =0;
temp = root;
while(temp != NULL ){
prev = temp;
left = 0;
right = 0;
if(strcmp(temp->city,new->city) < 0){
//printf("insertion in right\n");
temp = temp->right;
right = 1;
}
else{
//printf("insertion in left\n");
temp = temp->left;
left = 1;
}
}
if(right == 1){
//printf("inside right\n");
//printf("parent %s \n",prev->city);
prev->right = new;
new->parent = prev;
right =0;
left = 0;
}
if(left == 1){
//printf("inside left\n");
//printf("parent %s \n",prev->city);
prev->left = new;
new->parent = prev;
right =0;
left =0;
}
}
return root;
}

void display(struct node *current){
if(current !=NULL){
display(current->left);
printf("%s \n",current->city);
display(current->right);
}
}

void main(){
char city[20];
struct node *root = (struct node *)malloc(sizeof(struct node));
root->left = NULL;
root->right = NULL;
root->parent = NULL;
int i;
int num;
char del[20];
int ch=0;
while(1){
printf("press 1 to insert,2 to delete and 3 to exit\n");
scanf("%d",&ch);
switch(ch){
case 1 :
printf("Enter the number of elements you wana insert in tree \n");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("Enter the name of the city\n");
scanf("%s",city);
root = insert( city , root);
//printf("root city is %s\n",root->city);
}
printf("the list is as follows\n");
display(root);
break;
case 2 :
printf("enter the number of elements you wana delete\n");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("Enter the city to be deleated \n");
scanf("%s",del);
root = delete( root , del);
printf("the list is as follows\n");
display(root);
}
break;
case 3 :
exit(0);
break;
default :
printf("invalid input\n");
break;
}
}
}


struct node * delete(struct node *root,char del[20]){
int flag;
int right = 0;
int left = 0 ;
int rt =0;
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp = root;
struct node *prev =(struct node *)malloc(sizeof(struct node));
struct node *temp2 =(struct node *)malloc(sizeof(struct node));
while(temp != NULL){
//printf("current city to be compared is %s\n",temp->city);
//printf("left child is left %s and right child is %s and parent is %s\n",temp->left->city,temp->right->city,temp->parent->city);
if(strcmp(temp->city,del) == 0){
//printf("the city to be deleated is %s right value= %d left value = %d\n",temp->city,right,left);
if(temp == root){
rt = 1;
}
if(temp->right == NULL && temp->left == NULL && (right ==1 || rt == 1)){
if(rt == 1){
printf("tree is empty \n");
rt = 0;
temp->parent = NULL;
free(temp);
}
else{
//printf("the node has no child and is the right child\n");
temp->parent->right = NULL;
temp->parent = NULL;
right = 0;
left  = 0;
free(temp);
}
}
else if(temp->right == NULL && temp->left == NULL && (left == 1 || rt==1)){
if(rt == 1){
printf("TREE IS EMPTY\n");
rt = 0;
temp->parent = NULL;
free(temp);
}
else{
//printf("the node has no child and is the left child\n");
temp->parent->left = NULL;
temp->parent = NULL;
right = 0;
left = 0;
free(temp);
}
}
else if(temp->right == NULL && (left == 1 || rt == 1)){
//printf("the node has left child and is left child to its parent\n");
if(rt == 1){
root =root->left;
root->parent = root;
temp->left = NULL;
temp->right = NULL;
rt = 0;
free(temp);  
}
else{
//printf("the parent of node to be deleted is %s\n",temp->parent->city);
temp->parent->left = temp->left;
//printf("new left child of parent is %s\n",temp->parent->left->city);
//printf("original parent of left child is %s\n",temp->left->parent->city);
temp->left->parent = temp->parent;
//printf("the new parent is %s\n",temp->left->parent->city);
temp->left = NULL;
temp->parent = NULL;
left = 0;
right = 0;
free(temp);
}
}
else if(temp->right == NULL && (right == 1 || rt == 1)){
printf("the node has left child and is right child to its parent\n");
if(rt == 1){
root = root->left;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt =0;
free(temp);
}
else{
//printf("the parent of node to be deleted is %s\n",temp->parent->city);
temp->parent->right = temp->left;
//printf("new right child of parent is %s\n",temp->parent->right->city);
temp->left->parent = temp->parent;
temp->left = NULL;
temp->parent = NULL;
left =0;
right =0;
free(temp);
}
}
else if(temp->left == NULL && (right ==1 || rt == 1)){
//printf("the node has right child and itself is a right child\n");
if(rt == 1){
root = root->right;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt = 0;
free(temp);
}
else{
temp->parent->right = temp->right;
temp->right->parent = temp->parent;
temp->right = NULL;
temp->parent =NULL;
left = 0;
right = 0;
free(temp);
}
}
else if(temp->left == NULL && (left == 1 || rt == 1)){
//printf("the node has right child and itself is left child\n");
if(rt == 1){
root = root->right;
root->parent = root;
temp->left = NULL;
temp->parent = NULL;
rt = 0;
free(temp);
}
else{
temp->parent->left = temp->right;
temp->right->parent = temp->parent;
temp->right = NULL;
temp->parent = NULL;
left = 0;
right = 0;
free(temp);
}
}
else{
//printf("the city to be delated has both left and right child\n");
prev = temp;
temp = temp->right;
printf("the right child of city to be deleated is %s\n",temp->city);
if(temp->left == NULL && (left == 1 || rt == 1)){
if(rt == 1 ){
root = root->right;
                                        root->parent = root;
                                        root->left  = prev->left;
prev->left->parent = root;
prev->left = NULL;
prev->right = NULL;
                                        prev->parent = NULL;
                                        rt = 0;
                                      free(prev);
}
else{
prev->parent->left =prev->right;
prev->right->parent = prev->parent;
prev->left->parent = prev->right;
prev->right->left = prev->left;
prev->parent =NULL;
prev->left = NULL;
prev->right =  NULL;
right = 0;
left = 0;
free(prev);
}
break;
}
if(temp->left == NULL && right == 1){
//printf("the child of city to be deleated has no left child and itself is a right child\n");
                                        prev->parent->right =prev->right;
                                        prev->right->parent = prev->parent;
                                        prev->left->parent = prev->right;
                                        prev->right->left = prev->left;
                                        prev->parent =NULL;
                                        prev->left = NULL;
                                        prev->right =  NULL;
                                        right = 0;
                                        left = 0;
                                        free(prev);
                                        break;
                                }                                                          
else{
temp = temp->left;
}
//printf("before while left child been pionted is %s  and its left child is %s\n",temp->city,temp->left->city);
while(temp->left != NULL){
temp = temp->left;
}
//printf("the child to be replaced with the child to be deleated is %s\n",temp->city);
if(temp->right == NULL && (left == 1 ||rt == 1)){
//printf("the child has no further child and is in the left subtree of del child\n");
if(rt == 1){
root = temp;
rt = 0 ;
                                                temp->parent->left = NULL;
                                                temp->left = prev->left;
                                                temp->right = prev->right;
                                                temp->parent = root;
                                                prev->left->parent = root;
                                                prev->right->parent = root;
                                                prev->left = NULL;
                                                prev->right = NULL;
                                                prev->parent = NULL;
                                                left = 0;
                                                right = 0;
                                                free(prev);

}
else{
temp->parent = prev->parent;
temp->parent->left = NULL;
temp->left = prev->left;
temp->right = prev->right;
temp->parent = prev->parent;
prev->left->parent = temp;
prev->right->parent = temp;
prev->parent->left = temp;
prev->left = NULL;
prev->right = NULL;
prev->parent = NULL;
left = 0;
right = 0;
free(prev);
}
}
else if(temp->right == NULL && right == 1){
//printf("no further children of the child and it is in left right\n ");
temp->parent->left = NULL;
temp->left = prev->left;
temp->right = prev->right;
temp->parent = prev->parent;
prev->left->parent = temp;
prev->right->parent = temp;
prev->parent->right = temp;
prev->left = NULL;
prev->right = NULL;
prev->parent = NULL;
left = 0;
right = 0;
free(prev);
}
else if(temp->right != NULL && (right == 1 || rt == 1)){
//printf("the child has further a right child and is in right subtree of del\n");
if(rt == 1){
root  = temp;
rt = 0;
temp->parent->left = temp->right;
                                        temp->right->parent = temp->parent;
                                        temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = root;
                                        prev->left->parent = root;
                                        prev->right->parent = root;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
}
else{
temp->parent->left = temp->right;
temp->right->parent = temp->parent;
temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = prev->parent;
                                        prev->left->parent = temp;
                                        prev->right->parent = temp;
                                        prev->parent->right = temp;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
}
}
else if(temp->right != NULL && left == 1){
                                        //printf("the child has further children and is in left subtree of del\n");
temp->parent->left = temp->right;
                                        temp->right->parent = temp->parent;
                                        temp->left = prev->left;
                                        temp->right = prev->right;
                                        temp->parent = prev->parent;
                                        prev->left->parent = temp;
                                        prev->right->parent = temp;
                                        prev->parent->left = temp;
                                        prev->left = NULL;
                                        prev->right = NULL;
                                        prev->parent = NULL;
                                        left = 0;
                                        right = 0;
                                        free(prev);
                                }
}
return root;
}
else{
if(strcmp(temp->city,del)>0){
temp = temp->left;
left = 1;
right = 0;
rt  = 0;
}
else if(strcmp(temp->city,del)<0){
temp = temp->right;
right =1;
left = 0;
rt = 0;
}
}

}
return root;
}

Sunday 14 September 2014

Job Scheduler using heap for priotiry que.

/*
I want to design a job scheduler which work in a Shortest Job First Strategy. The input to the scheduler is a Priority Queue that can accept the request for a new job dynamically. Usingthe algorithms discussed in the class, design thisPriority Queue with all possible operationson it.
*/

#include<stdio.h>
#include<stdlib.h>
void max_heapify(int *ar,int *bst,int i,int *size);
void swap(int *a,int *b){
 int temp= *a;
 *a = *b;
 *b = temp;
}
void builtheap(int *ar,int *bst, int *no)
{
 int i; 
 for(i=(*no)/2;i>=1;i--){
//  printf("inside for val of i = %d and no =%d \n",i, *no);  
  max_heapify(ar,bst,i,no);
 }
}
void max_heapify(int *ar,int *bst,int i,int *size){ 
int p1,temp,temp2; 
if(i!=0 && ((2*i+1==(*size) && i == (*size)/2) || (i != (*size)/2) && (*size)!=0)){
// printf("2i+1 ele %d  size %d\n",ar[(2*i)+1],*size); 
 if(ar[2*i+1]<ar[2*i]){
  if(ar[2*i+1]<ar[i]){
   swap(&ar[i],&ar[2*i+1]);
   swap(&bst[i],&bst[2*i+1]); 
   max_heapify(ar,bst,i/2,size);  
  }
  else if(ar[2*i+1]==ar[i]){
   if(bst[2*i+1]<bst[i]){
    swap(&ar[2*i+1],&ar[i]);
    swap(&bst[2*i+1],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }    
  } 
 }
 else if(ar[2*i+1]>ar[2*i]){
  if(ar[2*i]<ar[i]){
   
   swap(&ar[2*i],&ar[i]);
   swap(&bst[2*i],&bst[i]);
   max_heapify(ar,bst,i/2,size);
  }
  else if(ar[2*i]==ar[i]){
   if(bst[2*i]<bst[i]){
    swap(&ar[2*i],&ar[i]);
    swap(&bst[2*i],&bst[i]);   
    max_heapify(ar,bst,i/2,size);
   }
  }
 }
 else{
  if(bst[2*i+1]<bst[2*i]){
   if(ar[2*i+1]<ar[i]){
    swap(&ar[2*i+1],&ar[i]);
    swap(&bst[2*i+1],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }
   else if(ar[2*i+1]==ar[i]){
                         if(bst[2*i+1]<bst[i]){
                                 swap(&ar[2*i+1],&ar[i]);
                                 swap(&bst[2*i+1],&bst[i]);
                          max_heapify(ar,bst,i/2,size);
    }   
   }
  }
  else if(bst[2*i+1]>bst[2*i]){
   if(ar[2*i]<ar[i]){
                         swap(&ar[2*i],&ar[i]);
    swap(&bst[2*i],&bst[i]);
    max_heapify(ar,bst,i/2,size);
   }
                 else if(ar[2*i]==ar[i]){
                         if(bst[2*i]<bst[i]){
                                 swap(&ar[2*i],&ar[i]);
                                 swap(&bst[2*i],&bst[i]);
     max_heapify(ar,bst,i/2,size);
                         }
   } 
  }
  else
  {
   if(ar[i]>ar[2*i]){
    swap(&ar[i],&ar[2*i]);
    swap(&bst[i],&bst[2*i]);
    max_heapify(ar,bst,i/2,size);
   }
   else if(ar[i]==ar[2*i]){
    if(bst[2*i]<bst[i]){
     swap(&ar[i],&ar[2*i]);
                                 swap(&bst[i],&bst[2*i]);
     max_heapify(ar,bst,i/2,size);
    }
   }
  }
 } 
}
if(i!=0 &&(2*i==(*size) && i == (*size)/2 || i != (*size)/2 && (*size)!=0)){
// printf("element 2i = %d size = %d \n",ar[2*i],*size);
 if(ar[i]>ar[2*i]){
         swap(&ar[i],&ar[2*i]);
                swap(&bst[i],&bst[2*i]);
                max_heapify(ar,bst,i/2,size);
         }
        else if(ar[i]==ar[2*i]){
         if(bst[2*i]<bst[i]){
                swap(&ar[i],&ar[2*i]);
                  swap(&bst[i],&bst[2*i]);
                    max_heapify(ar,bst,i/2,size);
                 }
      }

} 
}
void deletion(int *ar,int *bst ,int *size,int *delno){
 if(*size>=1){
  delno[0] = ar[1];
  delno[1] = bst[1];
 // printf("inside del job is with arvl time = %d and bst time = %d\n",delno[0],delno[1]);
 // printf("inside del value of *size is %d\n",*size);  
  swap(&ar[*size],&ar[1]);
  swap(&bst[*size],&bst[1]);
  (*size)--; 
 // printf("size in del = %d \n",*size);
  ar = realloc(ar,((*size)+1) * sizeof(int));
  bst = realloc(bst,((*size)+1)*sizeof(int)); 
  builtheap(ar,bst,size); 
 // int j;  
 // for(j=0;j<=*size;j++){
 //  printf("%d  %d \n",ar[j],bst[j]); 
 // }
 }
}
void main()
{
 int no;
 printf("Enter number of jobs\n");
 scanf("%d",&no);
 int arsize = no+1;
 int *ar = malloc(arsize * sizeof(int));
 int *bst = malloc(arsize * sizeof(int));
 int i=1;
 ar[0]=0;
 bst[0]=0;
 while(no--){
  printf("enter arival time and bust time for %d job\n",i);
  scanf("%d",&ar[i]);
  scanf("%d",&bst[i]);
  i++;
 }
 int size = arsize-1; 
 builtheap(ar,bst,&size);
 int j;
 //for(j=0;j<=size;j++){
 // printf("%d  %d \n",ar[j],bst[j]); 
 //}
 //printf("after builheap\n");
 int delno[2];
 for(j=1 ; j<= arsize-1; j++){
  deletion(ar,bst,&size,delno);  
  printf("%d job is with arvl time = %d and bst time = %d\n",j,delno[0],delno[1]);
 }
} 

Wednesday 3 September 2014

Cicular linked list with pointer used to traverse the link list (as an attempt to reduce complexity at insertion time)

#include<stdio.h>
#include<stdlib.h>
struct node{
    int val;
//  int *p;
    struct node *prev;
    struct node *next;
};

struct linklist{
    int pos;
    int max_pos;
    struct node *nodadd;
};
void createnode(struct linklist *linklist1,struct linklist *first);

void delete(struct linklist *linklist1,struct linklist *first){
    int pos;
    int i;
    if(linklist1->nodadd->prev != NULL){
       linklist1->nodadd = linklist1->nodadd->prev;//inorder to prevent changes in showlist made for insert function
    }
    showlist(linklist1,first);
    printf("enter the element position to be deleted \n");
    scanf("%d",&pos);
    if(pos < linklist1->max_pos){
    //printf("the element being pointed is %d and pos is %d and max pos is %d\n", linklist1->nodadd->val,linklist1->pos,linklist1->max_pos);
    if(linklist1->max_pos == 2 && pos ==1){
        struct node *temp;
        temp = linklist1->nodadd;
        linklist1->nodadd = NULL;
        linklist1->pos = 0;
        linklist1->max_pos = 1;
        first->nodadd = NULL;
        //printf("first node is deleted %p %p\n",first,linklist1);
        free(temp);
        //printf("first node is deleted after temp\n");

    }
    else if(linklist1->max_pos == 3 && pos == 2){
        first->nodadd->next->next =NULL;
first->nodadd->next->prev =NULL;
        struct node *temp;
        temp = first->nodadd->next;
        first->nodadd->next = NULL;
        first->nodadd->prev =NULL;
        free(temp);
        linklist1->nodadd = first->nodadd;
        linklist1->pos = 1;
        linklist1->max_pos = 2;
    }
    else if(linklist1->max_pos == 3 && pos == 1){
        first->nodadd = first->nodadd->next;
        first->nodadd->next->next =NULL;
        first->nodadd->next->prev =NULL;
        struct node *temp;
        temp = first->nodadd->next;
        first->nodadd->next = NULL;
        first->nodadd->prev =NULL;
        free(temp);
        linklist1->nodadd = first->nodadd;
        linklist1->pos = 1;
        linklist1->max_pos = 2;
    }
    else if(linklist1->pos == pos){
        linklist1->nodadd =linklist1->nodadd->prev;
        if(linklist1->pos != 1){
                linklist1->pos = linklist1->pos -1;
        }
        else if(pos == 1) {
                    linklist1->pos = linklist1->max_pos-2;
        }
        linklist1->max_pos = linklist1->max_pos-1;
        //printf("the element to be before deleted is %d and prev pos is %d\n", linklist1->nodadd->val,linklist1->pos);
        struct node *temp,*temp2;
        struct linklist *temp1 =(struct linklist *)malloc(sizeof(struct linklist));
        if(pos == 1){
                temp1->nodadd = linklist1->nodadd;
                first->nodadd = first->nodadd->next;
        }
        else {
                temp1->nodadd = first->nodadd;
                for(i = 1 ; i < pos-1; i++){
                        temp1->nodadd = temp1->nodadd->next;
                }
        }
        //printf("the element to be delated is %d and pos is %d\n",temp1->nodadd->next->val,pos);
        temp = temp1->nodadd->next;
        temp2 = temp1->nodadd;
        temp1->nodadd->next = temp1->nodadd->next->next;
        temp1->nodadd->next->prev = temp2;
        temp->prev = NULL;
        temp->next = NULL;
        free(temp);
        //printf("the value of temp1 in delete %d and temp next is %d\n",temp1->nodadd->val,temp1->nodadd->next->val);
    }
    else{
        //struct linklist *temp1;
        //printf("first node is deleted in last else\n");
        struct node *temp,*temp2;
        struct linklist *temp1 =(struct linklist *)malloc(sizeof(struct linklist));
        if(pos == 1){
                //linklist1->nodadd = linklist1->nodadd->prev;
                temp1->nodadd = first->nodadd->prev;
                first->nodadd =first->nodadd->next;
                //printf("the value of last node in delete %d and first node is %d\n",first->nodadd->prev->val,first->nodadd->val);
                //linklist1->pos = linklist1->max_pos-2;
        }
        else {
                temp1->nodadd = first->nodadd;
                for(i = 1 ; i < pos-1; i++){
                    temp1->nodadd = temp1->nodadd->next;
                }
        }
        //printf("the element to be delated is %d and pos is %d\n",temp1->nodadd->next->val,pos);
        temp  = temp1->nodadd->next;
        temp2 = temp1->nodadd;
        temp1->nodadd->next = temp1->nodadd->next->next;
        temp1->nodadd->next->prev = temp2;
        temp->prev = NULL;
        temp->next = NULL;
        free(temp);
        //printf("the value of temp1 in delete %d and temp next is %d\n",temp1->nodadd->val,temp1->nodadd->next->val);
        if(linklist1->pos == 1) {
                linklist1->pos = linklist1->max_pos -2;
        }
        if(linklist1->pos > pos){
                linklist1->pos = linklist1->pos-1;
        }

        linklist1->max_pos = linklist1->max_pos-1;
        //free(temp->nodadd->next);
    }
    if(first->nodadd !=NULL){
      if(linklist1->nodadd->prev!=NULL){
        linklist1->nodadd = linklist1->nodadd->prev;//function showlist balance this move
        //printf("First value is %d\n",first->nodadd->val);
       }
      //printf("first node is deleted this is before last few lines\n");
      showlist(linklist1,first);
    }
    else if(first->nodadd == NULL){
        printf("\nThe List is empty\n");
    }
        createnode(linklist1,first);
    }
    else{
printf("\n Invalid positon plz reenter the position\n");
if(linklist1->nodadd->next != NULL){
linklist1->nodadd = linklist1->nodadd->next;
}
delete(linklist1,first);
    }
}
void createnode(struct linklist *linklist1,struct linklist *first){
    int num;
    int pos;
    int ch;
    printf("\npress 1 to delete ,2 to insert and 3 to exit \n");
    scanf("%d",&ch);
    switch(ch){
    case 1 :
        if(linklist1->nodadd == NULL){
            printf("Sorry no element in list \n");
            createnode(linklist1,first);
        }
 else
            delete(linklist1,first);
        break;
    case 2 :
        /*printf("%d\n",linklist1->pos);
        if(linklist1->nodadd != NULL)
            printf("%d\n",linklist1->nodadd->val);
        printf("%d\n",linklist1->max_pos);
        */
        printf("enter the number and position of num\n");
        scanf("%d %d",&num,&pos);
        if(pos > (linklist1->max_pos) || pos <=0){
            printf("invalid pos \n");
        }
        else if(pos == 1 && linklist1->nodadd == NULL){
            struct node *node1;
            node1 = (struct node*)malloc(sizeof(struct node));
            node1->val = num;
            node1->prev = NULL;
            node1->next = NULL;
            linklist1->pos =1;
            linklist1->max_pos = 2;
            linklist1->nodadd = node1;
            first->nodadd = node1;
        }
        else if(pos == 1 && linklist1->nodadd->next == NULL){
            struct node *node1;
            node1 = (struct node*)malloc(sizeof(struct node));
            node1->val = num;
            node1->next = linklist1->nodadd;
            node1->prev = linklist1->nodadd;
            linklist1->nodadd->next = node1;
            linklist1->nodadd->prev = node1;
            first->nodadd = node1;
            linklist1->pos = 2;
            linklist1->nodadd = node1;
            linklist1->max_pos = linklist1->max_pos+1;
        }
        else if(pos == 2 && linklist1->nodadd->next == NULL){
            struct node  *node1 = (struct node *)malloc(sizeof(struct node));
            node1->prev = linklist1->nodadd;
            node1->next = linklist1->nodadd;
            node1->val  = num;
            printf("the value in node is %d %d",node1->val,num);
            linklist1->nodadd->next = node1;
            linklist1->nodadd->prev = node1;
            linklist1->pos = 1;
            linklist1->max_pos = linklist1->max_pos +1;
            linklist1->nodadd = node1;
        }
        else{
            int traverse;
            int temp = linklist1->pos;
            //printf("\ntemp in create node causing warning is %d\n", temp);
            if(pos >= linklist1->pos){
                if(pos == linklist1->pos){
                    linklist1->nodadd = linklist1->nodadd->prev;
                    if(linklist1->pos !=1);
                        linklist1->pos = (linklist1->pos) - 1;
                }
                int traverse = pos - (linklist1->pos);
                while(traverse > 1){
                  linklist1->nodadd =linklist1->nodadd->next;
                  traverse = traverse -1;
                  linklist1->pos = (linklist1->pos)+1;
                }
                struct node  *node1 = (struct node *)malloc(sizeof(struct node));
                struct node  *temp  = linklist1->nodadd->next;
                linklist1->nodadd->next->prev = node1;//new line added test it once
                linklist1->nodadd->next = node1;
                node1->prev = linklist1->nodadd;
                node1->next = temp;
                node1->val  = num;
                if(pos == 1)
                  first->nodadd = node1;
                if(pos != temp->val)
                  linklist1->pos = (linklist1->pos)+1;
                linklist1->max_pos = linklist1->max_pos +1;
            }
   else{
                 int traverse = linklist1->pos - pos;
                 while(traverse > -1){
                     linklist1->nodadd = linklist1->nodadd->prev;
                     traverse = traverse -1;
                     if(linklist1->pos ==1)
                       linklist1->pos = linklist1->max_pos-1;
                     else
                       linklist1->pos = (linklist1->pos) - 1;
                 }
                struct node  *node1 = (struct node *)malloc(sizeof(struct node));
                struct node  *temp  = linklist1->nodadd->next;
                temp->prev = node1;
                linklist1->nodadd->next = node1;
                node1->prev = linklist1->nodadd;
                node1->next = temp;
                node1->val  = num;
                if(linklist1->pos == linklist1->max_pos -1){
                  linklist1->pos = 1;
                  first->nodadd = node1;
                }
                else{
                  linklist1->pos = (linklist1->pos)+1;
                }
                linklist1->max_pos = linklist1->max_pos +1;
           }
       }
       showlist(linklist1,first);
       createnode(linklist1,first);
       break;
    case 3 :
       exit(0);
    default :
        printf("invaid input\n");
        createnode(linklist1,first);
    }
}

void showlist(struct linklist *linklist1,struct linklist *first){
    if(first->nodadd != NULL){
    int i;
    struct linklist *temp=(struct linklist *)malloc(sizeof(struct linklist)) ;
    temp->nodadd = first->nodadd;
    printf("The list is as follows \n");
    //printf("element at pos first is%d\n",first->nodadd->val);
    //printf("temp val in showlist is %d and linkist1 val is: %d\n",temp->nodadd->val , linklist1->nodadd->val);
    for(i =1; i <=(linklist1->max_pos);i++){
        if(i < linklist1->max_pos){
            printf(" the val is :%d and position is : %d \n", temp->nodadd->val,i);
            if(temp->nodadd->next != NULL)
                temp->nodadd = temp->nodadd->next;
        }
        if(linklist1->nodadd->next!=NULL)  //after insertion of new element to make t point at same element
            linklist1->nodadd = linklist1->nodadd->next;
    }
    //printf("\nin showlist last the element being pointed is %d and pos is %d and first is %d\n",linklist1->nodadd->val , linklist1->pos,first->nodadd->val);
}
}
void main(){
    int val;
    int num;
    int i;
    struct linklist *linklist1;
    linklist1 = (struct linklist *)malloc(sizeof(struct linklist));
    struct linklist *first = (struct linklist *)malloc(sizeof(struct linklist));
    linklist1->pos = 0;
    linklist1->max_pos =1;
    linklist1->nodadd = NULL;
//    printf("enter 1 to insert\n");
//    scanf("%d",&val);
    printf("%d\n",linklist1->pos);
            //showlist(linklist1);
            createnode(linklist1,first);
/*          case 2:
            showlist();
            printf("enter the num pos to be deleated");
            scanf("%d",&pos);
            deletenode(pos);
        */
}