邻接表-C++

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值