Difference between a Static Queue and a Singly Linked List
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:
C++
#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];
}
static void queueEnqueue( int data) {
if (capacity == rear) {
cout << "\nQueue is full\n" ;
return ;
}
else {
queue[rear] = data;
rear++;
}
return ;
}
static void queueDequeue() {
if (front == rear) {
cout << "\nQueue is empty\n" ;
return ;
}
else {
for ( int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return ;
}
static void queueDisplay() {
int i;
if (front == rear) {
cout << "\nQueue is Empty\n" ;
return ;
}
for (i = front; i < rear; i++) {
cout << " " << queue[i] << " <-- " ;
}
return ;
}
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() {
Queue q(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
cout << "\n\nafter two node deletion\n\n" ;
q.queueDisplay();
q.queueFront();
return 0;
}
|
Java
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue( int c)
{
front = rear = 0 ;
capacity = c;
queue = new int [capacity];
}
static void queueEnqueue( int data)
{
if (capacity == rear) {
System.out.printf( "\nQueue is full\n" );
return ;
}
else {
queue[rear] = data;
rear++;
}
return ;
}
static void queueDequeue()
{
if (front == rear) {
System.out.printf( "\nQueue is empty\n" );
return ;
}
else {
for ( int i = 0 ; i < rear - 1 ; i++) {
queue[i] = queue[i + 1 ];
}
if (rear < capacity)
queue[rear] = 0 ;
rear--;
}
return ;
}
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf( "\nQueue is Empty\n" );
return ;
}
for (i = front; i < rear; i++) {
System.out.printf( " %d <-- " , queue[i]);
}
return ;
}
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 {
public static void main(String[] args)
{
Queue q = new Queue( 4 );
q.queueDisplay();
q.queueEnqueue( 20 );
q.queueEnqueue( 30 );
q.queueEnqueue( 40 );
q.queueEnqueue( 50 );
q.queueDisplay();
q.queueEnqueue( 60 );
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf( "\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
}
}
|
C#
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];
}
public void queueEnqueue( int data)
{
if (capacity == rear)
{
Console.Write( "\nQueue is full\n" );
return ;
}
else
{
queue[rear] = data;
rear++;
}
return ;
}
public void queueDequeue()
{
if (front == rear)
{
Console.Write( "\nQueue is empty\n" );
return ;
}
else
{
for ( int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return ;
}
public void queueDisplay()
{
int i;
if (front == rear)
{
Console.Write( "\nQueue is Empty\n" );
return ;
}
for (i = front; i < rear; i++)
{
Console.Write( " {0} <-- " , queue[i]);
}
return ;
}
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
{
public static void Main(String[] args)
{
Queue q = new Queue(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
Console.Write( "\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
}
}
|
Javascript
class queue {
constructor(c) {
this .front = this .rear = 0;
this .capacity = c;
this .q = new Array( this .capacity);
}
queueEnqueue(data) {
if ( this .capacity == this .rear) {
process.stdout.write( "\nQueue is full\n" );
return ;
}
else {
this .q[ this .rear] = data;
this .rear++;
}
return ;
}
queueDequeue() {
if ( this .front == this .rear) {
process.stdout.write( "\nQueue is empty\n" );
return ;
}
else {
for (let i = 0; i < this .rear - 1; i++) {
this .q[i] = this .q[i + 1];
}
if ( this .rear < this .capacity)
this .q[ this .rear] = 0;
this .rear--;
}
return ;
}
queueDisplay() {
let i = 0;
if ( this .front == this .rear) {
process.stdout.write( "\nQueue is Empty\n" );
return ;
}
for (i = this .front; i < this .rear; i++) {
process.stdout.write( " " + this .q[i] + " <-- " );
}
return ;
}
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 ;
}
};
let q = new queue(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
process.stdout.write( "\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
|
Python3
class Queue:
def __init__( self , c):
self .front = self .rear = 0
self .capacity = c
self .q = [ 0 ] * self .capacity
def queueEnqueue( self , data):
if self .capacity = = self .rear:
print ( "\nQueue is full" )
return
else :
self .q[ self .rear] = data
self .rear + = 1
return
def queueDequeue( self ):
if self .front = = self .rear:
print ( "\nQueue is empty\n" )
return
else :
for i in range ( self .rear - 1 ):
self .q[i] = self .q[i + 1 ]
if self .rear < self .capacity:
self .q[ self .rear] = 0
self .rear - = 1
return
def queueDisplay( self ):
i = 0
if self .front = = self .rear:
print ( "\nQueue is Empty" )
return
for i in range ( self .front, self .rear):
print (f " {self.q[i]} <-- " , end = "")
return
def queueFront( self ):
if self .front = = self .rear:
print ( "\nQueue is Empty\n" )
return
print ( "\nFront Element is:" , self .q[ self .front])
return
q = Queue( 4 )
q.queueDisplay()
q.queueEnqueue( 20 )
q.queueEnqueue( 30 )
q.queueEnqueue( 40 )
q.queueEnqueue( 50 )
q.queueDisplay()
q.queueEnqueue( 60 )
q.queueDisplay()
q.queueDequeue()
q.queueDequeue()
print ( "\n\nafter two node deletion\n\n" )
q.queueDisplay()
q.queueFront()
|
Output
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:
C++
#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;
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
list.Display();
std::cout << std::endl;
list.InsertAtLast(1);
std::cout << "after inserting node at the end\n" ;
list.Display();
std::cout << std::endl;
list.DeleteAtPos(1);
list.DeleteAtStart();
list.DeleteAtLast();
std::cout
<< "after deleting node: second, first and last\n" ;
list.Display();
std::cout << std::endl;
return 0;
}
|
Java
class SinglyLList {
class Node {
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
Node head;
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 == null ) {
head = new_node;
return ;
}
new_node.next = null ;
Node last = head;
while (last.next != null ) {
last = last.next;
}
last.next = new_node;
}
void DeleteAtStart()
{
if (head == null ) {
System.out.println( "List is empty" );
return ;
}
head = head.next;
}
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;
}
void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null ) {
delete = delete.next;
}
delete.next = null ;
}
void Display()
{
Node disp = head;
while (disp != null ) {
System.out.print(disp.data + "->" );
disp = disp.next;
}
}
int Count()
{
int elements = 0 ;
Node count = head;
while (count != null ) {
count = count.next;
elements++;
}
return elements;
}
}
public class GFG {
public static void main(String[] args) throws Exception
{
SinglyLList list = new SinglyLList();
list.InsertAtStart( 3 );
list.InsertAtStart( 2 );
list.InsertAtStart( 1 );
list.Display();
list.InsertAtLast( 1 );
System.out.println( "\nafter inserting node at the end\n " );
list.Display();
list.DeleteAtPos( 1 );
list.DeleteAtStart();
list.DeleteAtLast();
System.out.println( "\nafter deleting node: second, first and last\n " );
list.Display();
}
}
|
Python3
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__" :
list = SinglyLList()
list .InsertAtStart( 3 )
list .InsertAtStart( 2 )
list .InsertAtStart( 1 )
list .Display()
print ()
list .InsertAtLast( 4 )
print ( "after inserting node at the end" )
list .Display()
print ()
list .DeleteAtPos( 1 )
list .DeleteAtStart()
list .DeleteAtLast()
print ( "after deleting node: second, first and last" )
list .Display()
print ()
|
C#
using System;
public class SinglyLList
{
public class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public Node head;
public void InsertAtStart( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
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;
}
public void DeleteAtStart()
{
if (head == null )
{
Console.WriteLine( "List is empty" );
return ;
}
head = head.next;
}
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;
}
public void DeleteAtLast()
{
Node delete = head;
while (delete.next != null
&& delete.next.next != null )
{
delete = delete.next;
}
delete.next = null ;
}
public void Display()
{
Node disp = head;
while (disp != null )
{
Console.Write(disp.data + "->" );
disp = disp.next;
}
}
public int Count()
{
int elements = 0;
Node count = head;
while (count != null )
{
count = count.next;
elements++;
}
return elements;
}
}
class GFG
{
public static void Main(String[] args)
{
SinglyLList list = new SinglyLList();
list.InsertAtStart(3);
list.InsertAtStart(2);
list.InsertAtStart(1);
list.Display();
list.InsertAtLast(1);
Console.WriteLine( "\nafter inserting node at the end\n " );
list.Display();
list.DeleteAtPos(1);
list.DeleteAtStart();
list.DeleteAtLast();
Console.WriteLine( "\nafter deleting node: second, first and last\n " );
list.Display();
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function InsertAtStart(data) {
var new_node = new Node(data);
new_node.next = head;
head = new_node;
}
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 DeleteAtStart() {
if (head == null ) {
document.write( "List is empty" );
return ;
}
head = head.next;
}
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 DeleteAtLast() {
var deletenode = head;
while (deletenode.next != null &&
deletenode.next.next != null ) {
deletenode = deletenode.next;
}
deletenode.next = null ;
}
function Display() {
var disp = head;
while (disp != null ) {
document.write(disp.data + "->" );
disp = disp.next;
}
}
function Count() {
var elements = 0;
var count = head;
while (count != null ) {
count = count.next;
elements++;
}
return elements;
}
InsertAtStart(3);
InsertAtStart(2);
InsertAtStart(1);
Display();
InsertAtLast(1);
document.write(
"<br/>after inserting node at the end<br/><br/> "
);
Display();
DeleteAtPos(1);
DeleteAtStart();
DeleteAtLast();
document.write(
"<br/>after deleting node: second, first and last<br/>"
);
Display();
</script>
|
Output
1->2->3->
after inserting node at the end
1->2->3->1->
after deleting node: second, first and last
3->
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...