Implement queue using linked list

Here given code implementation process.

//C Program
//Implement queue using linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure of queue
struct MyQueue
{
  //data
  int data;
  //pointer
  struct MyQueue*next;
};

//Pointers
struct MyQueue*head=NULL,*tail=NULL;

//insert MyQueue element
void enqueue(int value)
{
  //Create dynamic node
  struct MyQueue*node = (struct MyQueue*)malloc(
    sizeof(struct MyQueue)
  );
  if(node==NULL)
  {
    printf("Memory overflow\n");

  }else
  {
    //set new node data
    node->data=value;
    node->next=NULL;

    if(head==NULL)
    {
      //add first element into queue
      head=node;
      tail=node;

    }else
    {
      //add node at the end using tail pointer
      tail->next=node;
      tail=node;
    }
  }
}
//Delete a element into queue
void dequeue()
{
  if(head==NULL)
  {
    printf("Empty Queue\n");
    return;
  }
  //Pointer variable which are storing 
  //the address of deleted node
  struct MyQueue *temp=head;
  //Visit next node 
  head=head->next;

  if(head==NULL)
  {
    //When deleting a last node of linked list
    tail = NULL;
  }
  //before delete diaplay element
  printf("\ndequeue : %d",temp->data);
  free(temp);
  temp=NULL;
}

//Display element of Queue
void display(struct MyQueue*temp)
{

  if(temp==NULL)
  {
    printf("Empty linked list");
  }
  printf("\n");
  while(temp!=NULL)
  {
    //show node data
    printf("%d  ",temp->data);
    temp=temp->next;
  }
}
int main()
{
 
  //Add element into queue
  enqueue(1);
  enqueue(2);
  enqueue(3);
  enqueue(4);
  enqueue(5);

  //display all MyQueue
  display(head);

  dequeue();

  display(head);

  dequeue();

  display(head);
  return 0;
}

Output

1  2  3  4  5  
dequeue : 1
2  3  4  5  
dequeue : 2
3  4  5 
/*
  C++ Program
  Implement queue using linked list
*/
#include<iostream>

using namespace std;
class Node {
public:
  int data;
  Node *next;
  Node(int value) {
    this->data = value;
    this->next = NULL;
  }
};
class MyQueue {
  Node *head;
  Node *tail;
public:
  MyQueue() {
    this->head = NULL;
    this->tail = NULL;
  }
  void enqueue(int value) {
    Node *node = new Node(value);
    if (this->head == NULL) {
      this->head = node;
      this->tail = node;
    } else {
      this->tail->next = node;
      this->tail = node;
    }
  }
  void dequeue() {
    if (this->head == NULL) {
      cout << ("Empty Queue\n");
      return;
    }
    Node *temp = this->head;
    this->head = this->head->next;
    if (this->head == NULL) {
      this->tail = NULL;
    }
    cout << "\n dequeue : "<< temp->data;
    temp = NULL;
  }
  void display() {
    Node *temp = this->head;
    if (temp == NULL) {
      cout << "Empty linked list";
    }
    cout << "\n";
    while (temp != NULL) {
      cout << "  "<< temp->data;
      temp = temp->next;
    }
  }

};

int main() {
  MyQueue obj;
  obj.enqueue(1);
  obj.enqueue(2);
  obj.enqueue(3);
  obj.enqueue(4);
  obj.enqueue(5);
  obj.display();
  obj.dequeue();
  obj.display();
  obj.dequeue();
  obj.display();
  return 0;
}

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
/*
  Java Program
  Implement queue using linked list
*/
//Create class of queue
class Node
{

  public int data;

  public Node next;

  Node(int value)
  {
    this.data=value;
    this.next=null;
  }
}
public class MyQueue {

  public Node head;
  public Node tail;
  public MyQueue()
  {
    head = null;
    tail = null;

  }
 
