邻接表-C++

原创 2015年11月19日 20:58:46
#include <iostream>
using namespace std;

struct Edge
{
	int start;
	int dest;
	int weight;
	friend ostream &operator<<(ostream &out,Edge &e);
};

ostream &operator<<(ostream &out,Edge &e)
{
	out<<"("<<e.start<<","<<e .dest<<","<<e .weight<<")";
	return out;
}
#include "Edge.h"
#include "SinglyLinkedList.h"
#include <iostream>
using namespace std;

template<class T>
struct Vertex
{
	T data;
	SinglyLinkedList<Edge> adjlink;
};


#include "Node.h"
#include <iostream>
using namespace std;

template<class T>
class SinglyLinkedList
{
public:
	Node<T> *head;

	SinglyLinkedList();
	SinglyLinkedList(T value[],int n);
	~SinglyLinkedList();

	bool isEmpty();
	int length();
	Node<T> *getNode(int i);
	T get(int i);
	bool set(int i,T x);
	friend ostream &operator<<<T>(ostream &out,SinglyLinkedList<T> &list);
	Node<T> *insert(int i,T x);
	bool remove(int i,T &old);
	void clear();
	void concat(SinglyLinkedList<T> &list);

private:

};

template<class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
	this->head=NULL;
}

template<class T>
SinglyLinkedList<T>::SinglyLinkedList(T value[],int n)
{
	head=NULL;
	if (n>0)
	{
		head=new Node<T>(value[0]);
		Node<T> *rear=head;
		int i=1;
		while (i<n)
		{
			rear->next=new Node<T>(value[i++]);
			rear=rear->next;
		}
	}
}

template<class T>
SinglyLinkedList<T>::~SinglyLinkedList()
{
	clear();
}

template<class T>
bool SinglyLinkedList<T>::isEmpty()
{
	return head==NULL;
}

template<class T>
int SinglyLinkedList<T>::length()
{
	Node<T> *p=head;
	int i=0;
	while(p!=NULL)
	{
		i++;
		p=p->next;
	}
	return i;
}

template<class T>
Node<T> *SinglyLinkedList<T>::getNode(int i)
{
	if (i<0)
	{
		return NULL;
	}
	int j=0;
	Node<T> *p=head;
	while(p!=NULL&&j<i)
	{
		j++;
		p=p->next;
	}
	return p;
}

template<class T>
T SinglyLinkedList<T>::get(int i)
{
	Node<T> *p=getNode(i);
	if (p!=NULL)
	{
		return p->data;
	}
	throw "false";
}

template<class T>
bool SinglyLinkedList<T>::set(int i,T x)
{
	Node<T> *p=getNode(i);
	if (p!=NULL)
	{
		p->data=x;
		return true;
	}
	return false;
}

template<class T>
ostream &operator<<(ostream &out,SinglyLinkedList<T> &list)
{
	Node<T> *p=list.head;
	out<<"(";
	while (p!=NULL)
	{
		out<<p->data;
		p=p->next;
		if (p!=NULL)
		{
			out<<",";
		}
	}
	out<<")\n";
	return out;
}

template<class T>
Node<T> *SinglyLinkedList<T>::insert(int i,T x)
{
	Node<T> *q=NULL;
	if (head==NULL||i<=0)
	{
		q=new Node<T>(x,head);
		head =q;
	}
	else
	{
		int j=0;
		Node<T> *p=head;
		while (p->next!=NULL&&j<i-1)
		{
			j++;
			p=p->next;
		}
		q=new Node<T>(x,p->next);
		p->next=q;
	}
	return q;
}

template<class T>
bool SinglyLinkedList<T>::remove(int i,T &old)
{
	if (head!=NULL&&i>=0)
	{
		if (i==0)
		{
			Node<T> *q=head;
			old=q->data;
			head=head->next;
			delete q;
			return true;
		}
		else
		{
			Node<T> *p=getNode(i-1);
			if (p!=NULL&&p->next!=NULL)
			{
				Node<T> *q=p->next;
				old=q->data;
				p->next=q->next;
				delete q;
				return true;
			}
		}
	}
	return false;
}

