Using the standard template STD: vector in the standard template library (STL) allows me to quickly use the adjacent linked list. Some basic usage is as follows:
# Include <iostream> # include <stdio. h ># include <vector> using namespace STD; struct edge {// defines the struct to represent an edge int nextnode; // int cost of the next node; // weight of the edge}; int main () // use vector to implement the application in the adjacent linked list {vector <edge> edge [10]; // defines an array, the element in the array is the vector for (INT I = 0; I <= 9; I ++) {// initialize edge [I]. clear ();} edge temp1; temp1.nextnode = 3; temp1.cost = 39; edge [1]. push_back (temp1); // Add the edge to edge temp2; temp2.nextnode = 4; temp2.cost = 24; edge [1] In the single-link table of Node 1. push_back (temp2); // Add the edge to edge temp3 in the single-link table of Node 1; temp3.nextnode = 5; temp3.cost = 16; edge [1]. push_back (temp3); // Add the edge to edge [1] In the single-link table of Node 1. erase (edge [1]. begin () + 2, edge [1]. begin () + 3); // Delete the node adjacent to node 1. // The parameter is (vector. begin () + I, vector. begin () + I + 1) // I is the node number to be deleted (equivalent to the array subscript) // For example, delete the 3rd neighboring nodes (starting from 0) for (INT I = 0; I <= edge [1]. size ()-1; I ++) {// traverse all nodes adjacent to node 1 int nextnode = edge [1] [I]. nextnode; // here it is equivalent to a 2-dimensional array of int cost = edge [1] [I]. cost; printf ("% d \ n", nextnode, cost);} return 0 ;}
Notes on how to use vector to implement an adjacent linked list