  //insert Node element
  public void enqueue(int value)
  {
    //Create dynamic node
    Node node = new Node(value);

    if(head==null)
    {
      //Add first element into queue
      head=node;
      tail=node;

    }else
    {
      //Add node at the end using tail 
      tail.next=node;
      tail=node;
    }
    
  }
  //Delete a element into queue
  void dequeue()
  {
    if(head==null)
    {
      System.out.print("Empty Queue\n");
      return;
    }
    //Pointer variable which are storing 
    //the address of deleted node
    Node temp=head;
    //Visit next node 
    head=head.next;

    if(head==null)
    {
      //When deleting a last node of linked list
      tail = null;
    }
    //before delete diaplay element
    System.out.print("\n dequeue : "+temp.data);
    
    temp=null;
  }

  //Display element of Queue
  void display()
  {
    Node temp = this.head;
    if(temp==null)
    {
      System.out.print("Empty linked list");
    }
    System.out.print("\n");
    while(temp!=null)
    {
      //show node data
      System.out.print("  "+temp.data);
      temp=temp.next;
    }
  }
  public static void main(String[] args) {

    MyQueue obj = new MyQueue();
    //Add element into queue
    obj.enqueue(1);
    obj.enqueue(2);
    obj.enqueue(3);
    obj.enqueue(4);
    obj.enqueue(5);

    //display all MyQueue
    obj.display();

    obj.dequeue();

    obj.display();

    obj.dequeue();

    obj.display();
  }
}

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
/*
  C# Program
  Implement queue using linked list
*/
using System;
//Create class of queue
public class Node
{

  public int data;

  public Node next;

  public Node(int value)
  {
    this.data=value;
    this.next=null;
  }
}
public class MyQueue {

  public Node head;
  public Node tail;
  public MyQueue()
  {
    head = null;
    tail = null;

  }

  //insert Node element
  public void enqueue(int value)
  {
    //Create dynamic node
    Node node = new Node(value);

    if(head==null)
    {
      //Add first element into queue
      head=node;
      tail=node;

    }else
    {
      //Add node at the end using tail 
      tail.next=node;
      tail=node;
    }

  }
  //Delete a element into queue
  void dequeue()
  {
    if(head==null)
    {
      Console.Write("Empty Queue\n");
      return;
    }
    //Pointer variable which are storing 
    //the address of deleted node
    Node temp=head;
    //Visit next node 
    head=head.next;

    if(head==null)
    {
      //When deleting a last node of linked list
      tail = null;
    }
    //before delete diaplay element
    Console.Write("\n dequeue : "+temp.data);

    temp=null;
  }

  //Display element of Queue
  void display()
  {
    Node temp = this.head;
    if(temp==null)
    {
      Console.Write("Empty linked list");
    }
    Console.Write("\n");
    while(temp!=null)
    {
      //show node data
      Console.Write("  "+temp.data);
      temp=temp.next;
    }
  }
  public static void Main(String[] args) {

    MyQueue obj = new MyQueue();
    //Add element into queue
    obj.enqueue(1);
    obj.enqueue(2);
    obj.enqueue(3);
    obj.enqueue(4);
    obj.enqueue(5);

    //display all MyQueue
    obj.display();

    obj.dequeue();

    obj.display();

    obj.dequeue();

    obj.display();
  }
}

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
<?php
/*
  Php Program
  Implement queue using linked list
*/
class Node {
  public $data;
  public $next;

  function __construct($value) {
    $this->data = $value;
    $this->next = null;
  }
}
class MyQueue {
  public $head;
  public $tail;

  function __construct() {
    $this->head = null;
    $this->tail = null;
  }
  public
  function enqueue($value) {
    $node = new Node($value);
    if ($this->head == null) {
      $this->head = $node;
      $this->tail = $node;
    } else {
      $this->tail->next = $node;
      $this->tail = $node;
    }
  }

  public function dequeue() {
    if ($this->head == null) {
      echo("Empty Queue\n");
      return;
    }
    $temp = $this->head;
    $this->head = $this->head->next;
    if ($this->head == null) {
      $this->tail = null;
    }
    echo("\n dequeue : ". $temp->data);
    $temp = null;
  }

  public function display() {
    $temp = $this->head;
    if ($temp == null) {
      echo("Empty linked list");
    }
    echo ("\n");
    while ($temp != null) {
      echo ("  ". $temp->data);
      $temp = $temp->next;
    }
  }

}

