Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstdio>
- #include <conio.h>
- class Graph {
- private:
- int id;
- int size;
- int **zone;
- public:
- Graph(int id, int size) {
- printf("Creating graph instance... (id-%d)\n", id);
- this->size = size;
- this->id = id;
- zone = (int**)malloc(size * sizeof(int));
- if (zone == NULL) {
- fprintf(stderr, "out of memory\n");
- exit(0);
- }
- for (int i = 0; i < size; ++i) {
- zone[i] = (int*)malloc(size * sizeof(int));
- if (zone[i] == NULL) {
- fprintf(stderr, "out of memory\n");
- exit(0);
- }
- }
- // set all to 0s
- for (int i = 0; i < size; ++i)
- for (int j = 0; j < size; ++j)
- zone[i][j] = 0;
- }
- // W tym miejscu mam ERROR -Access violation reading location - tylko przy korzystaniu z listy
- // bez tego dziala normalnie i nie wiem jak to naprawic
- ~Graph() {
- printf("Deleting graph instance... (id-%d)\n", id);
- for (int i = 0; i < size; ++i) {
- free(zone[i]);
- }
- free(zone);
- }
- void addEdge(int pos_y, int pos_x, int value) {
- if (pos_y < size && pos_x < size)
- zone[pos_y][pos_x] = value;
- else
- printf("Position index out of zone size range\n");
- }
- void printMatrix() {
- for (int i = 0; i < size; ++i) {
- for (int j = 0; j < size; ++j) {
- printf("%d ", zone[j][i]);
- }
- printf("\n");
- }
- }
- int getPathLength(int pos_y, int pos_x) {
- return zone[pos_y][pos_x];
- }
- int getId() {
- return id;
- }
- // Nie wiem jak to ma dzialac wiec pominie cialo funkcji
- // zeby moc zrobic dalsze czesci zadania
- int getMaxLength(int pos_y, int pos_x) {
- return -1;
- }
- // Tak samo tutaj suma wszystkich mozliwych sciezek
- int sumAllPath() {
- return -1;
- }
- };
- typedef struct Node {
- Graph current_graph;
- Node *next_graph;
- } Node_t;
- bool isEmpty(Node_t *node) {
- return node == NULL;
- }
- Node_t *createNode_t(Graph &graph, Node_t *next_graph) {
- Node_t *node = (Node_t *)malloc(sizeof(Node_t));
- node->current_graph = graph;
- node->next_graph = next_graph;
- return node;
- }
- void addGraph(Node_t *head, Graph &graph) {
- Node_t *new_node = createNode_t(graph, NULL);
- Node_t *it = head;
- while (!isEmpty(it->next_graph)) {
- it = it->next_graph;
- }
- it->next_graph = new_node;
- }
- int findMaxLength(Node_t *head, int pos_y, int pos_x) {
- // dla kazdego graphu z tej listy wyszukaj najwieksza dlugosc miedzy
- // kazdym odcinkiem i ja zwroc
- Node_t *it = head;
- int max_length = -1;
- while (!isEmpty(it->next_graph)) {
- it = it->next_graph;
- if (it->current_graph.getMaxLength(pos_y, pos_x) > max_length) {
- max_length = it->current_graph.getMaxLength(pos_y, pos_x);
- }
- }
- return max_length;
- }
- void removeSmallest(Node_t *head) {
- Node_t *it = head;
- int smallest = INT_MAX;
- while (!isEmpty(it)) {
- if (it->current_graph.sumAllPath() < smallest) {
- smallest = it->current_graph.sumAllPath();
- }
- }
- // Tutaj powinieniem wyszukac teraz ten graf ktory mial ta najmniejsza sume
- // i usunac go, przesuwajac te elementy kolejne w miejsce wskaznika
- // na ten dany graf, ale nie wiem jak to napisac :/
- }
- void printAllGraphs(Node_t *head) {
- Node_t *it = head;
- while (!isEmpty(it)) {
- printf("%d\n", it->current_graph.getId());
- it->current_graph.printMatrix();
- printf("\n\n");
- it = it->next_graph;
- }
- printf("\n");
- }
- int main(void) {
- Graph head(0, 5);
- head.addEdge(2, 3, 20);
- head.addEdge(4, 1, 4);
- head.addEdge(2, 3, 1);
- Graph g1(1, 20);
- g1.addEdge(1, 2, 2);
- g1.addEdge(0, 2, 4);
- Graph g2(2, 3);
- g2.addEdge(1, 2, 3);
- g2.addEdge(0, 1, 40);
- Graph g3(10, 4);
- g3.addEdge(0, 3, 45);
- g3.addEdge(0, 2, 100);
- Node_t *HEAD = createNode_t(head, NULL);
- addGraph(HEAD, g1);
- addGraph(HEAD, g2);
- addGraph(HEAD, g3);
- printAllGraphs(HEAD);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement