Geeksforgeeks-adjacency list adjacency matrix c\c++ code

Source: Internet
Author: User

Diagram of the adjacency matrix:


Constructs a non-forward adjacency matrix.

Test Site: http://www.geeksforgeeks.org/graph-and-its-representations/

Here you write a class that adds an action to delete a diagram.

#pragma once#include <stdio.h> #include <stdlib.h>class adjlistgraph{struct node{int dest; Node *next;}; struct List{node *first;}; struct graph{int vers; List *verarr;}; Node *createnode (int dest) {node *newnode = (node *) malloc (sizeof (node)); newnode->dest = Dest;newnode->next = NULLP Tr;return NewNode;} Graph *creategraph (int vers) {graph * gra = (graph *) malloc (sizeof (graph)); gra->vers = Vers;gra->verarr = (List *) m Alloc (vers * sizeof (List)); for (int i = 0; i < vers; i++) {gra->verarr[i].first = nullptr;} return GRA;} void Addedge (Graph *gra, int src, int dest) {Node *n = CreateNode (dest); n->next = gra->verarr[src].first;//There is no need for-&GT Next, because the null head pointer Gra->verarr[src].first = n;//constructs a non-direction graph n = createnode (src); n->next = gra->verarr[dest].first; Gra->verarr[dest].first = n;} void Printgraph () {for (int i = 0; i < graph->vers; i++) {Node *n = graph->verarr[i].first;printf ("\ n adjacency Lis T of Vertex%d\n head ", I); while (n) {printf ("-%d ", N->deST); n = n->next;} Putchar (' \ n ');}} Graph *graph;public:adjlistgraph (int V = 0): Graph (nullptr) {graph = Creategraph (V); Addedge (graph, 0, 1); Addedge (graph, 0 , 4); Addedge (graph, 1, 2); Addedge (graph, 1, 3); Addedge (graph, 1, 4); Addedge (graph, 2, 3); Addedge (graph, 3, 4);p rintgraph ( );} ~adjlistgraph () {if (graph) {for (int i = 0; i < graph->vers; i++) {Node *n = graph->verarr[i].first; Node *p = Nullptr;while (n) {p = n;n = N->next;free (P);}} Free (Graph->verarr), free (graph);}};


The following is the C + + code, and the C + + code will be more concise.

Using the default constructor and using new will indeed be a lot easier.

malloc original fast, new convenient. Instead of setting up a CREATE function specifically, the direct new+ constructor is implemented.

#include <stdio.h> #include <stdlib.h> #include <iostream>class adjlistgraph_2{struct node{int label ; Node *next; node (int l = 0, node *n = nullptr): Label (l), next (n) {}};struct vertice{node *first; Vertice (): First (nullptr) {}};struct graph{int vers; Vertice *varr; Graph (int v = 5): vers (v) {Varr = new vertice[vers];}}; Graph *graph;public:adjlistgraph_2 (int V = 5): Graph (nullptr) {graph = new graph (V); Addedge (0, 1); Addedge (0, 4); Addedge (1 , 2); Addedge (1, 3); Addedge (1, 4); Addedge (2, 3); Addedge (3, 4);p rintgraph ();} void Addedge (int src, int dest) {Node *n = new Node (dest); n->next = Graph->varr[src].first;graph->varr[src]. First = N;n = new Node (src); n->next = Graph->varr[dest].first;graph->varr[dest].first = n;} void Printgraph () {if (graph) {for (int i = 0; i < graph->vers; i++) {Node *n = graph->varr[i].first;printf ("\ n the %d Vertice ' s adjcences is: \ n v%d ", I, I), while (n) {printf ("->v%d ", n->label); n = n->next;} Putchar (' \ n ');}}} ~adjlistgrapH_2 () {if (graph) {for (int i = 0; i < graph->vers; i++) {node *n = Graph->varr[i].first;while (n) {node *p = N;n = n ->next;delete p;}} Delete [] Graph->varr;delete graph;}};



Geeksforgeeks-adjacency list adjacency matrix c\c++ code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.