function main() {
  $obj = new MyQueue();
  $obj->enqueue(1);
  $obj->enqueue(2);
  $obj->enqueue(3);
  $obj->enqueue(4);
  $obj->enqueue(5);
  $obj->display();
  $obj->dequeue();
  $obj->display();
  $obj->dequeue();
  $obj->display();
}
main();

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
#Ruby Program
#Implement queue using linked list
class Node 
  attr_reader :data, :next
  attr_accessor :data, :next
  def initialize(value) 
    self.data = value
    self.next = nil
  end
end

class MyQueue 
  attr_reader :head, :tail
  attr_accessor :head, :tail
  def initialize() 
    @head = nil
    @tail = nil
  end
  def enqueue(value) 
    node = Node.new(value)
    if (@head == nil) 
      @head = node
      @tail = node
    else 
      @tail.next = node
      @tail = node
    end
  end
  def dequeue() 
    if (@head == nil) 
      print("Empty Queue\n")
      return
    end
    temp = @head
    @head = @head.next
    if (@head == nil) 
      @tail = nil
    end
    print("\n dequeue  :", temp.data)
    temp = nil
  end
  def display() 
    temp = self.head
    if (temp == nil) 
      print("Empty linked list")
    end
    print("\n")
    while (temp != nil) 
      print("  ", temp.data)
      temp = temp.next
    end
  end
end
def main() 
  obj = MyQueue.new()
  obj.enqueue(1)
  obj.enqueue(2)
  obj.enqueue(3)
  obj.enqueue(4)
  obj.enqueue(5)
  obj.display()
  obj.dequeue()
  obj.display()
  obj.dequeue()
  obj.display()
end
main()

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
/*
  Node Js Program
  Implement queue using linked list
*/
class Node {
  
  constructor(value) {
    this.data = value;
    this.next = null;
  }
}
class MyQueue {
  
  constructor() {
    this.head = null;
    this.tail = null;
  }
  enqueue(value) {
    var node = new Node(value);
    if (this.head == null) {
      this.head = node;
      this.tail = node;
    } else {
      this.tail.next = node;
      this.tail = node;
    }
  }
  dequeue() {
    if (this.head == null) {
      process.stdout.write("Empty Queue\n");
      return;
    }
    var temp = this.head;
    this.head = this.head.next;
    if (this.head == null) {
      this.tail = null;
    }
    process.stdout.write("\n dequeue : " + temp.data);
    temp = null;
  }
  display() {
    var temp = this.head;
    if (temp == null) {
      process.stdout.write("Empty linked list");
    }
    process.stdout.write("\n");
    while (temp != null) {
      process.stdout.write("  " + temp.data);
      temp = temp.next;
    }
  }
}
function main() {
  var obj = new MyQueue();
  obj.enqueue(1);
  obj.enqueue(2);
  obj.enqueue(3);
  obj.enqueue(4);
  obj.enqueue(5);
  obj.display();
  obj.dequeue();
  obj.display();
  obj.dequeue();
  obj.display();
}

main();

Output

  1  2  3  4  5
 dequeue : 1
  2  3  4  5
 dequeue : 2
  3  4  5
/*
  Swift 4 Program
  Implement queue using linked list
*/

class Node {
  var data: Int;
  var next: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.next = nil;
  }
}

class MyQueue {
  var head: Node? ;
  var tail: Node? ;
  init() {
    self.head = nil;
    self.tail = nil;
  }
  func enqueue(_ value: Int) {
    let node: Node? = Node(value);
    if (self.head == nil) {
      self.head = node;
      self.tail = node;
    } else {
      self.tail!.next = node;
      self.tail = node;
    }
  }
  func dequeue() {
    if (self.head == nil) {
      print("Empty Queue\n");
      return;
    }
    var temp: Node? = self.head;
    self.head = self.head!.next;
    if (self.head == nil) {
      self.tail = nil;
    }
    print("\n dequeue : ", temp!.data);
    temp = nil;
  }
  func display() {
    var temp: Node? = self.head;
    if (temp == nil) {
      print("Empty linked list");
    }
    
    while (temp != nil) {
      print(temp!.data,terminator : "  ");
      temp = temp!.next;
    }
  }
}
func main() {
  let obj: MyQueue = MyQueue();
  obj.enqueue(1);
  obj.enqueue(2);
  obj.enqueue(3);
  obj.enqueue(4);
  obj.enqueue(5);
  obj.display();
  obj.dequeue();
  obj.display();
  obj.dequeue();
  obj.display();
}
main();

