Static Queue: A queue is an ordered list of elements. It always works in first in first out(FIFO) fashion. All the elements get inserted at the REAR and removed from the FRONT of the queue. In implementation of the static Queue, an array will be used so all operation of queue are index based which makes it faster for all operations except deletion because deletion requires shifting of all the remaining elements to the front by one position.
A Static Queue is a queue of fixed size implemented using array.
Singly Linked List: A linked list is also an ordered list of elements. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list. Each node in the list stores the content and a pointer or reference to the next node in the list. To store a single linked list, only the reference or pointer to the first node in that list must be stored. The last node in a single linked list points to nothing (or null).
Here are some of the major differences between a Static Queue and a Singly Linked List
Static Queue | Singly Linked List |
---|---|
Queue is a collection of one or more elements arranged in memory in a contiguous fashion. |
A linked list is a collection of one or more elements arranged in memory in a dis-contiguous fashion. |
Static Queue is always fixed size. |
List size is never fixed. |
In Queue, only one and single type of information is stored because static Queue implementation is through Array. |
List also stored the address for the next node along with it’s content. |
Static Queue is index based. |
Singly linked list is reference based. |
Insertion can always be performed on a single end called REAR and deletion on the other end called FRONT. |
Insertion as well as deletion can performed any where within the list. |
Queue is always based on FIFO. |
List may be based on FIFI or LIFO etc. |
Queue have two pointer FRONT and REAR. |
While List has only one pointer basically called HEAD. |
Below is the implementation of a Static Queue:
#include <iostream> using namespace std;
class Queue {
private :
static int front, rear, capacity;
static int * queue;
public :
Queue( int c) {
front = rear = 0;
capacity = c;
queue = new int [capacity];
}
// function to insert an element
// at the rear of the queue
static void queueEnqueue( int data) {
// check queue is full or not
if (capacity == rear) {
cout << "\nQueue is full\n" ;
return ;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return ;
}
// function to delete an element
// from the front of the queue
static void queueDequeue() {
// if queue is empty
if (front == rear) {
cout << "\nQueue is empty\n" ;
return ;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for ( int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return ;
}
// print queue elements
static void queueDisplay() {
int i;
if (front == rear) {
cout << "\nQueue is Empty\n" ;
return ;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
cout << " " << queue[i] << " <-- " ;
}
return ;
}
// print front of queue
static void queueFront() {
if (front == rear) {
cout << "\nQueue is Empty\n" ;
return ;
}
cout << "\nFront Element is: " << queue[front];
return ;
}
}; int Queue::front;
int Queue::rear;
int Queue::capacity;
int * Queue::queue;
int main() {
// Create a queue of capacity 4
Queue q(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
cout << "\n\nafter two node deletion\n\n" ;
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
return 0;
} |
// Java program to implement a queue using an array class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue( int c)
{
front = rear = 0 ;
capacity = c;
queue = new int [capacity];
}
// function to insert an element
// at the rear of the queue
static void queueEnqueue( int data)
{
// check queue is full or not
if (capacity == rear) {
System.out.printf( "\nQueue is full\n" );
return ;
}
// insert element at the rear
else {
queue[rear] = data;
rear++;
}
return ;
}
// function to delete an element
// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf( "\nQueue is empty\n" );
return ;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for ( int i = 0 ; i < rear - 1 ; i++) {
queue[i] = queue[i + 1 ];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0 ;
// decrement rear
rear--;
}
return ;
}
// print queue elements
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf( "\nQueue is Empty\n" );
return ;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf( " %d <-- " , queue[i]);
}
return ;
}
// print front of queue
static void queueFront()
{
if (front == rear) {
System.out.printf( "\nQueue is Empty\n" );
return ;
}
System.out.printf( "\nFront Element is: %d" , queue[front]);
return ;
}
} public class StaticQueueinjava {
// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue( 4 );
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue( 20 );
q.queueEnqueue( 30 );
q.queueEnqueue( 40 );
q.queueEnqueue( 50 );
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue( 60 );
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf( "\n\nafter two node deletion\n\n" );
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
} |
// C# program to implement a queue using an array using System;
public class Queue
{ private static int front, rear, capacity;
private static int []queue;
public Queue( int c)
{
front = rear = 0;
capacity = c;
queue = new int [capacity];
}
// function to insert an element
// at the rear of the queue
public void queueEnqueue( int data)
{
// check queue is full or not
if (capacity == rear)
{
Console.Write( "\nQueue is full\n" );
return ;
}
// insert element at the rear
else
{
queue[rear] = data;
rear++;
}
return ;
}
// function to delete an element
// from the front of the queue
public void queueDequeue()
{
// if queue is empty
if (front == rear)
{
Console.Write( "\nQueue is empty\n" );
return ;
}
// shift all the elements from index 2 till rear
// to the right by one
else
{
for ( int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return ;
}
// print queue elements
public void queueDisplay()
{
int i;
if (front == rear)
{
Console.Write( "\nQueue is Empty\n" );
return ;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++)
{
Console.Write( " {0} <-- " , queue[i]);
}
return ;
}
// print front of queue
public void queueFront()
{
if (front == rear)
{
Console.Write( "\nQueue is Empty\n" );
return ;
}
Console.Write( "\nFront Element is: {0}" , queue[front]);
return ;
}
} public class StaticQueueinjava
{ // Driver code
public static void Main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
// print Queue elements
q.queueDisplay();
// insert element in the queue
q.queueEnqueue(60);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
Console.Write( "\n\nafter two node deletion\n\n" );
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
} /* This code contributed by PrinciRaj1992 */ |
//javascript code addition class queue { constructor(c) {
this .front = this .rear = 0;
this .capacity = c;
this .q = new Array( this .capacity);
}
// function to insert an element
// at the rear of the queue
queueEnqueue(data) {
// check queue is full or not
if ( this .capacity == this .rear) {
process.stdout.write( "\nQueue is full\n" );
return ;
}
// insert element at the rear
else {
this .q[ this .rear] = data;
this .rear++;
}
return ;
}
// function to delete an element
// from the front of the queue
queueDequeue() {
// if queue is empty
if ( this .front == this .rear) {
process.stdout.write( "\nQueue is empty\n" );
return ;
}
// shift all the elements from index 2 till rear
// to the right by one
else {
for (let i = 0; i < this .rear - 1; i++) {
this .q[i] = this .q[i + 1];
}
// store 0 at rear indicating there's no element
if ( this .rear < this .capacity)
this .q[ this .rear] = 0;
// decrement rear
this .rear--;
}
return ;
}
// print queue elements
queueDisplay() {
let i = 0;
if ( this .front == this .rear) {
process.stdout.write( "\nQueue is Empty\n" );
return ;
}
// traverse front to rear and print elements
for (i = this .front; i < this .rear; i++) {
process.stdout.write( " " + this .q[i] + " <-- " );
}
return ;
}
// print front of queue
queueFront() {
if ( this .front == this .rear) {
process.stdout.write( "\nQueue is Empty\n" );
return ;
}
process.stdout.write( "\nFront Element is: " + this .q[ this .front]);
return ;
}
}; // int Queue::front; // int Queue::rear; // int Queue::capacity; // int* Queue::queue; // Create a queue of capacity 4 let q = new queue(4);
// print Queue elements q.queueDisplay(); // inserting elements in the queue q.queueEnqueue(20); q.queueEnqueue(30); q.queueEnqueue(40); q.queueEnqueue(50); // print Queue elements q.queueDisplay(); // insert element in the queue q.queueEnqueue(60); // print Queue elements q.queueDisplay(); q.queueDequeue(); q.queueDequeue(); process.stdout.write( "\n\nafter two node deletion\n\n" );
// print Queue elements q.queueDisplay(); // print front of the queue q.queueFront(); // The code is contributed by Arushi Jindal. |
# Python program for the above approach class Queue:
def __init__( self , c):
self .front = self .rear = 0
self .capacity = c
self .q = [ 0 ] * self .capacity
# function to insert an element
# at the rear of the queue
def queueEnqueue( self , data):
# check queue is full or not
if self .capacity = = self .rear:
print ( "\nQueue is full" )
return
# insert element at the rear
else :
self .q[ self .rear] = data
self .rear + = 1
return
# function to delete an element
# from the front of the queue
def queueDequeue( self ):
# if queue is empty
if self .front = = self .rear:
print ( "\nQueue is empty\n" )
return
# shift all the elements from index 2 till rear
# to the right by one
else :
for i in range ( self .rear - 1 ):
self .q[i] = self .q[i + 1 ]
# store 0 at rear indicating there's no element
if self .rear < self .capacity:
self .q[ self .rear] = 0
# decrement rear
self .rear - = 1
return
# print queue elements
def queueDisplay( self ):
i = 0
if self .front = = self .rear:
print ( "\nQueue is Empty" )
return
# traverse front to rear and print elements
for i in range ( self .front, self .rear):
print (f " {self.q[i]} <-- " , end = "")
return
# print front of queue
def queueFront( self ):
if self .front = = self .rear:
print ( "\nQueue is Empty\n" )
return
print ( "\nFront Element is:" , self .q[ self .front])
return
# Create a queue of capacity 4 q = Queue( 4 )
# print Queue elements q.queueDisplay() # inserting elements in the queue q.queueEnqueue( 20 )
q.queueEnqueue( 30 )
q.queueEnqueue( 40 )
q.queueEnqueue( 50 )
# Print the queue elements q.queueDisplay() # Insert the element in queue q.queueEnqueue( 60 )
# Print the queue elements q.queueDisplay() q.queueDequeue() q.queueDequeue() print ( "\n\nafter two node deletion\n\n" )
# Print queue elements q.queueDisplay() # Print front of the queue q.queueFront() # This code is contributed by princekumaras |
Queue is Empty 20 <-- 30 <-- 40 <-- 50 <-- Queue is full 20 <-- 30 <-- 40 <-- 50 <-- after two node deletion 40 <-- 50 <-- Front Element is: 40
Below is the implementation of a Singly Linked List:
#include <iostream> class SinglyLList {
private :
class Node {
public :
int data;
Node* next;
Node( int data)
{
this ->data = data;
this ->next = nullptr;
}
};
Node* head;
public :
SinglyLList() { head = nullptr; }
void InsertAtStart( int data)
{
Node* new_node = new Node(data);
new_node->next = head;
head = new_node;
}
void InsertAtLast( int data)
{
Node* new_node = new Node(data);
if (head == nullptr) {
head = new_node;
return ;
}
new_node->next = nullptr;
Node* last = head;
while (last->next != nullptr) {
last = last->next;
}
last->next = new_node;
}
void DeleteAtStart()
{
if (head == nullptr) {
std::cout << "List is empty" << std::endl;
return ;
}
head = head->next;
}
void DeleteAtPos( int pos)
{
int position = 0;
if (pos > Count() || pos < 0) {
throw "Incorrect position exception" ;
}
Node* temp = head;
while (position != pos - 1) {
temp = temp->next;
position++;
}
temp->next = temp->next->next;
}
void DeleteAtLast()
{
Node* delete_node = head;
while (delete_node->next != nullptr
&& delete_node->next->next != nullptr) {
delete_node = delete_node->next;
}
delete_node->next = nullptr;
}
void Display()
{
Node* disp = head;
while (disp != nullptr) {
std::cout << disp->data << "->" ;
disp = disp->next;
}
}
int Count()
{
int elements = 0;
Node* count = head;
while (count != nullptr) {
count = count->next;
elements++;
}
return elements;
}
}; int main()
{ SinglyLList list;
// insert elements of singly linked list
// at beginning
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
// print linked list elements
list.Display();
std::cout << std::endl;
// insert element at the end of list
list.InsertAtLast(1);
std::cout << "after inserting node at the end\n" ;
// print linked list elements
list.Display();
std::cout << std::endl;
// delete an element at the given position
list.DeleteAtPos(1);
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
std::cout
<< "after deleting node: second, first and last\n" ;
// print linked list elements
list.Display();
std::cout << std::endl;
return 0;
} |
// Java program to implement singly linked list class SinglyLList {
class Node {
// node variables
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
// create reference variable of Node
Node head;
// function to insert a node
// at the beginning of the list
void InsertAtStart( int data)
{
// create a node
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
void InsertAtLast( int data)
{
Node new_node = new Node(data);
if (head == null ) {
head = new_node;
return ;
}
new_node.next = null ;
Node last = head;
while (last.next != null ) {
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
void DeleteAtStart()
{
if (head == null ) {
System.out.println( "List is empty" );
return ;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
void DeleteAtPos( int pos) throws Exception
{
int position = 0 ;
if (pos > Count() || pos < 0 ) {
throw new Exception( "Incorrect position exception" );
}
Node temp = head;
while (position != pos - 1 ) {
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null ) {
delete = delete.next;
}
delete.next = null ;
}
// function to display all the nodes of the list
void Display()
{
Node disp = head;
while (disp != null ) {
System.out.print(disp.data + "->" );
disp = disp.next;
}
}
// function to return the total nodes in the list
int Count()
{
int elements = 0 ;
Node count = head;
while (count != null ) {
count = count.next;
elements++;
}
return elements;
}
} public class GFG {
// Driver code
public static void main(String[] args) throws Exception
{
// create object of class singlyList
SinglyLList list = new SinglyLList();
// insert elements of singly linked list
// at beginning
list.InsertAtStart( 3 );
list.InsertAtStart( 2 );
list.InsertAtStart( 1 );
// print linked list elements
list.Display();
// insert element at the end of list
list.InsertAtLast( 1 );
System.out.println( "\nafter inserting node at the end\n " );
// print linked list elements
list.Display();
// delete an element at the given position
list.DeleteAtPos( 1 );
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
System.out.println( "\nafter deleting node: second, first and last\n " );
// print linked list elements
list.Display();
}
} |
class SinglyLList:
class Node:
def __init__( self , data):
self .data = data
self . next = None
def __init__( self ):
self .head = None
def InsertAtStart( self , data):
new_node = self .Node(data)
new_node. next = self .head
self .head = new_node
def InsertAtLast( self , data):
new_node = self .Node(data)
if self .head is None :
self .head = new_node
return
last = self .head
while last. next is not None :
last = last. next
last. next = new_node
def DeleteAtStart( self ):
if self .head is None :
print ( "List is empty" )
return
self .head = self .head. next
def DeleteAtPos( self , pos):
position = 0
if pos > self .Count() or pos < 0 :
raise Exception( "Incorrect position exception" )
temp = self .head
while position ! = pos - 1 :
temp = temp. next
position + = 1
temp. next = temp. next . next
def DeleteAtLast( self ):
delete_node = self .head
while delete_node. next is not None and delete_node. next . next is not None :
delete_node = delete_node. next
delete_node. next = None
def Display( self ):
disp = self .head
while disp is not None :
print (disp.data, "->" , end = " " )
disp = disp. next
def Count( self ):
elements = 0
count = self .head
while count is not None :
count = count. next
elements + = 1
return elements
if __name__ = = "__main__" :
# create a singly linked list object
list = SinglyLList()
# insert elements of singly linked list at beginning
list .InsertAtStart( 3 )
list .InsertAtStart( 2 )
list .InsertAtStart( 1 )
# print linked list elements
list .Display()
print ()
# insert element at the end of list
list .InsertAtLast( 4 )
print ( "after inserting node at the end" )
# print linked list elements
list .Display()
print ()
# delete an element at the given position
list .DeleteAtPos( 1 )
# delete starting element
list .DeleteAtStart()
# delete last element
list .DeleteAtLast()
print ( "after deleting node: second, first and last" )
# print linked list elements
list .Display()
print ()
|
// C# program to implement singly linked list using System;
public class SinglyLList
{ public class Node
{
// node variables
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
// create reference variable of Node
public Node head;
// function to insert a node
// at the beginning of the list
public void InsertAtStart( int data)
{
// create a node
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
public void InsertAtLast( int data)
{
Node new_node = new Node(data);
if (head == null )
{
head = new_node;
return ;
}
new_node.next = null ;
Node last = head;
while (last.next != null )
{
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
public void DeleteAtStart()
{
if (head == null )
{
Console.WriteLine( "List is empty" );
return ;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
public void DeleteAtPos( int pos)
{
int position = 0;
if (pos > Count() || pos < 0)
{
throw new Exception( "Incorrect position exception" );
}
Node temp = head;
while (position != pos - 1)
{
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
public void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null )
{
delete = delete.next;
}
delete.next = null ;
}
// function to display all the nodes of the list
public void Display()
{
Node disp = head;
while (disp != null )
{
Console.Write(disp.data + "->" );
disp = disp.next;
}
}
// function to return the total nodes in the list
public int Count()
{
int elements = 0;
Node count = head;
while (count != null )
{
count = count.next;
elements++;
}
return elements;
}
} class GFG
{ // Driver code
public static void Main(String[] args)
{
// create object of class singlyList
SinglyLList list = new SinglyLList();
// insert elements of singly linked list
// at beginning
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
// print linked list elements
list.Display();
// insert element at the end of list
list.InsertAtLast(1);
Console.WriteLine( "\nafter inserting node at the end\n " );
// print linked list elements
list.Display();
// delete an element at the given position
list.DeleteAtPos(1);
// delete starting element
list.DeleteAtStart();
// delete last element
list.DeleteAtLast();
Console.WriteLine( "\nafter deleting node: second, first and last\n " );
// print linked list elements
list.Display();
}
} // This code has been contributed by 29AjayKumar |
<script> // JavaScript program to implement // singly linked list class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
// create reference variable of Node
var head;
// function to insert a node
// at the beginning of the list
function InsertAtStart(data) {
// create a node
var new_node = new Node(data);
new_node.next = head;
head = new_node;
}
// function to insert node
// at the end of the list
function InsertAtLast(data) {
var new_node = new Node(data);
if (head == null ) {
head = new_node;
return ;
}
new_node.next = null ;
var last = head;
while (last.next != null ) {
last = last.next;
}
last.next = new_node;
}
// function to delete a node
// at the beginning of the list
function DeleteAtStart() {
if (head == null ) {
document.write( "List is empty" );
return ;
}
head = head.next;
}
// function to delete a node at
// a given position in the list
function DeleteAtPos(pos) {
var position = 0;
if (pos > Count() || pos < 0) {
}
var temp = head;
while (position != pos - 1) {
temp = temp.next;
position++;
}
temp.next = temp.next.next;
}
// function to delete a node
// from the end of the list
function DeleteAtLast() {
var deletenode = head;
while (deletenode.next != null &&
deletenode.next.next != null ) {
deletenode = deletenode.next;
}
deletenode.next = null ;
}
// function to display all the nodes of the list
function Display() {
var disp = head;
while (disp != null ) {
document.write(disp.data + "->" );
disp = disp.next;
}
}
// function to return the total nodes in the list
function Count() {
var elements = 0;
var count = head;
while (count != null ) {
count = count.next;
elements++;
}
return elements;
}
// Driver code
// create object of class singlyList
// insert elements of singly linked list
// at beginning
InsertAtStart(3);
InsertAtStart(2);
InsertAtStart(1);
// print linked list elements
Display();
// insert element at the end of list
InsertAtLast(1);
document.write(
"<br/>after inserting node at the end<br/><br/> "
);
// print linked list elements
Display();
// delete an element at the given position
DeleteAtPos(1);
// delete starting element
DeleteAtStart();
// delete last element
DeleteAtLast();
document.write(
"<br/>after deleting node: second, first and last<br/>"
);
// print linked list elements
Display();
// This code is contributed by todaysgaurav </script> |
1->2->3-> after inserting node at the end 1->2->3->1-> after deleting node: second, first and last 3->