Queue Implementation Using Linked List

Queue implementation using linked list gives following advantages:

  1. Linked list allow dynamic allocation of memory
  2. Insertion and deletion is easy 
  3. Efficient memory utilization due to run time allocation
  4. Deletion of elements free the memory, that can further utilized by other programs
Implementation of Queue
  1. First step to define the structure of the node
  2. Define two pointer called rear and front assigned with NULL
  3. Create the List of Operation in Main function
  4. Decide the condition for Queue empty or full
Last inserted elements always pointed by rear
#include
#include

struct node
{
int data;
struct  node *next;
}*front=NULL,*rear=NULL;

void Inqueue()
{
int n;
struct node *newNode=(struct node*)malloc(sizeof(struct node));
printf("Insert the data");
scanf("%d",&n);
newNode->data=n;
if(rear==NULL)
{
front=newNode;
rear=newNode;
newNode->next=NULL;
}
else
{
rear->next=newNode;
rear=newNode;
}
}

void Dequeue()
{
if(front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
front=front->next;
printf("Dequeue element is =%d",temp->data);
free(temp);
}
}

void Display()
{
if (front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}

int main()
{
int choice;
while(1)
{
printf("------Queue_l-------\n");
printf("1:Insert the element\n");
printf("2:Delete the element\n");
printf("3:Display\n");
printf("4:Exit\n");
printf("----------------------------\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
    Inqueue();
    break;
case 2:
   Dequeue();
   break;
case 3:
   Display();
   break;
case 4:
   exit(0);
   break;
default :
   printf("Invalid choice");
}
}
}


This code you can get it on below link..

https://drive.google.com/open?id=0B2O8tJ9QXc-3NG1wRlJLUVFfcGM

Comments

Popular posts from this blog

Types of Algorithm and Its Complexity Analysis

First Step of Problem Solving Using Data Structure and Algorithm

Asymptotic Notations