邻接链表代码实现

//创建邻接链表
//表节点
struct arcLNode
{
    int adjvex = -1;    //存储点节点下表
    int lineId  = -1;   //边的id
    std::shared_ptr<arcLNode> nextarc =nullptr;
};

//表头节点
struct headLNode
{
    headLNode(iCoord3d pt) : point(pt) {};
    ~headLNode() {};
    iCoord3d   point;  //节点坐标
    std::shared_ptr<arcLNode> firstarc=nullptr;      //表头连接的第一个节点
    bool      visited  = false;  //是否访问过
};

//邻接链表
struct AdjacentListGraph
{
    int vertexNum = 0, edgeNum = 0;
    vector<headLNode> node;
};

int main()
{
//创建邻接链表
    AdjacentListGraph graph;
    graph.edgeNum = static_cast<int>(lines.size());
    graph.vertexNum = static_cast<int>(points.size());
	//存储点
    for_each(points.begin(), points.end(), [&](const iCoord3d points) {
        headLNode hNode(points);
        graph.node.emplace_back(hNode);
		});
	//存储起始点在邻接表的位置
    int originNum(0);
	//存储边的信息
    for (const SegmentLine&line:lines)
	{
		//获取边的两个端点
        vector<iCoord3d> icPt {line.startPt, line.endPt, startPt};
        vector<int>      nums;
		for (const iCoord3d&pt:icPt)
		{
            for (int i = 0; i < graph.vertexNum; ++i)
            {   
				//如果两个点的距离大于1就不是同一个点
                if (pt.DistPtPt(graph.node[i].point) > 1)
                    continue;
                nums.emplace_back(i);
                break;
            }
		}
        iCoord3d sPt(line.startPt), ePt(line.endPt);
		//获取两个点的位置
        int sNum(nums[0]), eNum(nums[1]);
        originNum = nums[2];
		//如果第一点为空
        if (!graph.node[sNum].firstarc)
        {
            std::shared_ptr<arcLNode> arcNode(new arcLNode());
            arcNode->adjvex   = eNum;
            arcNode->lineId   = line.id;
            graph.node[sNum].firstarc  = std::move(arcNode);
        }
        else
        {
            std::shared_ptr<arcLNode> tempNode = graph.node[sNum].firstarc;
			//找到最后一个节点
            while (tempNode->nextarc)
                tempNode = tempNode->nextarc;
            std::shared_ptr<arcLNode> arcNode(new arcLNode());
            arcNode->adjvex           = eNum;
            arcNode->lineId  = line.id;
            tempNode->nextarc          = std::move(arcNode);
		}
        if (!graph.node[eNum].firstarc)
        {
            std::shared_ptr<arcLNode> arcNode(new arcLNode());
            arcNode->adjvex           = sNum;
            arcNode->lineId           = line.id;
            graph.node[eNum].firstarc = std::move(arcNode);
        }
        else
        {
            std::shared_ptr<arcLNode> tempNode = graph.node[eNum].firstarc;
            //找到最后一个节点
            while (tempNode->nextarc)
                tempNode = tempNode->nextarc;
            std::shared_ptr<arcLNode> arcNode(new arcLNode());
            arcNode->adjvex  = sNum;
            arcNode->lineId  = line.id;
            tempNode->nextarc = std::move(arcNode);
        }
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C 语言实现无向联通图的邻接链表代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 // 边节点结构体 typedef struct ArcNode { int adjvex; // 相邻顶点的下标 struct ArcNode* next; // 指向下一个边节点的指针 } ArcNode; // 顶点节点结构体 typedef struct VNode { int data; // 顶点的数据 ArcNode* firstarc; // 指向第一个边节点的指针 } VNode, AdjList[MAX_VERTEX_NUM]; // 邻接链表结构体 typedef struct { AdjList vertices; // 顶点数组 int vexnum; // 顶点数 int arcnum; // 边数 } ALGraph; // 初始化邻接链表 void InitGraph(ALGraph* G) { int i; G->vexnum = 0; G->arcnum = 0; for (i = 0; i < MAX_VERTEX_NUM; i++) { G->vertices[i].data = 0; // 顶点数据初始化为0 G->vertices[i].firstarc = NULL; // 边指针初始化为NULL } } // 添加顶点 void AddVertex(ALGraph* G, int v) { G->vertices[G->vexnum].data = v; G->vexnum++; } // 添加边 void AddEdge(ALGraph* G, int v1, int v2) { ArcNode* p; // 在v1的链表中添加一个节点 p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; // 在v2的链表中添加一个节点 p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v1; p->next = G->vertices[v2].firstarc; G->vertices[v2].firstarc = p; G->arcnum++; } // 输出邻接链表 void PrintGraph(ALGraph G) { int i; ArcNode* p; for (i = 0; i < G.vexnum; i++) { printf("%d: ", G.vertices[i].data); p = G.vertices[i].firstarc; while (p != NULL) { printf("%d ", G.vertices[p->adjvex].data); p = p->next; } printf("\n"); } } int main() { ALGraph G; InitGraph(&G); AddVertex(&G, 1); AddVertex(&G, 2); AddVertex(&G, 3); AddVertex(&G, 4); AddEdge(&G, 1, 2); AddEdge(&G, 1, 3); AddEdge(&G, 2, 4); AddEdge(&G, 3, 4); PrintGraph(G); return 0; } ``` 以上代码实现了无向联通图的邻接链表表示,其中使用了邻接链表结构体 ALGraph,顶点节点结构体 VNode 和边节点结构体 ArcNode,具体实现了初始化邻接链表,添加顶点和边,以及输出邻接链表等基本操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值