Output

1  2  3  4  5  
 dequeue :  1
2  3  4  5  
 dequeue :  2
3  4  5  
#   Python 3 Program
#   Implement queue using linked list

class Node :
  
  def __init__(self, value) :
    self.data = value
    self.next = None
  
# Create class of queue
class MyQueue :
  
  def __init__(self) :
    self.head = None
    self.tail = None
  
  # insert Node element
  def enqueue(self, value) :
    # Create dynamic node
    node = Node(value)
    if (self.head == None) :
      # Add first element into queue
      self.head = node
      self.tail = node
    else :
      # Add node at the end using tail 
      self.tail.next = node
      self.tail = node
    
  
  # Delete a element into queue
  def dequeue(self) :
    if (self.head == None) :
      print("Empty Queue\n", end = "")
      return
    
    # Pointer variable which are storing 
    # the address of deleted node
    temp = self.head
    # Visit next node 
    self.head = self.head.next
    if (self.head == None) :
      # When deleting a last node of linked list
      self.tail = None
    # before delete diaplay element
    print("\n dequeue : ", temp.data, end = "")
    temp = None
  
  # Display element of Queue
  def display(self) :
    temp = self.head
    if (temp == None) :
      print("Empty linked list", end = "")
    
    print("\n", end = "")
    while (temp != None) :
          # show node data
      print(" ", temp.data, end = "")
      temp = temp.next
    
  

def main() :
  obj = MyQueue()
  # Add element into queue
  obj.enqueue(1)
  obj.enqueue(2)
  obj.enqueue(3)
  obj.enqueue(4)
  obj.enqueue(5)
  # display all MyQueue
  obj.display()
  obj.dequeue()
  obj.display()
  obj.dequeue()
  obj.display()


if __name__ == "__main__":
  main()

Output

  1  2  3  4  5
 dequeue :  1
  2  3  4  5
 dequeue :  2
  3  4  5
/*
  Scala Program
  Implement queue using linked list
*/
//Create class of queue
class Node(var data: Int,
  var next: Node) {


  def this(value: Int) {
    this(value,null);
  }
}
class MyQueue(var head: Node,
  var tail: Node) {


  def this() {
    this(null,null);
  }
  //insert Node element
  def enqueue(value: Int): Unit = {
    //Create dynamic node
    var node: Node = new Node(value);

    if (head == null) {
      //Add first element into queue
      head = node;
      tail = node;
    } else {
      //Add node at the end using tail 
      tail.next = node;
      tail = node;
    }
  }
  //Delete a element into queue
  def dequeue(): Unit = {
    if (head == null) {
      print("Empty Queue\n");

      return;
    }
    //Pointer variable which are storing 
    //the address of deleted node
    var temp: Node = head;

    //Visit next node 
    head = head.next;

    if (head == null) {
      //When deleting a last node of linked list
      tail = null;
    }
    //before delete diaplay element
    print("\n dequeue : " + temp.data);
    temp = null;
  }
  //Display element of Queue
  def display(): Unit = {
    var temp: Node = this.head;

    if (temp == null) {
      print("Empty linked list");
    }
    print("\n");
    while (temp != null) {
      //show node data
      print(" " + temp.data);
      temp = temp.next;
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: MyQueue = new MyQueue();

    //Add element into queue
    obj.enqueue(1);
    obj.enqueue(2);
    obj.enqueue(3);
    obj.enqueue(4);
    obj.enqueue(5);

    //display all MyQueue
    obj.display();
    obj.dequeue();
    obj.display();
    obj.dequeue();
    obj.display();
  }
}

Output

 1 2 3 4 5
 dequeue : 1
 2 3 4 5
 dequeue : 2
 3 4 5

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved