IMPLEMENTATION OF QUEUE BY LINKEDLIST : C PROGRAM

 


Hey there ,welcome to yocoding.

Here is the code for queue by linked list in C programming language.

Program code :

#include<stdio.h>

#include <stdlib.h>


struct node

{

    int data;

    struct node *next;

};

struct node *rear=NULL,*front=NULL;


void enque(int d)

{

    struct node *p;

    p=(struct node *)malloc(sizeof(struct node));

    p->data=d;

    if(front==NULL)

    {

        p->next=NULL;

        front=p;

        rear=p;

        return;

    }

    rear->next=p;

    p->next=NULL;

    rear=p;

    return;

}


void deque()

{

    struct node *temp;

    if(front==NULL)

        printf("queue is empty.\n");

    else if(front==rear){

        printf("deleted element %d.\n",front->data);

        free(front);

        front=NULL;

    }

    else{

        printf("deleted element %d.\n",front->data);

        temp=front;

        front=front->next;

        free(temp);

    }

    return;

}


void display()

{

    struct node *temp=front;

    if(front==NULL)

    {

        printf("queue is empty.\n");

        return;

    }

    printf("queue elements are : ");

    while(temp!=NULL)

    {

        printf("%d ",temp->data);

        temp=temp->next;

    }

    printf("\n");

    return;

}


int main()

{

    int d,op,exit=0;

    do

    {

        printf("1.enque  2.deque  3.display  4.exit\n");

        printf("enter your choice : ");

        scanf("%d",&op);

        switch(op)

        {

        case 1:

            printf("enter data to be inserted(enque) : ");

            scanf("%d",&d);

            enque(d);

            break;

        case 2:

            deque();

            break;

        case 3:

            display();

            break;

        case 4:

            exit=1;

            break;

        default:

            printf("enter valid key \n");

            break;

        }

    }while(exit==0);


    return 0;

}

output:


Queue is a data structure, which we can insert from one end and delete from another end. Front and rear are the two ends of the queue. We can insert from front end and the process is called as enque, we can delete from rear end and the process is called de que. 

Happy coding. 

Comment what you need. 


Comments

Popular posts from this blog

swapping first and last digits of a number :Python Program

infix to postfix expression converter in "C" language

Implementation of Stack with Singly Linked List : C Program