tsnaik

appid_sessid

Feb 10th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<stdint.h>
  5. /**----------------------creates linked list remembering position that require changes -------------*/
  6.  
  7.  
  8. uint16_t dsess_id=1;
  9. #define MAX 500
  10.  
  11. struct snode
  12. {
  13.     uint16_t index;
  14.     struct snode *next;
  15.    
  16. }*start=NULL;
  17.  
  18.  
  19. void insert(uint16_t ind)
  20. {
  21.     struct snode *p=start,*new,*prev=NULL;
  22.     new=(struct snode *)malloc(sizeof(struct snode));
  23.  
  24.    
  25.     if(start==NULL)
  26.     {
  27.         start=new;
  28.         new->index=ind;
  29.         new->next=NULL;
  30.         return;
  31.     }
  32.     else
  33.     {
  34.         while(p!=NULL )
  35.         {
  36.             if((p->index)>ind)
  37.             break;
  38.             else
  39.             {
  40.                 prev=p;
  41.                 p=p->next;
  42.             }
  43.         }
  44.         new->next=p;
  45.         if(prev!=NULL)
  46.         prev->next=new;
  47.         else
  48.         start=new;
  49.         new->index=ind;
  50.    
  51.        
  52.     }
  53.     return;
  54.    
  55. }
  56.  
  57. void remove_front()
  58. {
  59.     struct snode *ptr=NULL;
  60.    
  61.     if(start==NULL)
  62.     {
  63.         return ;
  64.     }
  65.     else
  66.     {
  67.         ptr=start;
  68.         start=start->next;
  69.         free(ptr);
  70.         return;
  71.     }
  72. }
  73.  
  74. void remove_end()
  75. {
  76.     struct snode *ptr=start,*prev=NULL;
  77.    
  78.     if(start==NULL)
  79.     {
  80.         return;
  81.     }
  82.     else if(start->next==NULL)
  83.     {
  84.         ptr=start;
  85.         start=start->next;
  86.        free(ptr);
  87.         return;
  88.        
  89.     }
  90.     else
  91.     {
  92.         while(ptr->next!=NULL)
  93.         {
  94.             prev=ptr;
  95.             ptr=ptr->next;
  96.         }
  97.         prev->next=NULL;
  98.         free(ptr);
  99.         return;
  100.     }
  101. }
  102.  
  103. void print()
  104. {
  105.     struct snode *ptr=start;
  106.     if(start==NULL)
  107.     return;
  108.     while(ptr->next!=NULL)
  109.     {
  110.         printf("nnindex is %u ",ptr->index);
  111.         ptr=ptr->next;
  112.     }
  113.     printf("nnindex is %u ",ptr->index);
  114. }
  115.  
  116. uint16_t get_index()
  117. {
  118.     if(start==NULL)
  119.     return (-1);
  120.     return (start->index);
  121.    
  122. }
  123.  
  124. uint16_t get_last_index()
  125. {
  126.     struct snode *ptr=start;
  127.    
  128.     if(start==NULL)
  129.     return -1;
  130.    
  131.     while(ptr->next!=NULL)
  132.     ptr=ptr->next;
  133.     return ptr->index;
  134.    
  135. }
  136.  
  137. uint16_t getSessionId()
  138. {
  139.     uint16_t i;
  140.     if(start==NULL)
  141.     {
  142.         if(dsess_id>MAX)
  143.         {
  144.             dsess_id=0;
  145.         }
  146.        
  147.        
  148.         return dsess_id++;
  149.     }
  150.     else
  151.     {
  152.         i=get_index();
  153.         remove_front();
  154.         return i;
  155.    
  156.     }
  157. }
  158.  
  159.  
  160. int isNull()
  161. {
  162.     if(start==NULL)
  163.         return 1;
  164.  
  165.     return 0;
  166. }
Add Comment
Please, Sign In to add comment