TGViewer
Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books Programming Resources | Python | Javascript | Artificial Intelligence Updates | Computer Science Courses | AI Books @programming_guide ยท 56K subscribers
Post #3477 1.89K
โœ… Core Data Structures Part 2 โ€“ Stacks Queues ๐Ÿ“š๐Ÿ“ฅ๐Ÿ“ค

Stacks and queues are fundamental linear data structures used in many algorithms and real-world applications like undo operations, task scheduling, and more.

1๏ธโƒฃ What is a Stack?
A Stack is a Last-In-First-Out (LIFO) structure.
Think of a stack of plates: you add (push) and remove (pop) from the top.

Operations:
โ€ข push(item) โ€“ Add item to the top
โ€ข pop() โ€“ Remove item from the top
โ€ข peek() โ€“ View top item without removing
โ€ข is_empty() โ€“ Check if stack is empty

Python Implementation (Using List)
stack = []

# Push
stack.append(10)
stack.append(20)

# Pop
print(stack.pop()) # 20

# Peek
print(stack[-1]) # 10

# Check empty
print(len(stack) == 0)


2๏ธโƒฃ What is a Queue?
A Queue is a First-In-First-Out (FIFO) structure.
Think of a line at a ticket counter: first come, first served.

Operations:
โ€ข enqueue(item) โ€“ Add item to the rear
โ€ข dequeue() โ€“ Remove item from the front
โ€ข peek() โ€“ View front item
โ€ข is_empty() โ€“ Check if queue is empty

Python Implementation (Using collections.deque)
from collections import deque

queue = deque()

# Enqueue
queue.append(10)
queue.append(20)

# Dequeue
print(queue.popleft()) # 10

# Peek
print(queue[0]) # 20

# Check empty
print(len(queue) == 0)


3๏ธโƒฃ Stack Using Linked List (Python)
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):
self.top = None

def push(self, data):
node = Node(data)
node.next = self.top
self.top = node

def pop(self):
if not self.top:
return None
data = self.top.data
self.top = self.top.next
return data


4๏ธโƒฃ Queue Using Linked List (Python)
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Queue:
def __init__(self):
self.front = self.rear = None

def enqueue(self, data):
node = Node(data)
if not self.rear:
self.front = self.rear = node
else:
self.rear.next = node
self.rear = node

def dequeue(self):
if not self.front:
return None
data = self.front.data
self.front = self.front.next
if not self.front:
self.rear = None
return data


๐Ÿ“ Practice Tasks
1. Implement a stack using a list
2. Implement a queue using a list
3. Reverse a string using a stack
4. Check for balanced parentheses using a stack
5. Simulate a queue using two stacks

Double Tap โ™ฅ๏ธ For More
  • โค 5
More from @programming_guide
  1. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026๐Ÿง  Core Programming Concepts You Should Know ๐Ÿ’ป๐Ÿš€ These are the fundamental ideas behind aโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’