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

सी ++ प्रोग्राम ग्राफ़ स्ट्रक्चर्ड स्टैक को लागू करने के लिए

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

एल्गोरिदम

Begin
   Function graphStructuredStack(int **adjMat, int s,int bNode):
   Take an array adjMat, source s and bottom node bNode
   initialize stackFound = false
   for sVertex = 1 to noOfNodes
      for dVertex = 1 to noOfNodes
         this->adjMat[sVertex][dVertex] = adjMat[sVertex][dVertex]
      Done
   Done
   Push source into mystack.
   while (!mystack.empty())
      element = mystack.top()
      Initialize destination, d=1
      while (d <= noOfNodes)
         if (this->adjMat[element][d] == 1)
            Push destination into mystack
            par[d] = element
            this->adjMat[element][d] = 0
            if (d == bNode)
               Set, stackFound = true
               Break
            done
         element = d
         d = 1
         Continue
         done
      Increment d
      done
      if (stackFound)
         for node = bNode to node!=s
         push node into istack
      Push s into istack
      stackList.push_back(istack)
      update, stackFound = false
      done
   pop element from mystack
   done
   iterator = stackList.begin()
   while (iterator != stackList.end())
      increment iterator
      while (!stack.empty())
         print top element of stack
         pop element from stack
   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:
31

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

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

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

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

  1. सी ++ प्रोग्राम सरणी का उपयोग करके स्टैक को लागू करने के लिए

    स्टैक एक सार डेटा संरचना है जिसमें तत्वों का संग्रह होता है। स्टैक LIFO तंत्र को लागू करता है यानी अंत में धकेले जाने वाले तत्व को पहले पॉप आउट किया जाता है। स्टैक में कुछ सिद्धांत संचालन हैं - पुश - यह स्टैक के शीर्ष पर डेटा मान जोड़ता है। पॉप - यह स्टैक के शीर्ष पर डेटा मान को हटा देता है