0% found this document useful (0 votes)
2 views3 pages

Stack

Stacks and Queues are essential linear data structures that organize data in LIFO and FIFO orders, respectively. Stacks allow access to the last inserted element, while Queues process items in the order they were added, both having various use cases in algorithms and system design. Java offers built-in support for these structures, including variations like Deques and Priority Queues.

Uploaded by

dogan.jonat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Stack

Stacks and Queues are essential linear data structures that organize data in LIFO and FIFO orders, respectively. Stacks allow access to the last inserted element, while Queues process items in the order they were added, both having various use cases in algorithms and system design. Java offers built-in support for these structures, including variations like Deques and Priority Queues.

Uploaded by

dogan.jonat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stacks and Queues – In-Depth Summary

Stacks and Queues are fundamental linear data structures that store and organize data in
specific orders. They are used heavily in algorithms, memory management, and real-world
systems like job scheduling and browser history tracking.

📚 Stack – LIFO (Last In, First Out)


A Stack allows access only to the last inserted element. The most recently added item is the
first to be removed—like a stack of plates.

🧠 Operations:
●​ push(item) → Add item to the top​

●​ pop() → Remove and return top item​

●​ peek() or top() → View the top item without removing it​

●​ isEmpty() → Check if the stack is empty​

🛠 Java Example:
java
CopyEdit
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.pop();

🔍 Use Cases:
●​ Function call stack​

●​ Undo/Redo systems​

●​ Parsing expressions (e.g., balanced parentheses)​

●​ Depth-First Search (DFS)​


🚶 Queue – FIFO (First In, First Out)
A Queue processes items in the order they were added. The first element added is the first to
be removed—like people in a line.

🧠 Operations:
●​ enqueue(item) → Add item at the end​

●​ dequeue() → Remove and return item from the front​

●​ peek() or front() → View front item​

●​ isEmpty() → Check if the queue is empty​

🛠 Java Example:
java
CopyEdit
Queue<Integer> queue = new LinkedList<>();
queue.add(10);
queue.remove();

🔍 Use Cases:
●​ CPU/task scheduling​

●​ Print queues​

●​ Breadth-First Search (BFS)​

●​ Buffers in networking​

🔄 Variations
Type Description
Deque Double-ended queue (can add/remove both
ends)

Priority Queue Elements dequeued by priority, not order

Circular Queue Wraps around when it reaches the end

⚖️ Time Complexity
Operation Stac Queu
k e

Insertion O(1) O(1)

Deletion O(1) O(1)

Access O(1) O(1)


(peek)

Note: Priority queues and deques may have different complexities depending on
implementation.

✅ Summary
●​ Stacks use LIFO: Last in is the first out.​

●​ Queues use FIFO: First in is the first out.​

●​ Used in algorithms, memory, processing flows, and system design.​

●​ Java provides built-in support with Stack, Queue, Deque, and PriorityQueue.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy