How to Create a Queue Using linked list in C program DSA

 How to Create a Queue Using the linked list  in C program DSA


#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>


struct node* r=NULL;

struct node* f=NULL;


struct node{

    int data;

    struct node* next;

};

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

void enqueue(int val){

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

    if(n==NULL){

        printf("Queue is Full!");

    }


    else{

        n->data =val;

        n->next=NULL; 

        if(f==NULL){

            f=r=n;

        }

        else{

            r->next=n;

            r=n;

        }

    }

}

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

int dequeue(){

    int val=-1;

    struct node *ptr =f;

    if(f==NULL){

        printf("Queue is Empty!\n");

    }


    else{

        f=f->next;

        val=ptr->data;

        free(ptr);   

    }

    return val;

}

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

int linkedlisttraversal(struct node* ptr){

while(ptr!=NULL)

{

printf("Element :=>%d\n",ptr->data);

ptr=ptr->next;

}

}

//**********************Main Function *********************

int main(){


    linkedlisttraversal(f);

    //printf("the Dequeue element %d\n",dequeue());

    enqueue(6);

    enqueue(4);

    enqueue(9);

    enqueue(3);

    linkedlisttraversal(f);

    printf("the Dequeue element %d\n",dequeue());

    printf("the Dequeue element %d\n",dequeue());

    linkedlisttraversal(f);

    

    

    return 0;

}

output-







Post a Comment

0 Comments