Notes on how to use vector to implement an adjacent linked list

Source: Internet
Author: User

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

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.