Difference between a Static Queue and a Singly Linked List - GeeksforGeeks

27 May, 2021
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  
Below is the implementation of a Static Queue:  
Java
C#
// Java program to implement a queue using an arrayclass 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 arrayusing 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 */
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:  
Java
C#
Javascript
// Java program to implement singly linked listclass 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();    }}
// C# program to implement singly linked listusing 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->
 
29AjayKumar
princiraj1992
nidhi_biet
todaysgaurav
Arrays
Technical Scripter 2018
Linked List
Queue
Technical Scripter
Linked List
Arrays
Queue
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Circular Linked List | Set 2 (Traversal)
Swap nodes in a linked list without swapping data
Program to implement Singly Linked List in C++ using class
Circular Singly Linked List | Insertion
Given a linked list which is sorted, how will you insert in sorted way
Breadth First Search or BFS for a Graph
Level Order Binary Tree Traversal
Queue Interface In Java
Queue in Python
Queue | Set 1 (Introduction and Array Implementation)