Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ में ग्राफ स्ट्रक्चर्ड स्टैक का कार्यान्वयन

इस C++ प्रोग्राम में हम ग्राफ स्ट्रक्चर्ड स्टैक को लागू करते हैं।

एल्गोरिदम

Begin
    Function graphStructuredStack(int **adjMat, int s, int bNode):
        ऐरे adjMat, स्रोत s और बॉटम नोड bNode लें
        stackFound = false इनिशियलाइज़ करें
        sVertex = 1 से noOfNodes तक
            dVertex = 1 से noOfNodes तक
                this->adjMat[sVertex][dVertex] = adjMat[sVertex][dVertex]
            Done
        Done
        स्रोत को mystack में पुश करें।
        while (!mystack.empty())
            element = mystack.top()
            गंतव्य d = 1 इनिशियलाइज़ करें
            while (d <= noOfNodes)
                if (this->adjMat[element][d] == 1)
                    गंतव्य को mystack में पुश करें
                    par[d] = element
                    this->adjMat[element][d] = 0
                    if (d == bNode)
                        stackFound = true सेट करें
                        ब्रेक
                    done
                element = d
                d = 1
                कंटिन्यू
            done
            d बढ़ाएं
            done
            if (stackFound)
                node = bNode से node != s तक
                    node को istack में पुश करें
                s को istack में पुश करें
                stackList.push_back(istack)
                stackFound = false अपडेट करें
            done
        mystack से तत्व पॉप करें
        done
        इटरेटर = stackList.begin()
        while (इटरेटर != stackList.end())
            इटरेटर बढ़ाएं
            while (!stack.empty())
                स्टैक के शीर्ष तत्व को प्रिंट करें
                स्टैक से तत्व पॉप करें
            done
        done
End.

उदाहरण कोड

#include <iostream>
#include <cstdlib>
#include <stack>
#include <list>
using namespace std;

class GraphStructuredStack {
private:
    list< stack<int> > stackList;
    stack<int> mystack;
    int noOfNodes;
    int **adjMat;
    int *par;

public:
    GraphStructuredStack(int noOfNodes) {
        this->noOfNodes = noOfNodes;
        adjMat = new int*[noOfNodes + 1];
        this->par = new int[noOfNodes + 1];
        for (int i = 0; i < noOfNodes + 1; i++)
            adjMat[i] = new int[noOfNodes + 1];
    }

    void graphStructuredStack(int **adjMat, int s, int bNode) {
        bool stackFound = false;
        for (int sVertex = 1; sVertex <= noOfNodes; sVertex++) {
            for (int dVertex = 1; dVertex <= noOfNodes; dVertex++) {
                this->adjMat[sVertex][dVertex] = adjMat[sVertex][dVertex];
            }
        }
        mystack.push(s);
        int element, d;
        while (!mystack.empty()) {
            element = mystack.top();
            d = 1;
            while (d <= noOfNodes) {
                if (this->adjMat[element][d] == 1) {
                    mystack.push(d);
                    par[d] = element;
                    this->adjMat[element][d] = 0;
                    if (d == bNode) {
                        stackFound = true;
                        break;
                    }
                    element = d;
                    d = 1;
                    continue;
                }
                d++;
            }
            if (stackFound) {
                stack<int> istack;
                for (int node = bNode; node != s; node = par[node]) {
                    istack.push(node);
                }
                istack.push(s);
                stackList.push_back(istack);
                stackFound = false;
            }
            mystack.pop();
        }
        list<stack<int> >::iterator iterator;
        iterator = stackList.begin();
        while (iterator != stackList.end()) {
            stack<int> stack = *iterator;
            iterator++;
            while (!stack.empty()) {
                cout << stack.top() << "\t";
                stack.pop();
            }
            cout << endl;
        }
    }
};

int main() {
    int noofnodes;
    cout << "Enter number of nodes: ";
    cin >> noofnodes;
    GraphStructuredStack gss(noofnodes);
    int source, bottom;
    int **adjMatrix;
    adjMatrix = new int*[noofnodes + 1];
    for (int i = 0; i < noofnodes + 1; i++)
        adjMatrix[i] = new int[noofnodes + 1];
    cout << "Enter the graph matrix: " << endl;
    for (int sVertex = 1; sVertex <= noofnodes; sVertex++) {
        for (int dVertex = 1; dVertex <= noofnodes; dVertex++) {
            cin >> adjMatrix[sVertex][dVertex];
        }
    }
    cout << "Enter the source node: ";
    cin >> source;
    cout << "Enter the bottom node: ";
    cin >> bottom;
    cout << "The stacks are: " << endl;
    gss.graphStructuredStack(adjMatrix, source, bottom);
    return 0;
}

आउटपुट

Enter number of nodes: 4
Enter the graph matrix:
1 1 1 0
0 1 1 0
1 0 0 0
1 1 1 1
Enter the source node: 3
Enter the bottom node: 1
The stacks are:
3 1
  1. - C++ प्रोग्राम: बबल सॉर्ट लागू करना

    बबल सॉर्ट तुलना-आधारित सॉर्टिंग एल्गोरिदम है। इस एल्गोरिदम में आसन्न तत्वों की तुलना की जाती है और सही क्रम बनाने के लिए स्वैप किया जाता है। यह एल्गोरिदम अन्य एल्गोरिदम की तुलना में सरल है, लेकिन इसकी कुछ कमियां भी हैं। यह एल्गोरिदम बड़ी संख्या में डेटा सेट के लिए उपयुक्त नहीं है। सॉर्टिंग कार्यों

  1. - C++ प्रोग्राम: रेडिक्स सॉर्ट लागू करना

    रेडिक्स सॉर्ट गैर-तुलनात्मक सॉर्टिंग एल्गोरिदम है। यह सॉर्टिंग एल्गोरिदम पूर्णांक कुंजियों पर काम करता है उन अंकों को समूहित करके जो समान स्थिति और मान साझा करते हैं। रेडिक्स एक संख्या प्रणाली का आधार है। जैसा कि हम जानते हैं कि दशमलव प्रणाली में रेडिक्स या आधार 10 है। इसलिए कुछ दशमलव संख्याओं को स

  1. - C++ प्रोग्राम: ऐरे का उपयोग करके स्टैक कार्यान्वयन

    ऐरे आधारित स्टैक निश्चित आकार की सरणी और top सूचकांक का उपयोग करके स्टैक कार्यान्वयन। LIFO सिद्धांत का पालन करता है। C++ प्रोग्राम उदाहरण = MAX - 1) cout << Stack Overflow << endl; else { top++; stack[top] = val; } } void pop() { if (top = 0; i--)