Relearning basics of CS – Implementing Queue

Have you ever stood in a queue, The queue data structure also does the same thing. You stand in the end of line when you want to place an order maybe in your favorite self service restaurant and then you move up the queue and leave.

The Queue data st…


This content originally appeared on DEV Community and was authored by urulaik

Have you ever stood in a queue, The queue data structure also does the same thing. You stand in the end of line when you want to place an order maybe in your favorite self service restaurant and then you move up the queue and leave.

The Queue data structure in CS performs the same functionality. The Queue Data structure is a First In First Out data structure. The Queue Data structure can be built with two basic functions Enqueue and Dequeue which basically is adding to the list and removing from the list.

Real life examples

In the real world of computer science Queues form the backbone of the following software components

  • Task scheduling
  • Event handling
  • Async communication etc

While a simple easy to visualize component would be

  1. A queue in playlist for the songs
  2. A queue in the order of data being sent across the network etc

Implementation

package main

import (
    "fmt"
    "errors"
)

type Queue struct{
    Elements []int
    Size int
}

func (q *Queue) Enqueue(value int){
    if q.GetLength() == q.Size { 
        fmt.Println("Overflow") 
        return
    } 
    q.Elements = append(q.Elements, value) 
}

func (q *Queue) Dequeue() int {
    if q.IsEmpty() { 
        fmt.Println("UnderFlow") 
        return 0
    } 
    element := q.Elements[0] 
    if q.GetLength() == 1 { 
        q.Elements = nil 
        return element 
    } 
    q.Elements = q.Elements[1:] 
    return element
}

func (q *Queue) GetLength() int { 
    return len(q.Elements) 
} 

func (q *Queue) IsEmpty() bool { 
    return len(q.Elements) == 0
} 

func (q *Queue) Peek() (int, error) { 
    if q.IsEmpty() { 
        return 0, errors.New("empty queue") 
    } 
    return q.Elements[0], nil 
} 

Complexity

Time Complexity - O(1) for Enqueue and Dequeue
Space complexity - O(1) for Enqueue and Dequeue

References

Reference - https://www.geeksforgeeks.org/queue-in-go-language/


This content originally appeared on DEV Community and was authored by urulaik


Print Share Comment Cite Upload Translate Updates
APA

urulaik | Sciencx (2024-10-14T17:17:07+00:00) Relearning basics of CS – Implementing Queue. Retrieved from https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/

MLA
" » Relearning basics of CS – Implementing Queue." urulaik | Sciencx - Monday October 14, 2024, https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/
HARVARD
urulaik | Sciencx Monday October 14, 2024 » Relearning basics of CS – Implementing Queue., viewed ,<https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/>
VANCOUVER
urulaik | Sciencx - » Relearning basics of CS – Implementing Queue. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/
CHICAGO
" » Relearning basics of CS – Implementing Queue." urulaik | Sciencx - Accessed . https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/
IEEE
" » Relearning basics of CS – Implementing Queue." urulaik | Sciencx [Online]. Available: https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/. [Accessed: ]
rf:citation
» Relearning basics of CS – Implementing Queue | urulaik | Sciencx | https://www.scien.cx/2024/10/14/relearning-basics-of-cs-implementing-queue/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.