UPD: I rewrote the code and don’t understand why it crashes to for(j = gr[i].list.begin(); j != gr[i].list.end(); ++j)

The code itself http://pastebin.com/aHsGD2GK

I declare structures

 struct tlist { int inf; tlist *next; }; struct orgraph { int top; tlist *first; }; void insert(orgraph *gr, int x, int y) { tlist *cur = gr[x].first; tlist *pr = NULL; tlist* now = new tlist; now->inf = y; now->next = NULL; while(cur != NULL) { pr = cur; cur = cur->next; } if(pr == NULL) { gr[x].first = now; } else { pr->next = now; } } 

And everything seems fine, but I need to remove the arc connecting the vertices a and b. And here I am in a stupor, what to do. Understand, what

  void delete_xy(orgraph *gr, int x, int y) { tlist *tmp = gr[x].first; } 

Well, how can I erase Y?

  • If C ++, why not standard containers, but a bike from a single-linked list? (From the names of the structures, by the way, it is not clear where you have the edges of the graph.) - VladD
  • Writing a bike part of the job - Error
  • It turns out I need to create one structure and in it to store information about the vertex and a list of adjacencies in the set container. I understand everything correctly? - Error
  • one
    @Error, and you, what - a practical task, and not a training exercise? - avp
  • Yes, a practical task, the main condition is that the graph be represented as an adjacency list. - Error

2 answers 2

@Error , there may be a clearer formulation of the question:

 Как удалить элемент односвязного списка с заданным ключем? 

(The key is inf in the tlist structure)

will help you solve it yourself?

I'll tell you a bit - when deleting, you should remember the address of the previous item in the list.

Another small tip: for quick insertion at the end of the list, modify the structure

 struct orgrahp { int top; tlist *first, *last; }; 

Better yet, go to the doubly linked list.

    Ehh, hit your teacher on the head with something, for example, with Stroustrup's book ...


    But at least you describe the meaning of the tlist structure, but it’s not clear without a thoughtful parsing of the code.

    Well, std::vector there, std::set , or in extreme cases std::list . For a set of edges, it is logical to use std::set<edge*> , where edge is a structure representing an edge.