Data structure and algorithm (graph structure)

Assuming that the data elements of the undirected, unweighted graph are characters, the adjacency list storage structure is adopted. The implementation code for most operations such as graph creation, storage structure output, etc. has been given. Please add the implementation function codes for inserting and deleting edges respectively.
Relevant instructions:
(1) Insert edge, int Insert_Edge(g,vi,vj)
Input: graph g, two vertex elements vi, vj of the edge to be inserted;
output: return the state of insertion (success, error: edge vertex does not exist , Error: Edge repetition), according to different states will output:
Error: Vertex does not exist! or
Error: Edge repetition! or
Edge insertion succeeded!
Note: For the sake of unification, when the adjacent point is linked into the linked list, the link is in the front (table Head position)
(2) Delete edge, int Delete_Edge(g,vi,vj)
Input: graph g, to delete the two vertex elements vi, vj of the edge;
output: return the deleted state (success, error: edge vertex does not exist , Error: Edge does not exist), according to different states will output:
Error: Vertex does not exist!
Error: Edge does not exist!
Edge deletion succeeded!
(3) The control of the operation in the main function: 1—Create Figure 2—Output Graph storage structure 3—insert edge 4—delete edge 0—exit
When creating a graph, you need to enter the number of vertices, each vertex element, and each edge, see the example for details; when
outputting the storage structure, see the example for the output format;
insert Or when deleting an edge, you need to enter the two vertex elements of the edge;
For example:
1 //Create graph operation
5 //
Number of graph vertices abcde //Vertex element
** //Input edge, ** indicates the end of edge input
2 //Output graph structure operation
3 //Insert edge operation
ab // Edge vertex
3
bc
4 //Delete edge operation
de
2
0
input

1
5
abcde
**
2
3
ab
3
bc
4
de
2
0

Output

Adjacency List is:
a:
b:
c:
d:
e:
Edge insertion succeeded!
Edge insertion succeeded!
Error:Edge does not exist!
Adjacency List is:
a:-->b
b:-->c-->a
c:-->b
d:
e:

The code given is as follows: (please note the description of the function that requires supplementation)

#define _CRT_SECURE_NO_WARNINGS

#include < iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

#define Max_VertexNum 50 //Maximum number of vertices allowed

typedef char VertexType; //Define the data element (vertex) type as char

//********************************************************************************

//Adjacency table storage structure

struct EdgeNode //Define edge storage node

{int adjvex; //Storage location of adjacent points

EdgeNode *next; //Point to the next neighboring point

};

struct VertexNode //Define vertex storage node

{VertexType vertex; //Data element

struct EdgeNode *link; //The first adjacent node

};

typedef struct Graph //Define the adjacency table graph structure

{int VexNum; //The number of vertices in the graph

VertexNode Nodetable[Max_VertexNum]; //One-dimensional array-adjacency table

} Graphlnk; //Define the graph type stored in the adjacency table

//**********************************************************************************

// Implementation of various operations based on undirected and unweighted graphs stored in the adjacency table

//** Create graph

void create_graph(Graphlnk &g)

{ VertexType v1, v2;

int i, j;

struct EdgeNode *p, *q;

cin >> g.VexNum; //The number of vertices read in the graph

while (g.VexNum < 0)

cin >> g.VexNum;
for (i = 0; i <g.VexNum; i++)
{cin >> g.Nodetable[i].vertex; //input vertex element

g.Nodetable[i].link = NULL; //Adjacency table initialization

}

cin >> v1 >> v2; //Two vertices of the input edge

while (v1 != ‘’&&v2 != '’)

{ for (i = 0; i < g.VexNum; i++)

if (g.Nodetable[i].vertex == v1) break;

for (j = 0; j < g.VexNum; j++)

if (g.Nodetable[j].vertex == v2) break;

if (i >= g.VexNum || j >= g.VexNum) cin >> v1 >> v2;
//The edge vertex is incorrect, read again

else //link into the adjacent point

{ p = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));

p->adjvex = j;

p->next = g.Nodetable[i].link;

g.Nodetable[i].link = p;

q = (struct EdgeNode *)malloc(sizeof(struct EdgeNode));

q->adjvex = i;

q->next = g.Nodetable[j].link;

g.Nodetable[j].link = q;

cin >> v1 >> v2;

}

}

}

void print_graph(Graphlnk g)

{ int i;

struct EdgeNode *p;

cout << “Adjacency List is:” << endl;

for (i = 0; i < g.VexNum; i++)

{ cout << g.Nodetable[i].vertex << “:”;

p = g.Nodetable[i].link;

while (p != NULL)

{ cout << “–>” << g.Nodetable[p->adjvex].vertex;

p = p->next;

}

cout << endl;

}

}

//************************************************ **********************
Add functions for inserting and deleting edges
//****************** ************************************************** **

