Priority LinkedList

 /******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

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

typedef enum {
    CHAR,
    INT,
    FLOAT,
    STRING,
    STRUCT
} Prlist_t;

struct PriorityList
{
    int priority;
    Prlist_t type;
    char size;
    void *data;
    struct PriorityList* next;
};
typedef struct PriorityList prList;



prList* prlist_insert(prList *tail,void *data, Prlist_t type, char size,int pr)
{
  if(tail == NULL) /* create head node and return it */
  {
      tail = (prList*) malloc(sizeof(prList));
      tail->data = data;
      tail->type = type;
      tail->size = size;
      tail->priority = pr;
      return tail;
  }
  else
  {
      prList* new_node = (prList*) malloc(sizeof(prList));
      new_node->data = data;
      new_node->type = type;
      new_node->size = size;
      new_node->priority = pr;
      tail->next = new_node;
      tail = new_node;
  }
  return tail;
}
prList *prlist_at(prList *head,int index)
{
    int i=0;
    prList *ptr = head;
    prList *prev = NULL;
    while (i <= index )
    {
        if(ptr->next == NULL && ptr != NULL)
          return ptr;
        if(ptr->next != NULL)
        {  prev = ptr;
           ptr = ptr->next;
        }
        else
        {
            printf("Index is not present !");
            return NULL;
        }
        i++;
    }
    return prev;
}
prList *maximum_node(prList *head, prList **prev)
{
   prList *ptr = head;
   int max = -1;
   prList *max_node = NULL;
   while(ptr != NULL)
   {
       if(max <= ptr->priority)
       {   (*prev) = max_node;
           max = ptr->priority;
           max_node = ptr;
       }
   }
   return max_node;
}
int prlist_count(prList *head)
{
   prList *ptr = head;
   int count = 0;
   if(head == NULL)
     return 0;
     
   while(ptr != NULL)
   {
       ptr = ptr->next;
       count++;
   }
    
   return count;
}
prList *prlist_rearrange(prList *head)
{
   int cnt = prlist_count(head);
   prList *ptr = head;
   prList *prev;
   prList *dup;
   prList *dup_head;
   while(cnt > 0)
   {
      prList *max= maximum_node(ptr,&prev);
      if(prev != NULL)
      {
          prev->next = max->next;
          if(dup == NULL)
          {
              dup_head = max;
              dup = max;
          }
          else
          {
              dup->next = max;
              dup = max;
          }
      }
      else
      {
          if(dup == NULL)
          {
              dup_head = max;
              dup = max;
          }
          else
          {
             ptr = max->next;
             max->next = NULL;
             dup->next = max;
             dup = dup->next;
          }
      }
      if(max != NULL)
       cnt--;
    
   }
   return dup_head;
}
void prlist_deleteAll(prList *head)
{
    prList *del_ptr = head;
    while(del_ptr != NULL)
    {
        prList *toBeDeleted = del_ptr;
        del_ptr = del_ptr->next;
        toBeDeleted->next = NULL;
        free(toBeDeleted);
    }
}


int main()
{
    int *a = (int*)malloc(sizeof(int));
    *a = 5;
    int b=4,c=3,d=6,e=9;
    prList *head = prlist_insert(NULL,a,INT,sizeof(int),5);
    prList *tail =prlist_insert(head,&b,INT,sizeof(int),2);
    prlist_insert(tail,&c,INT,sizeof(int),1);
    prlist_insert(tail,&d,INT,sizeof(int),3);
    prlist_insert(tail,&e,INT,sizeof(int),4);
    prList *z= prlist_at(head,3);
    printf("%d\n", *(int*)z->data);
    return 0;
}

Comments