CategoryTagArticle

admin

I'm a Full-stack developer

Tag

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
Difference Between Stack and Queue
Published date: 07/04/2024

In the fundamental data structure, besides the linked list, the stack and queue are also used widely in computer science and programming.


Table of content

  • What is a stack data structure?
  • What is a queue data structure?
  • The difference between stack and queue
  • How to implement in Python
  • Stack
  • Queue
  • Conclusion


What is a stack data structure?

A stack is a way to manage data and the data order based on the (LIFO) Last In, First Out principle.



What is a queue data structure?

A stack is a way to manage data and the data order based on the (FIFO) First In, First Out principle.


The difference between stack and queue

Both stack and queue are abstract data types that are used in computer science and programming.

Some key differences are below:


Operations:

  • Stack:
  • Push: Add an element to the top of the stack
  • Pop: Removes and returns the top element from the stack
  • Is Empty: Check stack is empty
  • Peek: Return the top element from the stack
  • Queue:
  • Push: Add an element to the end of the queue
  • Pop: Removes and returns the first element from the queue
  • Is Empty: Check queue is empty
  • Peek: Return the first element from the queue

Usage:

  • Stack: Used for tasks (managing function calls in a program, undoing functionality in applications, and parsing expressions)
  • Queue: Used for scenarios (managing tasks in a printer spooler, managing requests in a web server, and implementing breadth-first search algorithms)


Implementation: The queue is implemented more complex than the stack


Order of Operations:

  • Stack: LIFO - the element that was added last is the one to be removed first
  • Queue: FIFO - the element that was added first is the one to be removed first


How to implement in Python

You can clone the source code stack and queue to follow it more easily.


class Node:
  def __init__(self, data: int) -> None:
    self.data = data
    self.next = None


Stack

  • Create a class Stack
class Stack:
  def __init__(self) -> None:
    self.head = None


  • Check is empty func
def is_empty(self):
  if self.head == None:
    return True
  else:
    return False


  • Add an element
def push(self,data):
  new_node = Node(data)

  if self.head == None:
    self.head = new_node
    return

    new_node.next = self.head
    self.head = new_node


  • Remove an element
def pop(self):
  if self.is_empty():
    return None

  popped_node = self.head
  self.head = self.head.next
  popped_node.next = None

  return popped_node.data


  • Return an element
def peek(self):
  if self.is_empty():
    return None

  return self.head.data


Queue

  • Create a class Queue
class Queue:
  def __init__(self) -> None:
    self.head = None


  • Check is empty func
def is_empty(self):
  if self.head == None:
    return True
  else:
    return False


  • Add an element
def push(self,data):
  new_node = Node(data)

  if self.head == None:
    self.head = new_node
    return
    
  current = self.head

  while current.next:
    current = current.next
  current.next = new_node


  • Remove an element
def pop(self):
  if self.is_empty():
    return None

  popped_node = self.head
  self.head = self.head.next
  popped_node.next = None

  return popped_node.data


  • Return an element
def peek(self):
  if self.is_empty():
    return None

  return self.head.data


Conclusion

In summary, understanding the differences between stacks and queues is essential for choosing the appropriate data structure for specific problem-solving scenarios in computer science and programming.


Recommend

State management with redux in Flutter
admin14/06/2023

State management with redux in Flutter
In this article I will show an example to manage state in Flutter application using Redux.
Part 4: How to use Redux Toolkit in React
admin18/06/2023

Part 4: How to use Redux Toolkit in React
In this article, I will explain Redux and delve into Redux Toolkit. a collection of tools that simplify using Redux. These tools help make Redux less daunting and easier to use.
How to secure your API gateway
admin17/04/2024

How to secure your API gateway
In this blog, I will cover the 6 methods that technology leaders need to incorporate to secure and protect APIs.
Newest

Create Project with Express + TypeScript + ESLint + Auto Reload
admin12/06/2023

Create Project with Express + TypeScript + ESLint + Auto Reload
In this article, I introduce to you how to initialize an Express + TypeScript project.
TypeScript Design Pattern - Prototype
admin07/08/2023

TypeScript Design Pattern - Prototype
The prototype pattern is one of the Creational pattern groups. The responsibility is to create a new object through clone the existing object instead of using the new key. The new object is the same as the original object, and we can change its property does not impact the original object.
Writing a Data Transformation Pipeline Using Go
admin20/03/2024

Writing a Data Transformation Pipeline Using Go
In this article, I will show how to implement data processing using the Go programing language with a simple tutorial.
Đinh Thành Công Blog

My website, where I write blogs on a variety of topics and where I have some experiments with new technologies.

hotlinelinkedinskypezalofacebook
DMCA.com Protection Status
Feedback
Name
Phone number
Email
Content
Download app
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About