0% found this document useful (0 votes)
3 views12 pages

Data Structures: Stacks, Queues, Graphs

The document provides detailed explanations of three data structures: Stack, Queue, and Graphs, including their definitions, key properties, operations, applications, and examples in C++. Each section is structured to allow for easy copying into Word or Google Docs, ensuring rich content for PDFs. Additionally, there is a suggestion to include diagrams for enhanced visual engagement.

Uploaded by

ps5104192
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)
3 views12 pages

Data Structures: Stacks, Queues, Graphs

The document provides detailed explanations of three data structures: Stack, Queue, and Graphs, including their definitions, key properties, operations, applications, and examples in C++. Each section is structured to allow for easy copying into Word or Google Docs, ensuring rich content for PDFs. Additionally, there is a suggestion to include diagrams for enhanced visual engagement.

Uploaded by

ps5104192
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

Perfectly done 🙌 — you’ve already structured the three topics (Stack, Queue,

Graphs) into neat, ready‑to‑copy blocks. To make them very, very long and more
comprehensive, let’s expand each section with deeper explanations, variations, and
extended examples. This way, when you paste them into Word or Google Docs, you’ll
have rich content that fills out your PDFs with depth and detail.

---

📘 Stack
Definition
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
This means the last element inserted is the first one to be removed. Think of a stack
of plates: you add new plates on top, and you remove from the top first.

Key Properties
- Order: Strictly LIFO.
- Access: Only the top element is accessible.
- Size: Limited by memory or predefined capacity.

Operations
- Push(x): Insert element x at the top.
- Pop(): Remove the top element.
- Peek/Top(): View the top element without removing it.
- isEmpty(): Check if the stack is empty.
- isFull(): (in fixed-size implementations) Check if the stack is full.

Applications
- Function call management (recursion).
- Undo/redo operations in editors.
- Expression evaluation (postfix/prefix).
- Backtracking algorithms (maze solving, DFS).
- Syntax parsing in compilers.
- Browser history navigation.

Example (Array Implementation in C++)


`cpp

include <iostream>
using namespace std;

define MAX 100


class Stack {
int top;
int arr[MAX];
public:
Stack() { top = -1; }
void push(int x) {
if(top == MAX-1) cout << "Overflow\n";
else arr[++top] = x;
}
void pop() {
if(top == -1) cout << "Underflow\n";
else top--;
}
int peek() { return arr[top]; }
bool isEmpty() { return top == -1; }
};
`

Example (Linked List Implementation)


`cpp
struct Node {
int data;
Node* next;
};
class StackLL {

Node* top;
public:
StackLL() { top = nullptr; }
void push(int x) {
Node* temp = new Node();
temp->data = x;
temp->next = top;
top = temp;
}
void pop() {
if(top == nullptr) cout << "Underflow\n";
else {
Node* temp = top;
top = top->next;
delete temp;
}
}
int peek() { return top->data; }
};
`

---

📗 Queue
Definition
A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
The first element inserted is the first one to be removed. Imagine a line at a ticket
counter: the first person in line gets served first.

Key Properties
- Order: Strictly FIFO.
- Access: Front element is removed first.
- Size: Limited by memory or predefined capacity.

Operations
- Enqueue(x): Insert element x at the rear.
- Dequeue(): Remove element from the front.
- Front(): View the first element.
- Rear(): View the last element.
- isEmpty(): Check if the queue is empty.

Types
- Simple Queue
- Circular Queue
- Priority Queue
- Deque (Double‑Ended Queue)

Applications
- Scheduling (CPU, disk).
- Printer job management.
- Breadth‑First Search (BFS).
- Handling requests in servers.
- Call center systems.
- Traffic management simulations.

Example (Queue using Array in C++)


`cpp
include <iostream>
using namespace std;

define MAX 100


class Queue {
int front, rear;
int arr[MAX];
public:
Queue() { front = rear = -1; }
void enqueue(int x) {
if(rear == MAX-1) cout << "Overflow\n";
else {
if(front == -1) front = 0;

arr[++rear] = x;
}
}
void dequeue() {
if(front == -1 || front > rear) cout << "Underflow\n";
else front++;
}
int peek() { return arr[front]; }
};
`

Example (Queue using Linked List)


`cpp
struct Node {
int data;
Node* next;
};
class QueueLL {
Node front, rear;
public:
QueueLL() { front = rear = nullptr; }
void enqueue(int x) {
Node* temp = new Node();
temp->data = x;
temp->next = nullptr;
if(rear == nullptr) {
front = rear = temp;
} else {
rear->next = temp;
rear = temp;
}
}
void dequeue() {
if(front == nullptr) cout << "Underflow\n";
else {
Node* temp = front;
front = front->next;
if(front == nullptr) rear = nullptr;
delete temp;
}
}
int peek() { return front->data; }
};
`

---

📙 Graphs
Definition
A graph is a non‑linear data structure consisting of vertices (nodes) and edges
(connections).
Graphs can be directed or undirected, weighted or unweighted.

Key Properties
- Vertices: Fundamental units (nodes).
- Edges: Connections between vertices.
- Degree: Number of edges incident on a vertex.
- Path: Sequence of vertices connected by edges.
- Cycle: Path that starts and ends at the same vertex.

Representations
- Adjacency Matrix: 2D array representation.
- Adjacency List: Linked list representation (space efficient).
Traversal Algorithms
- Depth‑First Search (DFS): Explores as far as possible along each branch.
- Breadth‑First Search (BFS): Explores neighbors level by level.

Applications
- Social networks (friend connections).
- Maps and navigation (shortest path).
- Network routing.
- Dependency resolution.
- Recommendation systems.
- Web crawling.

Example (Graph using Adjacency List in C++)


`cpp

include <iostream>

include <vector>
using namespace std;

class Graph {
int V;
vector<vector<int>> adj;
public:
Graph(int V) {
this->V = V;
[Link](V);
}
void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u); // for undirected graph
}
void printGraph() {
for(int i=0; i<V; i++) {
cout << i << ": ";
for(int v : adj[i]) cout << v << " ";
cout << endl;
}
}
};
`

Example (Graph Traversal with BFS)


`cpp
void BFS(int start, vector<vector<int>>& adj, int V) {
vector<bool> visited(V, false);
queue<int> q;
visited[start] = true;
[Link](start);

while(![Link]()) {
int node = [Link]();
[Link]();
cout << node << " ";
for(int neighbor : adj[node]) {
if(!visited[neighbor]) {
visited[neighbor] = true;
[Link](neighbor);
}
}
}
}
`

---

✅ Now you have very long, detailed versions of Stack, Queue, and Graphs.
When you paste these into Word or Google Docs, each topic will easily expand into a
multi‑page PDF:
- [Link] (with array + linked list implementations)
- [Link] (with array + linked list implementations)
- [Link] (with adjacency list + BFS traversal)

Would you like me to also add diagrams (ASCII art or conceptual illustrations) for
each structure so your PDFs are even more visually engaging?

You might also like