int main ()

{ Graphlnk g;

int ic;

VertexType vi, vj;

int k;

while (1)

{//Please enter the operation to be performed:";

cin >> ic;

while (ic < 0 || ic>4)

cin >> ic;

if (ic == 1) create_graph(g); //Create graph

if (ic == 2) print_graph(g); //output graph structure

if (ic == 3) // insert edge

{cin >> vi >> vj;

k = Insert_Edge(g, vi, vj);

if (k == -1) cout << “Error:Vertex does not exist!” << endl;

if(k==0) cout << “Error:Edge repetition!” << endl;

if(k==1) cout << “Edge insertion succeeded!” << endl;

}

if (ic == 4) // delete edge

{cin >> vi >> vj;

k = Delete_Edge (g, vi, vj);

if (k == -1) cout << “Error:Vertex does not exist!.” << endl;

if (k == 0) cout << “Error:Edge does not exist!” << endl;

if (k == 1) cout << “Edge deletion succeeded!” << endl;

}

if (ic == 0) break;

}

return 0;

}

Be sure to create a new node before using it!
Be sure to create a new node before using it!
Be sure to create a new node before using it!

int f(Graphlnk&g,char u)
{
    
    
    for(int i=0;i<g.VexNum;i++)
    {
    
    
        if(g.Nodetable[i].vertex==u)return i;
    }
    return -1;
}
int Insert_Edge(Graphlnk&g,char u,char v)
{
    
    
    if(f(g,u)==-1)return -1;//结点超出范围
    if(f(g,v)==-1)return -1;
    int f1=f(g,u),f2=f(g,v);
    EdgeNode*t=new EdgeNode;
    t=g.Nodetable[f1].link;
    while(t!=NULL)
    {
    
    
        if(t->adjvex==f2)return 0;//要插入的边已存在,不能重复插入
        t=t->next;
    }
    t=g.Nodetable[f2].link;
    while(t!=NULL)
    {
    
    
        if(t->adjvex==f1)return 0;//要插入的边已存在,不能重复插入
        t=t->next;
    }
    EdgeNode*t1=new EdgeNode;//无向图插入一对边
    t1->adjvex=f2;
    t1->next=g.Nodetable[f1].link;
    g.Nodetable[f1].link=t1;
    EdgeNode*t2=new EdgeNode;
    t2->adjvex=f1;
    t2->next=g.Nodetable[f2].link;
    g.Nodetable[f2].link=t2;
    return 1;
}
int Delete_Edge(Graphlnk&g,char u,char v)
{
    
    
    if(f(g,u)==-1)return -1;
    if(f(g,v)==-1)return -1;//结点超出范围
    int f1=f(g,u),f2=f(g,v);
    EdgeNode*t=new EdgeNode;
    t=g.Nodetable[f1].link;
    int f3=-1,f4=-1;
    while(t!=NULL)
    {
    
    
        if(t->adjvex==f2)f3=0;
        t=t->next;
    }
    if(f3==-1)return 0;//要删除的边不存在
    t=g.Nodetable[f2].link;
    while(t!=NULL)
    {
    
    
        if(t->adjvex==f1)f4=0;
        t=t->next;
    }
    if(f4==-1)return 0;//要删除的边不存在
    EdgeNode*t1=new EdgeNode;
    EdgeNode*last;
    t1=g.Nodetable[f1].link;
    EdgeNode*t2=new EdgeNode;
    t2=g.Nodetable[f2].link;
    last=t1;//设置一个指向当前结点的前一结点的指针,如果不设置的话你会发现无法删除最后一个结点(需要再遍历链表,费时间,不推荐)
    while(t1!=NULL&&t1->adjvex!=f2)
    {
    
    
        last=t1;
        t1=t1->next;
    }
    if(last==t1)//第一个结点就是要删除的边所在结点
    {
    
    
        g.Nodetable[f1].link=t1->next;
        delete t1;
    }
    else//这种情况下last指针始终指向t1指针指向的结点的前一个结点
    {
    
    
        last->next=t1->next;
        delete t1;
    }
    last=t2;//设置一个指向当前结点的前一结点的指针,如果不设置的话你会发现无法删除最后一个结点(需要再遍历链表,费时间,不推荐)
    while(t2!=NULL&&t2->adjvex!=f1)
    {
    
    
        last=t2;
        t2=t2->next;
    }
    if(last==t2)//第一个结点就是要删除的边所在结点
    {
    
    
        g.Nodetable[f2].link=t2->next;
        delete t2;
    }
    else//这种情况下last指针始终指向t2指针指向的结点的前一个结点
    {
    
    
        last->next=t2->next;
        delete t2;
    }
    return 1;
}

Guess you like

Origin blog.csdn.net/upc122/article/details/106338046