Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Graph Structured Stack
In this article, we will learn what is graph structured stack and how to implement it in a C++ program.
What is Graph Structured Stack?
Graph structured stack is a directed acyclic graph, where each directed path represents a stack. In this types of stack, the flow of execution is not linear like a traditional stack, instead the flow is represented using a graph structure. Each node in the graph can represent a function call or a state, and the edges define possible transitions or calls. This type of stacks are useful in advanced control flow handling like backtracking, parallel computation paths, and interpreters for non-linear languages. In the following diagram, there are four stacks: {7,3,1,0}, {7,4,1,0}, {7,5,2,0}, and {8,6,2,0}.
Graph Structured Stack in C++
We can implement a graph structured stack using classes and pointers in C++. Each node of the stack will be represented by a structure that holds a value and a list of child nodes, representing branches in the stack. The current state of the stack can be seen as a node with possible transitions to other nodes.
Steps to Implement Graph Structured Stack
The following steps explain the logic used to create a graph-structured stack:
- Step 1: Define a node structure with a value and a list of child nodes (next possible states).
- Step 2: Implement pushing new states onto the graph by adding children to a node.
- Step 3: Traverse the graph to explore possible stack paths.
- Step 4: Use DFS or BFS to print all paths or find specific ones.
C++ Program to Implement Graph Structured Stack
The code below shows how to create and explore a graph-structured stack in C++.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class StackNode {
public:
int data;
vector<shared_ptr<StackNode>> children;
StackNode(int val) {
data = val;
}
void addChild(shared_ptr<StackNode> child) {
children.push_back(child);
}
};
void dfs(shared_ptr<StackNode> node, vector<int>& path) {
if (!node) return;
path.push_back(node->data);
if (node->children.empty()) {
for (int val : path)
cout << val << " ";
cout << endl;
} else {
for (auto child : node->children) {
dfs(child, path);
}
}
path.pop_back();
}
int main() {
// Creating the root of the stack
auto root = make_shared<StackNode>(1);
// Adding children (branching paths)
auto node2 = make_shared<StackNode>(2);
auto node3 = make_shared<StackNode>(3);
auto node4 = make_shared<StackNode>(4);
auto node5 = make_shared<StackNode>(5);
root->addChild(node2);
root->addChild(node3);
node2->addChild(node4);
node3->addChild(node5);
// Display all paths using DFS
cout << "Graph Structured Stack Paths:" << endl;
vector<int> path;
dfs(root, path);
return 0;
}
The output of the above code will be:
Graph Structured Stack Paths: 1 2 4 1 3 5