Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<stdint.h>
- /**----------------------creates linked list remembering position that require changes -------------*/
- uint16_t dsess_id=1;
- #define MAX 500
- struct snode
- {
- uint16_t index;
- struct snode *next;
- }*start=NULL;
- void insert(uint16_t ind)
- {
- struct snode *p=start,*new,*prev=NULL;
- new=(struct snode *)malloc(sizeof(struct snode));
- if(start==NULL)
- {
- start=new;
- new->index=ind;
- new->next=NULL;
- return;
- }
- else
- {
- while(p!=NULL )
- {
- if((p->index)>ind)
- break;
- else
- {
- prev=p;
- p=p->next;
- }
- }
- new->next=p;
- if(prev!=NULL)
- prev->next=new;
- else
- start=new;
- new->index=ind;
- }
- return;
- }
- void remove_front()
- {
- struct snode *ptr=NULL;
- if(start==NULL)
- {
- return ;
- }
- else
- {
- ptr=start;
- start=start->next;
- free(ptr);
- return;
- }
- }
- void remove_end()
- {
- struct snode *ptr=start,*prev=NULL;
- if(start==NULL)
- {
- return;
- }
- else if(start->next==NULL)
- {
- ptr=start;
- start=start->next;
- free(ptr);
- return;
- }
- else
- {
- while(ptr->next!=NULL)
- {
- prev=ptr;
- ptr=ptr->next;
- }
- prev->next=NULL;
- free(ptr);
- return;
- }
- }
- void print()
- {
- struct snode *ptr=start;
- if(start==NULL)
- return;
- while(ptr->next!=NULL)
- {
- printf("nnindex is %u ",ptr->index);
- ptr=ptr->next;
- }
- printf("nnindex is %u ",ptr->index);
- }
- uint16_t get_index()
- {
- if(start==NULL)
- return (-1);
- return (start->index);
- }
- uint16_t get_last_index()
- {
- struct snode *ptr=start;
- if(start==NULL)
- return -1;
- while(ptr->next!=NULL)
- ptr=ptr->next;
- return ptr->index;
- }
- uint16_t getSessionId()
- {
- uint16_t i;
- if(start==NULL)
- {
- if(dsess_id>MAX)
- {
- dsess_id=0;
- }
- return dsess_id++;
- }
- else
- {
- i=get_index();
- remove_front();
- return i;
- }
- }
- int isNull()
- {
- if(start==NULL)
- return 1;
- return 0;
- }
Add Comment
Please, Sign In to add comment