template<class T>
void SinglyLinkedList<T>::clear()
{
	Node<T> *p=head;
	while (p!=NULL)
	{
		Node<T> *q=p;
		p=p->next;
		delete q;
	}
	head =NULL;
}

template<class T>
void SinglyLinkedList<T>::concat(SinglyLinkedList<T> &list)
{
	if (this->head==NULL)
	{
		this->head=list.head;
	}
	else
	{
		Node<T> *p=head;
		while (p->next!=NULL)
		{
			p=p->next;
		}
		p->next=list.head;
	}
	list.head=NULL;
}

#include "Vertex.h"
#include <iostream>
using namespace std;

template<class T>
class AdjListGraph
{
public:
	AdjListGraph(int size=10);
	AdjListGraph(T vertices[],int vertCount,Edge edges[],int edgeCount);
	~AdjListGraph();

	int vertexCount();
	T get(int i);
	void insertVertex(T vertex);
	bool insertEdge(int i,int j,int weight);
	bool insertEdge(Edge edge);
	friend ostream &operator<<<T>(ostream &out,AdjListGraph<T> &graph);
	bool removeEdge(int i,int j);
	bool removeVertex(int v,T &old);

private:
	Vertex<T> *vertexList;
	int size;
	int vertCount;
};

template<class T>
AdjListGraph<T>::AdjListGraph(int size)
{
	this->size=size<10?10:size;
	this->vertexList=new Vertex<T>[this->size];
	this->vertCount=0;
}

template<class T>
AdjListGraph<T>::AdjListGraph(T vertices[],int vertCount,Edge edges[],int edgeCount)
{
	this->size=vertCount<10?10:vertCount;
	this->vertexList=new Vertex<T>[this->size];
	this->vertCount=0;

	for (int i=0;i<vertCount;i++)
	{
		insertVertex(vertices[i]);
	}
	for (int j=0;j<edgeCount;j++)
	{
		insertEdge(edges[j]);
	}
}

template<class T>
AdjListGraph<T>::~AdjListGraph()
{
	delete []vertexList;
}

template<class T>
int AdjListGraph<T>::vertexCount()
{
	return vertCount;
}

template<class T>
T AdjListGraph<T>::get(int i)
{
	if (i=0&&i<vertCount)
	{
		return vertexList[i].data;
	}
	throw "false";
}

template<class T>
ostream &operator<<(ostream &out,AdjListGraph<T> &graph)
{
	for (int i=0;i<graph.vertCount;i++)
	{
		out<<" "<<"顶点"<<graph.vertexList[i].data<<" 出边表:";
		out<<graph.vertexList[i].adjlink;
	}
	return out;
}

template<class T>
void AdjListGraph<T>::insertVertex(T vertex)
{
	if (vertCount==size)
	{
		Vertex<T> *temp=vertexList;
		vertexList=new Vertex<T>[size*2];
		for (int i=0;i<size;i++)
		{
			vertexList[i]=temp[i];
		}
		size*=2;
	}
	vertexList[vertCount].data=vertex;
	vertCount++;
}

template<class T>
bool AdjListGraph<T>::insertEdge(int i,int j,int weight)
{
	if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j)
	{
		Edge e={i,j,weight};
		Node<Edge> *p=vertexList[i].adjlink.head,*front;
		if (p==NULL||p!=NULL&&j<p->data.dest)
		{
			vertexList[i].adjlink.head=new Node<Edge>(e,p);
			return true;
		}
		while (p!=NULL)
		{
			if (j==p->data.dest)
			{
				return false;
			}
			front=p;
			p=p->next;
		}
		front->next=new Node<Edge>(e,p);
		return true;
	}
	return false;
}

