Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // matrica susedstva.h
- #pragma once
- class MatricaSusedstva {
- int ***mat;
- int n;
- public:
- MatricaSusedstva(int x);
- ~MatricaSusedstva();
- void insertEdge(int c1, int c2, int x);
- void deleteEdge(int c1, int c2);
- int** findNode(int x);
- int* findEdge(int x);
- void stampaj();
- };
- // .cpp
- #pragma once
- #include <iostream>
- #include "MatSusedstva.h"
- using namespace std;
- MatricaSusedstva::MatricaSusedstva(int x) {
- n = x;
- mat = new int**[n];
- for (int i = 0; i < n; i++)
- mat[i] = new int*[n]();
- }
- MatricaSusedstva::~MatricaSusedstva() {
- if (mat != 0) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++)
- if (mat[i][j] != 0)
- delete mat[i][j];
- delete[] mat[i];
- }
- delete[] mat;
- }
- }
- void MatricaSusedstva::insertEdge(int c1, int c2, int x) {
- if (c1 < 0 || c2 < 0 || c1 >= n || c2 >= n) {
- cout << "Nevalidna vrednost cvora!\n";
- return;
- }
- mat[c1][c2] = new int(x);
- }
- void MatricaSusedstva::deleteEdge(int c1, int c2) {
- if (c1 < 0 || c2 < 0 || c1 >= n || c2 >= n) {
- cout << "Nevalidna vrednost cvora!\n";
- return;
- }
- if (mat[c1][c2] == 0) {
- cout << "Taj poteg ne postoji!\n";
- return;
- }
- delete mat[c1][c2];
- mat[c1][c2] = 0;
- }
- int** MatricaSusedstva::findNode(int x) {
- if (x >= n || x < 0) {
- cout << "Taj cvor ne postoji!\n";
- return 0;
- }
- return mat[x];
- }
- int* MatricaSusedstva::findEdge(int x) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (*mat[i][j] == x)
- return mat[i][j];
- }
- }
- cout << "Taj poteg ne postoji!\n";
- return 0;
- }
- void MatricaSusedstva::stampaj() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (mat[i][j] != 0)
- cout << 1 << "\t";
- else
- cout << 0 << "\t";
- }
- cout << endl;
- }
- }
- // mejn
- #pragma once
- #include "MatSusedstva.h"
- #include <iostream>
- using namespace std;
- void main() {
- MatricaSusedstva mat(4);
- mat.stampaj();
- cout << "\n";
- mat.insertEdge(0, 2, -9);
- mat.insertEdge(1, 3, 10);
- mat.stampaj();
- cout << endl;
- mat.deleteEdge(0, 2);
- mat.stampaj();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement