Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication5.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- #define map_size 10
- class node
- {
- public:
- node(int n){ next = NULL; data = n; }
- int data;
- node *next;
- };
- class list
- {
- public:
- void push(int);
- list(){ head = NULL; }
- void print();
- node *head;
- private:
- };
- void list::push(int push_in)
- {
- //cout << "\ncall push\n";
- if (head == NULL)
- {
- head = new node(push_in);
- return;
- }
- node* pos = list::head;
- while (pos->next != NULL)
- {
- pos = pos->next;
- }
- pos->next = new node(push_in);
- //cout << "push end\n";
- }
- void list::print()
- {
- node *pos = head;
- while (pos != NULL)
- {
- cout << "[" << pos->data + 1 << "]";
- pos = pos->next;
- }
- }
- class graph
- {
- public:
- graph();
- list maplist[map_size];
- bool isvisit[map_size];
- void visit(int, bool *, list*);
- void convert_list();
- void print_list();
- };
- graph::graph()
- {
- cout << "\nhave to caret map :\n";
- for (int i = 0; i < map_size; i++)
- {
- cout << "V" << i + 1 << "can go :";
- string in;
- getline(cin, in);
- for (int j = 0; j < in.length(); j++)
- {
- if (isdigit(in[j]))
- {
- if (in[j + 1] == '0')
- {
- maplist[i].push(9);
- j++;
- }
- else
- maplist[i].push(int(in[j] - '0')-1);
- }
- }
- }
- for (int i = 0; i < map_size; i++)
- isvisit[i] = 0;
- cout << "creat success!\n\n";
- }
- void graph::print_list()
- {
- for (int i = 0; i < map_size; i++)
- {
- cout << "V" << i + 1 << " :";
- maplist[i].print();
- cout << endl;
- }
- }
- void graph::visit(int i, bool *isvisit, list* head)
- {
- node *ptr;
- isvisit[i] = 1;
- cout << "visit= " << i + 1 << endl;
- ptr = head[i].head;
- while (ptr != NULL)
- {
- if (isvisit[ptr->data] == 0)
- {
- visit(ptr->data, isvisit, head);
- }
- ptr = ptr->next;
- }
- }
- int main()
- {
- graph gr1;
- gr1.print_list();
- gr1.visit(0, gr1.isvisit, gr1.maplist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement