Danh mụcThẻBài viết

admin

I'm a Full-stack developer

Thẻ

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
Difference Between Stack and Queue
Ngày đăng: 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.


Đề xuất

Design Patterns
admin07/08/2023

Design Patterns
The design pattern does not be a specific programming language. Almost programming languages might apply design patterns that to resolve a problem repeat.
🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
admin13/04/2024

🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
In this article, I will illustrate to you how to build an RBAC in Node.js using Bitwise Operators.
TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
Mới nhất

How to integrate ChatGPT-3.5 Turbo into Node.js
admin10/01/2024

How to integrate ChatGPT-3.5 Turbo into Node.js
Step-by-Step Guide to Incorporating ChatGPT-3.5 Turbo into Node.js for Basic ReactJS Applications
Create S3 Bucket with AWS CDK
admin09/06/2023

Create S3 Bucket with AWS CDK
In this article, I introduce Amazon CDK and how to write AWS infrastructure-as-code using TypeScript. We will do it step by step.
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.
Đ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
Góp ý
Họ & Tên
Số điện thoại
Email
Nội dung
Tải ứng dụng
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About