queue with linked list in c

/*
 * Program  : Queue using linked list
 * Language : C
 */

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};

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

void enqueue(int val)
{
    struct node *newNode = malloc(sizeof(struct node));
    newNode->data = val;
    newNode->next = NULL;

    //if it is the first node
    if(front == NULL && rear == NULL)
        //make both front and rear points to the new node
        front = rear = newNode;
    else
    {
        //add newnode in rear->next
        rear->next = newNode;

        //make the new node as the rear node
        rear = newNode;
    }
}

void dequeue()
{
    //used to free the first node after dequeue
    struct node *temp;

    if(front == NULL)
         printf("Queue is Empty. Unable to perform dequeue\n");
    else
    {
        //take backup
        temp = front;

        //make the front node points to the next node
        //logically removing the front element
        front = front->next;

        //if front == NULL, set rear = NULL
        if(front == NULL)
            rear = NULL;

       //free the first node
       free(temp);
    }

}

void printList()
{
    struct node *temp = front;

    while(temp)
    {
        printf("%d->",temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    printf("Queue :");
    printList();
    dequeue();
    printf("After dequeue the new Queue :");
    printList();
    dequeue();
    printf("After dequeue the new Queue :");
    printList();

    return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
program to implement a queue using linked list in c queue using linked list cpp queue linked list representation c++ implement queue as linked list queue implementation linked list queue using a linked list c++ queue using a linked list code a c program to implement queue adt using linked list c queue using linked list how to make a queue using linked list Queue &ndash; Linked List queue linked list program in c implement queue using linked list in c++ implement a queue using linked list queue c++ implementation linked list queue using linked list in c programming9 queue linked list c queue as linked list queue implementation by linked list queue using linked list c is queues doubly linked list are implementation in c data structure c program to perform all operations related with queue as linked list c++ program to implement queue using linked list do queues use linked list implementation of queue in linked list c queue on linked list queue linked list implementation linked list queue in c singly linked list queue in c language implement queue using linked list in C algorithm for queue using linked list in c algorithm for queue using linked list in c\\ queue implementation using linked list implementation of queue using array and linked list in c queue in c linked list queue linked list name program in Queue in C++ using Linked list queue using linked lisyt c++ queue using linked list in c++ queue linkedlist Simple C FIFO Queues linked list queue using linked lists program to implement queue using linked list Write a program to implement Queue operations using Linked List implement a queue using a linked list linked list - stack - queues - linear ds in c programming queue as linked list c queue c implementation linked list linked list using queue queue made from a linked list queue using linked list * Write a program to implement queue using linked list implement queue using linked list queue using linked list queue using linked list c++ linked list implementation of queue in c implementation of queue using linked list queue in c++ using linked list queue using linked list in c how to sort queues with linked list in c implementing a queue using linked list
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source