QUEUE IMPLEMENTATION USING LINKED LIST

    

IMPLEMENTATION OF QUEUE BY LINKED LIST



     In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear and removal of entities from the front. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.
Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.
Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists.

[Courtsey: Wikipedia.org

#include<stdlib.h>

typedef struct ll
 {
  int digit;
  struct ll *next;
 }mynode;

void addnode(mynode **hd,int data);
void delnode(mynode **hd);
void traverse(mynode *hd);
void findnode(mynode *hd,int data);

mynode *head,*front,*rear,*prev,*new1;

void main()
{
  int data,i,choice;
  clrscr();

  head=(mynode*)malloc(sizeof(mynode));
  head->next=NULL;

  while(1)
   { printf("\n\n\n\n\n\n");
     for(i=0;i<80;i++)
     printf("_");

     printf("\n\t\t\t1.ADD NODE");
     printf("\n\t\t\t2.DELETE NODE");
     printf("\n\t\t\t3.TRAVERSE LIST");
     printf("\n\t\t\t4.SEARCH NODE");
     printf("\n\t\t\t5.EXIT");
     printf("\n\n");

     for(i=0;i<80;i++)
     printf("_");

     printf("\n\t\t\tENTER CHOICE: ");
     scanf("%d",&choice);

     switch(choice)
      {
    case 1: printf("\n\t\t\tEnter Data To Add: ");
        scanf("%d",&data);

        addnode(&head,data);
        break;


    case 2: delnode(&head);
        break;


    case 3: traverse(head);
        break;


    case 4: printf("\n\t\t\tEnter Data To Search: ");
        scanf("%d",&data);

        findnode(head,data);
        break;


    case 5: printf("\n\t\t\tTHANKS FOR SPENDING TIME WITH ME \1 !!!\n ");
        printf("\t\t\tGOOD DAY!!");
        getch();


     exit(0);
     break;


       default: clrscr();
            printf("\n\t\t\tINVALID CHOICE BUDDY!! PLEASE RE-ENTER!! ");
            break;
     }//switch
   }//while
}



void addnode(mynode **hd,int data)
{
 clrscr();



 new1=(mynode*)malloc(sizeof(mynode));
 new1->next=NULL;
 new1->digit=data;

    if((*hd)->next==NULL)

     {
       rear=*hd;
       rear->next=new1;


       printf("\n\t\t\tFIRST DATA %d HAS BEEN ENTERED",data);
       rear=rear->next;//new
       return;
     }

   rear->next=new1;
   rear=new1;

   printf("\n\t\t\tDATA %d HAS BEEN ADDED",data);

}


void delnode(mynode **hd)
{ int i;
  clrscr();


 front=prev=*hd;

  if((*hd)->next==NULL)
   {
     printf("\n\t\t\tTHE LIST IS EMPTY!") ;
     return;
   }


  if((*hd)->next->next==NULL)
    {
      prev=prev->next;
      free(prev);
      front->next=NULL;
      printf("\n\t\t\t");
      for(i=0;i<35;i++)
      printf("_");
      printf("\n\t\t\tWARNING: FIRST NODE %d IS DELETED!!",prev->digit);
      printf("\n\t\t\t");
      for(i=0;i<35;i++)
      printf("_");
      return;
    }


    prev  = (*hd)->next;
    front = prev->next;

    printf("\n\t\t\tDATA %d IS TERMINATED!!",prev->digit);

    free(prev);
    (*hd)->next = front;




}


void findnode(mynode *hd,int data)
{
 clrscr();


 rear=hd;

    if(rear->next==NULL)
     { printf("\n\t\t\tNO DATA IS PRESENT IN THE LIST.");
       return;
     }


    while(rear->next!=NULL)
     {
       rear=rear->next;

       if(rear->digit==data)
    { printf("\n\t\t\tDATA %d HAS BEEN FOUND!",data);
      return;
    }

     }

   printf("\n\t\t\tDATA %d IS ABSENT!",data);

}


void traverse(mynode *hd)
{
 clrscr();

 rear=hd;

    if(rear->next==NULL)
     {
     printf("\n\t\t\tLIST IS EMPTY.");
     return;
     }

     printf("\n\t\t\tFOLLOWING DATA HAS BEEN FOUND: ");
     while(rear->next!=NULL)
      { rear=rear->next;

      printf("%d ",rear->digit);

      }
}
Edited By Sarvesh Prajapati

Comments

Popular posts from this blog

Program To Evaluate Square Root Of A Number

TIC-TAC-TOE THROUGH C

LINKED LIST IMPLEMENTATION FOR GAME BATTLESHIP THROUGH C