Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Graf.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct list {
- int vertex;
- list* next;
- };
- int Matrix(int n, int m) {
- int A[10][10], x, y, c;
- for (int i=1; i<=n; i++)
- for (int j = 1; j <=n; j++) {
- A[i][j] = 0;
- }
- for (int i = 1; i <=m; i++) {
- cout << "Ввод откуда идет ребро, куда идет, вес ребра " << endl;
- cin >> x >> y >> c;
- cout << endl;
- A[x][y] = c;
- A[y][x] = c;
- }
- for (int i = 1; i <= n; i++) {
- cout << endl;
- for (int j = 1; j <= n; j++) {
- cout << " " << A[i][j];
- }
- }
- return 0;
- }
- void inlist(int n, int m) {
- int x, y, i;
- list* HEAD[10];
- for (int i = 1; i <= n; i++) {
- HEAD[i] = new(list);
- HEAD[i]->next = 0;
- HEAD[i]->vertex = i;
- }
- for (int i = 1; i <= m; i++) {
- cout << "Введите откуда идет реберо, куда идет " << endl;
- cin >> x >> y;
- cout << endl;
- if (HEAD[x]->next == 0) {
- list* q = new(list);
- q->vertex = y;
- HEAD[x]->next = q;
- q->next = 0;
- }
- else {
- list*p = HEAD[x]->next;
- while (p->next != 0) p = p->next;
- list* q = new(list);
- q->vertex = y;
- q->next = 0;
- p->next = q;
- }
- }
- for (int i = 1; i <= n; i++) {
- cout << "Вершина " << HEAD[i]->vertex << " Ведет в ";
- list *p = new(list);
- p = HEAD[i]->next;
- while (p != 0) {
- cout << p->vertex;
- p = p->next;
- }
- if (HEAD[i]->next == 0) cout << "Пусто ";
- cout << endl;
- }
- }
- int main()
- {
- setlocale(0, "");
- //--------------------INPUT VIA ADJACENCY MATRIX--------------------------------/
- /*
- int n;
- cout << "Введите кол-во вершин в графе ";
- cin >> n;
- cout << endl;
- cout << "Введите кол-во ребер ";
- cin >> m;
- cout << endl;
- Matrix(n,m);
- */
- //------------------------------------------------------------------------------/
- //--------------------INPUT VIA ADJACENCY LIST----------------------------------/
- /*
- int n, m;
- cout << "Введите количество вершин в графе ";
- cin >> n;
- cout << endl;
- cout << "Введите количество ребер в графе ";
- cin >> m;
- cout << endl;
- inlist(n, m);
- */
- //------------------------------------------------------------------------------/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement