stack, queue, deque

📰 Medium · Python

Learn the basics of stack, queue, and deque data structures and how to implement them in Python

beginner Published 18 Apr 2026
Action Steps
  1. Create a stack using a Python list and practice pushing and popping elements
  2. Implement a queue using a Python list and practice adding and removing elements from the front and back
  3. Use the deque data structure from the collections module to efficiently add and remove elements from both ends
Who Needs to Know This

Software engineers and data scientists can benefit from understanding these fundamental data structures to improve their coding skills and solve problems efficiently

Key Insight

💡 Stack, queue, and deque are essential data structures that can be used to solve various problems in programming, and Python provides efficient implementations for these data structures

Share This
Learn the basics of stack, queue, and deque data structures in Python! #Python #DataStructures

Key Takeaways

Learn the basics of stack, queue, and deque data structures and how to implement them in Python

Full Article

Title: stack, queue, deque

URL Source: https://medium.com/@kiancaca/stack-queue-deque-f377f48b6967?source=rss------python-5

Published Time: 2026-04-18T10:33:55Z

Markdown Content:
# stack, queue, deque - Chiahui Chen - Medium

[Sitemap](https://medium.com/sitemap/sitemap.xml)

[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40kiancaca%2Fstack-queue-deque-f377f48b6967&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)

Get app

[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)

[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40kiancaca%2Fstack-queue-deque-f377f48b6967&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

![Image 1](https://miro.medium.com/v2/resize:fill:32:32/1*dmbNkD5D-u45r44go_cf0g.png)

# stack, queue, deque

[![Image 2: Chiahui Chen](https://miro.medium.com/v2/da:true/resize:fill:32:32/0*24nFS0OxJH6QzsaL)](https://medium.com/@kiancaca?source=post_page---byline--f377f48b6967---------------------------------------)

[Chiahui Chen](https://medium.com/@kiancaca?source=post_page---byline--f377f48b6967---------------------------------------)

Follow

2 hours ago

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2Ff377f48b6967&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40kiancaca%2Fstack-queue-deque-f377f48b6967&user=Chiahui+Chen&userId=9566120b15e1&source=---header_actions--f377f48b6967---------------------clap_footer------------------)

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2Ff377f48b6967&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40kiancaca%2Fstack-queue-deque-f377f48b6967&source=---header_actions--f377f48b6967---------------------bookmark_footer------------------)

Share

三個滿類似的 資料結構,可以放在一起看,只是存資料、拿資料的順序不同,稍微記一下語法就行。

## stack

從尾端存、取。

值得注意的是 python 是用 list 實作。

stack<int> st;

st.push(3);

if (!st.empty()) {

cout << st.top() << '\n';

st.pop();

}st = []

st.append(3)

if st:

print(st.pop())

## queue

從尾端存,但從前方取出。

## Get Chiahui Chen’s stories in your inbox

Join Medium for free to get updates from this writer.

Subscribe

Subscribe

- [x]

Remember me for faster sign in



python 一樣是用 list 實作。

queue<int> q;

q.push(3);

while (!q.empty()) {

cout << q.front() << '\n';

q.pop();

}q = []

q.append(3)

if q:

print(q.popleft())

## deque

頭尾都能自由的存、取資料。

python 真正 O(1).

deque<int> deq;

deq.push_front(3);

deq.push_back(5);

cout << deq.front() << '\n';

cout << deq.back() << '\n';

deq.pop_front();

deq.pop_back();deq = deque()

deq.append(3)

deq.appendleft(5)

if deq:

print(deq.popleft())

if deq:

print(deq.pop())

[Data Structure Algorithm](https://medium.com/tag/data-structure-algorithm?source=post_page-----f377f48b6967---------------------------------------)

[Data Structures](https://medium.com/tag/data-structures?source=post_page-----f377f48b6967---------------------------------------)

[Python](https://medium.com/tag/python?source=post_page-----f377f48b6967---------------------------------------)

[Cpp](https://medium.com/tag/cpp?source=post_page-----f377f48b6967---------------------------------------)

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2Ff377f48b6967&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40kiancac
Read full article → ← Back to Reads