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

RESTful API - How to design?
admin09/06/2023

RESTful API - How to design?
In this article, I will describe the useful points of designing a good API.
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.
Part 1: How to deploy Ghost Blog on AWS using Lightsail
admin17/06/2023

Part 1: How to deploy Ghost Blog on AWS using Lightsail
In this article, I want to introduce about how to deploy Ghost Blog on AWS using Lightsail.
Newest

TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
Microservice in a Monorepo
admin22/06/2023

Microservice in a Monorepo
Microservice in a Monorepo
Data structure: Doubly Linked List
admin07/04/2024

Data structure: Doubly Linked List
In this article, I would like to show you about Data structure - Doubly Linked List
Đ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