template<class T>
bool AdjListGraph<T>::insertEdge(Edge edge)
{
	return insertEdge(edge.start,edge.dest,edge.weight);
}

template<class T>
bool AdjListGraph<T>::removeEdge(int i,int j)
{
	if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j)
	{
		Node<Edge> *p=vertexList[i].adjlink.head,*front;
		if (p!=NULL&&p->data.dest==j)
		{
			vertexList[i].adjlink.head=p->next;
			delete p;
			return true;
		}
		while(p!=NULL&&p->data.dest!=j)
		{
			front=p;
			p=p->next;
		}
		if (p!=NULL)
		{
			front->next=p->next;
			delete p;
			return true;
		}
	}
	return false;
}

template<class T>
bool AdjListGraph<T>::removeVertex(int v,T &old)
{
	if (v<0||v>=vertCount)
	{
		return false;
	}
	Node<Edge> *p=vertexList[v].adjlink.head;
	while (p!=NULL)
	{
		removeEdge(p->data.dest,p->data.start);
		p=p->next;
	}
	old=vertexList[v].data;
	vertexList[v].adjlink.clear();

	int i;
	for (i=v;i<vertCount-1;i++)
	{
		vertexList[i].data=vertexList[i+1].data;
		vertexList[i].adjlink.head=vertexList[i+1].adjlink.head;
		vertexList[i+1].adjlink.head=NULL;
	}
	vertCount--;

	for(i=0;i<vertCount;i++)
	{
		p=vertexList[i].adjlink.head;
		while (p!=NULL)
		{
			if (p->data.start>v)
			{
				p->data.start--;
			}
			if (p->data.dest>v)
			{
				p->data.dest--;
			}
			p=p->next;
		}
	}
	return true;
}

#include "AdjListGraph.h"
#include <iostream>
using namespace std;

int main()
{
	char *vertices="ABCDE";
	Edge edges[]={{0,1,5},{0,3,2},{1,0,6},{1,2,7},{2,4,3},{3,2,8},{3,4,9}};
	AdjListGraph<char> graph(vertices,5,edges,7);
	cout<<"带权有向图G4,\n"<<graph<<endl;

	char old=' ';
	if (graph.removeVertex(2,old))
	{
		cout<<"删除顶点"<<old<<",";
	}
	cout<<"删除边(V2,V3),"<<graph.removeEdge(2,3)<<"\n";
	cout<<graph<<endl;
	system("pause");
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

带权图(网)的邻接表存储的C++实现

// 图的邻接表数据结构C++描述#includeusingnamespacestd;constint MaxVexs=20;//前向声明templateclass Graph;templatecla...

邻接表数据结构C++

C++ 无向图 邻接表创建 广度遍历 深度遍历

花了点时间,写了下 无向图lingjieb

求有向图的强连通分支(邻接表存储)C++实现

// 强连通分支(邻接表存储).cpp : Defines the entry point for the console application. //通过二次利用深度优先搜索访问节点,每次的深度...

C++_将图二维矩阵形式转为邻接表结构

用二维矩阵表示图的结构我们用c++中的vector容器来表示二维矩阵, #include using std::vector vector<vector> Array; 关于vector的基...

图的邻接表表示代码C/C++

#include #include"iostream"#include"malloc.h"using namespace std;#define MaxVertexNum 50       ...

邻接表无向图 C++详解

转载请注明出处:http://www.cnblogs.com/skywang12345/ 邻接表无向图的介绍 邻接表无向图是指通过邻接表表示的无向图。 ...

邻接表实现PRIM算法(C++)

参考张宪超老师《数据结构、算法及应用》原理:G=是一个连通的带权无向图。Prim算法通过不断地增加生成树的顶点来得到最小生成树。在算法的任一时刻,一部分顶点已经添加到生成树的顶点集合中,而其余顶点尚未...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)