Implementation of Queue using Linked List in C

Queue With Linked List

We had seen how to implement simple queue using array.
If you had not seen, just go through it once Queue with Array

Now, let us see the implementation of queue using linked list.


#include <stdio.h> 
#include <conio.h>
#include 
struct node
{
 int info;
 struct node *ptr;
}*front,*rear,*temp,*front1;

void iq(int data);
void deq();
void display();
int count = 0;
void main()
{
 int no, ch, e;
 clrscr();
 printf("\n 1 - Insert\n 2 - Delete\n 3 - Exit\n 4 - Display");

 while (1)
 {
  printf("\n Enter choice : ");
  scanf("%d", &ch);
  switch (ch)
  {
   case 1:
    printf("Enter data : ");
    scanf("%d", &no);
    iq(no);
    break;
   case 2:
    deq();
    break;
   case 3:
    exit(0);
   case 4:
    display();
    break;
   default:
    printf("Wrong choice, Please enter correct choice  ");
    break;
  }
 }
}
void iq(int data)
{
 if (rear == NULL)
 {
  rear = (struct node *)malloc(1*sizeof(struct node));
  rear->ptr = NULL;
  rear->info = data;
  front = rear;
 }
 else
 {
  temp=(struct node *)malloc(1*sizeof(struct node));
  rear->ptr = temp;
  temp->info = data;
  temp->ptr = NULL;

  rear = temp;
 }
 count++;
}
void display()
{
 front1 = front;

 if ((front1 == NULL) && (rear == NULL))
 {
  printf("Queue is empty");
  return;
 }
 while (front1 != rear)
 {
  printf("%d ", front1->info);
  front1 = front1->ptr;
 }
 if (front1 == rear)
  printf("%d", front1->info);
}
void deq()
{
 front1 = front;
 if (front1 == NULL)
 {
  printf("\n Error: Trying to display elements from empty queue");
  return;
 }
 else
 if (front1->ptr != NULL)
 {
  front1 = front1->ptr;
  printf("\n Dequed value : %d", front->info);
  free(front);
  front = front1;
 }
 else
 {
  printf("\n Dequed value : %d", front->info);
  free(front);
  front = NULL;
  rear = NULL;
 }
 count--;
}





















Happy Coding :)

Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort