图的十字链表
#圖的十字鏈表
圖的十字鏈表就是圖的鄰接表和逆鄰接表結合在一起的東西,比較方便在查找一個結點的出度和入度
采用的是數組加鏈表的形式,首先現在結點構造的數組中填入結點,然后在采用鏈表的方法在每個結點后面添加相應的邊。
邊就是兩個結點的組成的集合嘛。
 那首先要構造兩個數據類型,一個是鏈表的,一個是數組的嘛;如下
數組的結構體定義如下:
這個就是由很多個單鏈表組成的。
###基本思想如下:
首先要有一找尋頂點在數組中位置的函數。用戶輸入頂點數首先先初始化數組,然后用戶出入邊數,根據邊數來創建,用戶輸入一條邊的兩個頂點,通過函數找到位置,創建一個新的node,然后讓node初始化node,連接,創建一個樹,就好了。代碼如下:
// main.cpp
// 十字鏈表
//
// Created by 橘子和香蕉 on 2018/11/24.
// Copyright ? 2018 橘子和香蕉. All rights reserved.
//
/*
在開發中遇到的問題:
1:在添加結點的時候,開始時候要查找結點在數組中的位置,若是沒有那就添加,找結點位置函數返回值是-1;,我遇到的問題是在添加新結點的時候結點的位置沒有更新這個結點在數組中的位置 ,講-1添加進去了。
2刪除結點中遇到的問題,首先要分為兩個方面a:如果結點在第一個位置,怎么刪除b:結點在不在第一個位置,怎么刪除
刪除其實就是單鏈表中的刪除操作,用兩個指針一個在前,一個在后的,遍歷鏈表刪除。
*/
#include <iostream>
using namespace std;
#define MAXSIZE 100
#define dataType char
typedef struct node{int tail;//弧尾在數組中的位置int head;//弧首在數組中的位置node *tailNode;//以這個頂點為弧尾的下一條的位置node *headNode;//以這個頂點為弧頭的下一天的位置int info;//權重
}node;
typedef struct Box{dataType data;node *in;//入度node *out;//出讀
}Box;
class Graph{
private:Box base[MAXSIZE];int vertexNum;int edgeNum;
#pragma private mathodint locate(dataType x);//定位
public:void init(int vertexNum,int edgeNum);//初始化結點個數和邊的個數void create();//創建圖int indegree(dataType x);//入度int outdegree(dataType x);//出度void addEdge(dataType start,dataType end,int wieght);//添加一條邊void deleteEdge(dataType start,dataType end);//刪除一條邊void printNode(dataType data,bool isIn);void printBase();
};
#pragma 公有函數聲明開始
void Graph::init(int vertexNum, int edgeNum){this->vertexNum = vertexNum;this->edgeNum = edgeNum;
}
int Graph::locate(dataType x){for (int i = 0; i<vertexNum; i++) {if(base[i].data == x){return I;}}return -1;
}
void Graph::create(){cout<<"input Graph node data \n";for (int i = 0; i<vertexNum; i++) {cin>>base[i].data;base[i].in = base[i].out = NULL;}cout<<"input Graph node Start node and en node:\n";dataType start,end;int wieght;int startPosition,endPosition;node *p;for (int i = 0; i<edgeNum; i++) {cout<<"input edge start and end and wieght:\n";cin>>start>>end>>wieght;startPosition = locate(start);endPosition = locate(end);p = new node;p->info = wieght;p->tail = startPosition;p->head = endPosition;p->tailNode = base[startPosition].out;base[startPosition].out = p;p->headNode= base[endPosition].in;base[endPosition].in = p;}cout<<"create finish\n";
}
int Graph::indegree(dataType x){int count = 0;node*p =base[ locate(x) ].in;while (p != NULL) {count++;p = p->headNode;}return count;
}
int Graph::outdegree(dataType x){int count = 0;node*p = base[locate(x)].out;while ( p != NULL) {count++;p = p->tailNode;}return count;
}
void Graph::addEdge(dataType start, dataType end, int wieght){int startPosition = locate(start);int endPostion = locate(end);if(startPosition == -1){//返回值為-1,說明這個結點還沒有在數組中,先是要添加,然后頂點數+1;邊數+1;base[vertexNum].data = start;base[vertexNum].in = base[vertexNum].out = NULL;init(vertexNum+1, edgeNum+1);}if(endPostion == -1){base[vertexNum].data = end;base[vertexNum].in = base[vertexNum].out = NULL;init(vertexNum+1, edgeNum+1);}if(startPosition == -1 && endPostion == -1){// 兩個頂點都沒有在數組中,那添加之后頂點個數+2.邊數+1;base[vertexNum].data = start;base[vertexNum].in = base[vertexNum].out = NULL;base[vertexNum].data = end;base[vertexNum].in = base[vertexNum].out = NULL;init(vertexNum+2, edgeNum+1);}node *p = new node;p->info = wieght;p->tail = startPosition;p->head = vertexNum-1;p->tailNode = base[startPosition].out;base[startPosition].out = p;p->headNode = base[endPostion].in;base[endPostion].in = p;
}void Graph::deleteEdge(dataType start , dataType end){int startPostion = locate(start);int endPostion = locate(end);if(startPostion == -1 || endPostion == -1){cout<<"can't not find edge\n";return;}node *nout = base[startPostion].out;node *Hnout = base[startPostion].out;node *nin = base[endPostion].in;node *Hnin = base[endPostion].in;int num = 0;while (nout != NULL) {if(num == 0 && nout->tail == startPostion && nout->head == endPostion){base[startPostion].out = nout->tailNode;Hnout = nout;nout = nout->tailNode;continue;}if(nout->tail == startPostion && nout->head == endPostion){Hnout->tailNode = nout->tailNode;break;}num++;Hnout = nout;nout = nout->tailNode;}num=0;while (nin != NULL) {if(num == 0 && nin->tail == startPostion && nin->head == endPostion){base[endPostion].in = nin->headNode;Hnin = nin;nin = nin->headNode;continue;}else if(nin->tail == startPostion && nin->head == endPostion ){Hnin->headNode = nin->headNode;delete nin;break;}num++;Hnin = nin;nin = nin->headNode;}}void Graph::printNode(dataType data,bool isIn){cout<<"printNode++++++++++++++++++++++++++++++++\n";int position = locate(data);if(position == -1){cout<<"input error\n";return;}node *p = nullptr;isIn?(p = base[position].in):(base[position].out);while (p!= NULL) {cout<<"node start:"<<base[p->tail].data<<"\t"<<"node end"<<base[p->head].data<<"\t"<<"node weight:"<<p->info<<"\n";isIn? p = p->headNode:p = p->tailNode;}cout<<"printNode_________________________________\n";
}
void Graph::printBase(){cout<<"PrintBse +++++++++++++++++++++++++++++++++++++\n";for (int i = 0; i<vertexNum; i++) {cout<<"base:"<<base[i].data<<endl;}cout<<"vertexNum:"<<this->vertexNum<<endl;cout<<"edgeNum:"<<this->edgeNum<<endl;cout<<"PrintBse _____________________________________\n";
}#pragma 公有函數聲明結束
int main (){Graph h;h.init(4, 7);h.create();h.printBase();cout<<"入度"<< h.indegree('b')<<endl;cout<<"出度"<< h.outdegree('b')<<endl;h.printNode('b', true);h.addEdge('b', 'e', 1);cout<<"出度"<< h.outdegree('b')<<endl;h.deleteEdge('b', 'e');cout<<"出度"<< h.outdegree('b')<<endl;h.printBase();cout<<"入度"<< h.indegree('a')<<endl;cout<<"出度"<< h.outdegree('a')<<endl;h.deleteEdge('a', 'b');cout<<"出度"<<h.outdegree('a');cout<<"入度"<<h.indegree('b');return 1;
}
測試用的圖如下:
總結
- 上一篇: 我是服务的执政官-服务发现和注册工具co
- 下一篇: 高翔《视觉SLAM十四讲》从理论到实践