实验5 图的物理实现
分别提交两个附件:
1:实验5的日志报告文件,提交规范,参看课程规范。
2:实验5的源码包。(文件名也要规范,可以参考报告文件的格式)
实验五-图-实验日志
【2020.12.2】
任务:复习图论,知道图的矩阵存储结构和链表存储结构是如何实现的,并根据题目要求,理解书本上给的图的ADT如何完成图的相邻矩阵实现和邻接表实现。
【2020.12. 3】
任务:在理解图的两种存储实现方式后,将书本上的ADT模板输入程序中,并补足书本给出的模板未完成的链表实现的ADT。
【2020.12.4】
任务:在图的ADT大致完成的前提下,利用图的相邻矩阵,完成题目要求的main函数的编写。
【遇到问题1】:在codeblock上运行无误后上交,网页上运行出现错误。反馈结果如下图:
错误原因:链表的ADT中,int length()函数和int currPos()函数重复出现了,并且其中一对数据类型和函数名未分隔开。
改正方法:将数据类型和函数名未分开的一组多于函数删去即可。更改后的代码和运行截图如下:
再次提交,已经不再报错。(但出现了运行错误)
【遇到问题2】:在codeblock上运算,返回一个随机数值,但输出正确。提示信息如下。
错误原因:输出无误,但返回一个随机数值,可见是析构函数出了问题。经过一番检查,发现是图的相邻矩阵的ADT析构函数出现了问题。
改正方法:delete的是一个双重的指针。具体修改如下图:
运行结果如图:
此时运行返回值为,无误。
【遇到问题3】:提交后发现一组运行正常,一组出现错误。初步判断是逻辑出现了问题。
错误原因:在统计结点的出度时,中间比较值mymax未更新,导致逻辑只能记录出度比0大的结点。
更正方法:及时更新中间比较值mymax,如下图:
更正后提交,第一组数据正确。(但第二组数据又出错了)
【遇到问题4】:第二组数据输出错误,如下图:
错误原因:当两个节点出度一样时,程序优先记录前面一个节点,但题目要求我们记录后一个节点。
更正方法:修改查找出度最大值时的数据更新条件即可。如图:
修改后再次提交,无误。
【2020.12.5】
任务:在图的ADT大致完成的前提下,利用图的邻接表,完成题目要求的main函数的编写。
遇到问题5:和之前相邻矩阵实现时一样,输出正确,但返回值为一个随机值,如图所示:
错误原因:输出正确但返回值错误,初步判断是析构函数出了问题。回到graphl.h文件中看析构函数,发现是释放链表空间时出错。
改正方法:将标注的[ ]去除即可。
修改后再次运行,返回值无误,为0。
至此,实验五完成。
代码实现部分:
我记得图的邻接表和相邻矩阵有几个头文件内容是一样的,但具体哪几个不一样我给忘了,所以就全放上来了。
图的邻接表实现
graph.h
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include <iostream>
#include "list.h"
class graph
{
private:
void operator = (const graph&){}
graph(const graph&){}
public:
graph(){}
virtual ~graph(){}
virtual void init(int n) =0;
virtual int n() = 0;
virtual int e() = 0;
virtual int first(int v) = 0;
virtual int next(int v,int w) = 0;
virtual void setedge(int v1,int v2,int wght) = 0;
virtual void deledge(int v1,int v2) = 0;
virtual bool isedge(int i,int j) = 0;
virtual int weight(int v1,int v2) = 0;
virtual void setmark(int v,int val) = 0;
virtual void print() = 0;
virtual int find_out() = 0;
virtual int find_out_num() = 0;
};
#endif // GRAPH_H_INCLUDED
graphl.h
#ifndef GRAPHL_H_INCLUDED
#define GRAPHL_H_INCLUDED
#define UNVISITED 0
#include "graph.h"
class edge
{
int vert ,wt;
public:
edge() {vert = -1; wt = -1;}
edge(int v,int w) {vert = v; wt = w;}
int vertex() {return vert;}
int weight() {return wt;}
};
class graphl:public graph //аз╫с╠М
{
private:
List<edge>** vertex;
int numvertex,numedge;
int *mark;
public:
graphl(int numvert) {init(numvert);}
~graphl()
{
delete []mark;
for(int i = 0; i < numvertex; i++)
delete vertex[i];
delete [] vertex;
}
void init(int n)
{
int i;
numvertex = n;
numedge = 0;
mark = new int[n];
for(i = 0 ;i< numvertex ; i++)
mark[i] = UNVISITED;
vertex = (List<edge>**) new List<edge>*[numvertex];
for(i = 0 ;i< numvertex ; i++)
vertex[i] = new LList<edge>();
}
int n() {return numvertex;}
int e() {return numedge;}
int first(int v)
{
if(vertex[v]->length() == 0)
return numvertex;
vertex[v]->moveToStart();
edge it = vertex[v]->getValue();
return it.vertex();
}
int next(int v,int w)
{
edge it;
if(isedge(v,w))
{
if((vertex[v]->currPos()+1) < vertex[v]->length())
{
vertex[v]->next();
it = vertex[v]->getValue();
return it.vertex();
}
}
return n();
}
void setedge(int i,int j,int weight)
{
//Assert(weight > 0,"May not set weight to 0");
edge curredge(j,weight);
if(isedge(i,j))
{
vertex[i]->remove();
vertex[i]->insert(curredge);
}
else
{
numedge++;
for(vertex[i]->moveToStart();
vertex[i]->currPos() < vertex[i]->length();
vertex[i]->next())
{
edge temp = vertex[i]->getValue();
if(temp.vertex() > j) break;
}
vertex[i]->insert(curredge);
}
}
void deledge(int i,int j)
{
if(isedge(i,j))
{
vertex[i]->remove();
numedge--;
}
}
bool isedge(int i , int j)
{
edge it;
for(vertex[i]->moveToStart();
vertex[i]->currPos() < vertex[i]->length();
vertex[i]->next())
{
edge temp = vertex[i]->getValue();
if(temp.vertex() == j) return true;
}
return false;
}
int weight(int i,int j)
{
edge curr;
if(isedge(i,j))
{
curr = vertex[i]->getValue();;
return curr.weight();
}
else
return 0;
}
void print()
{
for(int i = 0 ; i < numvertex ; i++)
{
for(int j = 0 ; j < numvertex ; j++)
{
if(isedge(i,j))
{
cout <<weight(i,j) << " ";
}
else
cout << '0' << " ";
}
cout << endl;
}
}
int find_out() //vertex[v]->length()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = vertex[i]->length();
if(mymax <= sum)
{
answer = i ;
mymax = sum;
}
}
return answer;
}
int find_out_num()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = vertex[i]->length();
if(mymax <= sum)
{
answer = sum ;
mymax = sum;
}
}
return answer;
}
int getmark(int v) {return mark[v];}
void setmark(int v,int val) {mark[v] = val;}
};
#endif // GRAPHL_H_INCLUDED
graphm.h
#ifndef GRAPHM_H_INCLUDED
#define GRAPHM_H_INCLUDED
#define UNVISITED 0
#include "graph.h"
class graphm : public graph//脕脷陆脫戮脴脮贸
{
private:
int numvertex,numedge;
int **matrix;
int *mark;
public:
graphm(int numvert)
{
init(numvert);
}
~graphm()
{
delete []mark;
for(int i = 0 ; i < numvertex; i++)
delete [] matrix[i];
delete [] matrix;
}
void init(int n)
{
int i;
numvertex = n;
numedge = 0;
mark = new int [n];
for(i = 0 ; i < numvertex; i++)
mark[i] = UNVISITED;
matrix = (int**)new int *[numvertex];
for(i = 0 ; i < numvertex ; i++)
matrix[i] = new int[numvertex];
for(i = 0 ; i < numvertex ; i++)
for(int j = 0; j < numvertex; j++)
matrix[i][j] = 0;
}
int n() {return numvertex;}
int e() {return numedge;}
int first(int v)
{
for(int i = 0; i < numvertex; i++)
if(matrix[v][i] != 0)
return i;
return numvertex;
}
int next(int v,int w)
{
for(int i = w+1; i < numvertex; i++)
if(matrix[v][i] != 0)
return i;
return numvertex;
}
void setedge(int v1,int v2,int wt)
{
//Assert(wt > 0,"Illegal weight value");
if(matrix[v1][v2] == 0) numedge++;
matrix[v1][v2] = wt;
}
void deledge(int v1,int v2)
{
if(matrix[v1][v2] != 0) numedge--;
matrix[v1][v2] = 0;
}
bool isedge(int i,int j)
{
return matrix[i][j] != 0;
}
void print()
{
for(int i = 0 ; i < numvertex ; i++)
{
for(int j = 0 ; j < numvertex ; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int find_out()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = 0;
for(int j = 0 ; j < numvertex ; j++)
if(matrix[i][j] != 0) sum++;
if(mymax <= sum)
{
answer = i ;
mymax = sum;
}
}
return answer;
}
int find_out_num()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = 0;
for(int j = 0 ; j < numvertex ; j++)
if(matrix[i][j] != 0) sum++;
if(mymax <= sum)
{
answer = sum ;
mymax = sum;
}
}
return answer;
}
int weight(int v1,int v2) {return matrix[v1][v2];}
int getmark(int v) {return mark[v];}
void setmark(int v,int val) {mark[v] = val;}
};
#endif // GRAPHM_H_INCLUDED
list.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include<assert.h>
using namespace std;
template <typename E> class Link {
public:
E element; // Value forthis node 结点值
Link *next;
// Pointer to next node in list 结点指针:在链表中指向下一结点
// Constructors 构造函数
Link(const E& elemval, Link* nextval =NULL)
{ element =elemval; next = nextval; }
Link(Link*nextval =NULL) { next = nextval; }
};
template <typename E> class List { // List ADT 抽象数据类型定义
private:
void operator=(const List&) {}
List(const List&) {}
public:
List() {} //构造函数
virtual~List() {} //析构函数
// 清空列表中的内容
virtual void clear() = 0;
// 在当前位置插入元素item
virtual void insert(const E& item) = 0;
// 在表尾添加元素item
virtual void append(const E& item) = 0;
// 删除当前元素,并将其作为返回值
virtual E remove() = 0;
// 将当前位置设置为顺序表起始处
virtual void moveToStart() = 0;
//将当前位置设置为顺序表末尾
virtual void moveToEnd() = 0;
// 将当前位置左移一步,如果当前位置在首位就不变
virtual void prev() = 0;
// 将当前位置右移一步,如果当前位置在末尾就不变
virtual void next()= 0;
// 返回列表当前的元素个数
virtual int length() const = 0;
// 返回当前元素的位置
virtual int currPos() const = 0;
// 将当前位置设置为pos
virtual void moveToPos(int pos) = 0;
// 返回当前元素
virtual const E& getValue() const = 0;
};
template <typename E> class LList:public List<E> {
private:
Link<E>* head; // Pointer to list header 指向链表头结点
Link<E>* tail; // Pointer to last element 指向链表最后一个结点
Link<E>* curr; // Access to current element 指向当前元素
int cnt; // Size of list 当前列表大小
void init() { // Intialization helper method 初始化
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {
while(head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size=100) { init(); } // Constructor 构造函数
~LList() { removeall(); } // Destructor 析构函数
void print() const; // Print list contents 打印列表内容
void clear() { removeall(); init(); } // Clear list清空列表
// Insert "it" atcurrent position 在当前位置插入“it”
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if (tail == curr) tail = curr->next; // New tail 新的尾指针
cnt++;
}
void append(const E& it) { // Append "it" to list 在列表的尾部追加“it”
tail = tail->next = new Link<E>(it, NULL);
cnt++;
}
// Remove and return current element 删除并返回当前元素
E remove() {
assert(curr->next != NULL); //"Noelement" 若当前没有元素则中断程序
E it = curr->next->element; // Remember value 保存元素值
Link<E>*ltemp = curr->next; // Remember link node保存指针域信息
if (tail == curr->next) tail = curr; // Reset tail 重置尾指针
curr->next = curr->next->next; // Remove from list从列表中删除
delete ltemp; // Reclaim space 回收空间
cnt--; // Decrement the count 当前链表长度减一
return it;
}
void moveToStart() // Place curr at list start将curr设置在链表头部
{ curr = head; }
void moveToEnd() // Place curr at list end 将curr设置在链表尾部
{curr = tail; }
// Move curr one step left; no change ifalready at front
// 将curr指针往前移一步;如果已经指向头部了就不需要改变
void prev() {
if (curr == head) return; // No previous element 若当前指针是头指针直接返回
Link<E>* temp = head;
// March down list until we findthe previous element 循环链表直到找到前一个元素
while (temp->next!=curr) temp=temp->next;
curr = temp;
}
// Move curr one step right; no changeif already at end
//将curr指针往后移一步;如果已经指向尾部了就不需要改变
void next()
{if (curr != tail) curr = curr->next; }
// Move down list to "pos" position 向下移动到列表“pos”位置
void moveToPos(int pos) {
assert ((pos>=0)&&(pos<=cnt));//"Position out of range" 不在范围内
curr = head;
for(int i=0; i<pos; i++) curr = curr->next;
}
const E& getValue() const { // Return current element 返回当前元素
assert(curr->next != NULL);//"No value" 内容为空
return curr->next->element;
}
int length() const
{
return cnt;
}
int currPos() const
{
Link<E>* temp = head;
int i;
for(i = 0 ; curr != temp ; i++)
temp = temp->next;
return i;
}
};
#endif // LIST_H_INCLUDED
main.c
#include
#include “graphm.h”
#include “graphl.h”
#include “string.h”
using namespace std;
int main()
{
int numv,nume;
cin >> numv >> nume;
graphl mymap(numv);
char myvertex[numv];
for(int i = 0 ; i < numv ; i++)
cin >> myvertex[i];
for(int time = 0 ; time < nume ; time++)
{
char start , ending;
int key;
cin >> start >> ending >> key;
int v1 = 0 , v2 = 0;
for(int i = 0 ; i < numv ; i++)
{
if(start == myvertex[i])
{
v1 = i;
break;
}
}
for(int i = 0 ; i < numv ; i++)
{
if(ending == myvertex[i])
{
v2 = i;
break;
}
}
mymap.setedge(v1,v2,key);
}
mymap.print();
int address = mymap.find_out();
cout << myvertex[address] << " " << mymap.find_out_num() << endl;
return 0;
}
图的相邻矩阵实现
graph.h
#include <iostream>
#include "graphm.h"
#include "graphl.h"
#include "string.h"
using namespace std;
int main()
{
int numv,nume;
cin >> numv >> nume;
graphm mymap(numv);
char myvertex[numv];
for(int i = 0 ; i < numv ; i++)
cin >> myvertex[i];
for(int time = 0 ; time < nume ; time++)
{
char start , ending;
int key;
cin >> start >> ending >> key;
int v1 = 0 , v2 = 0;
for(int i = 0 ; i < numv ; i++)
{
if(start == myvertex[i])
{
v1 = i;
break;
}
}
for(int i = 0 ; i < numv ; i++)
{
if(ending == myvertex[i])
{
v2 = i;
break;
}
}
mymap.setedge(v1,v2,key);
}
mymap.print();
int address = mymap.find_out();
cout << myvertex[address] << " " << mymap.find_out_num() << endl;
return 0;
}
graphl.h
#ifndef GRAPHL_H_INCLUDED
#define GRAPHL_H_INCLUDED
#define UNVISITED 0
#include "graph.h"
class edge
{
int vert ,wt;
public:
edge() {vert = -1; wt = -1;}
edge(int v,int w) {vert = v; wt = w;}
int vertex() {return vert;}
int weight() {return wt;}
};
class graphl:public graph //邻接表
{
private:
List<edge>** vertex;
int numvertex,numedge;
int *mark;
public:
graphl(int numvert) {init(numvert);}
~graphl()
{
delete []mark;
for(int i = 0; i < numvertex; i++)
delete [] vertex[i];
delete [] vertex;
}
void init(int n)
{
int i;
numvertex = n;
numedge = 0;
mark = new int[n];
for(i = 0 ;i< numvertex ; i++)
vertex[i] = new LList<edge>();
}
int n() {return numvertex;}
int e() {return numedge;}
int first(int v)
{
if(vertex[v]->length() == 0)
return numvertex;
vertex[v]->moveToStart();
edge it = vertex[v]->getValue();
return it.vertex();
}
int next(int v,int w)
{
edge it;
if(isedge(v,w))
{
if((vertex[v]->currPos()+1) < vertex[v]->length())
{
vertex[v]->next();
it = vertex[v]->getValue();
return it.vertex();
}
}
return n();
}
void setedge(int i,int j,int weight)
{
//Assert(weight > 0,"May not set weight to 0");
edge curredge(j,weight);
if(isedge(i,j))
{
vertex[i]->remove();
vertex[i]->insert(curredge);
}
else
{
numedge++;
for(vertex[i]->moveToStart();
vertex[i]->currPos() < vertex[i]->length();
vertex[i]->next())
{
edge temp = vertex[i]->getValue();
if(temp.vertex() > j) break;
}
vertex[i]->insert(curredge);
}
}
void deledge(int i,int j)
{
if(isedge(i,j))
{
vertex[i]->remove();
numedge--;
}
}
bool isedge(int i , int j)
{
edge it;
for(vertex[i]->moveToStart();
vertex[i]->currPos() < vertex[i]->length();
vertex[i]->next())
{
edge temp = vertex[i]->getValue();
if(temp.vertex() == j) return true;
}
return false;
}
int weight(int i,int j)
{
edge curr;
if(isedge(i,j))
{
curr = vertex[i]->getValue();;
return curr.weight();
}
else
return 0;
}
int getmark(int v) {return mark[v];}
void setmark(int v,int val) {mark[v] = val;}
};
#endif // GRAPHL_H_INCLUDED
graphm.h
#ifndef GRAPHM_H_INCLUDED
#define GRAPHM_H_INCLUDED
#define UNVISITED 0
#include "graph.h"
class graphm : public graph//áú?ó???ó
{
private:
int numvertex,numedge;
int **matrix;
int *mark;
public:
graphm(int numvert)
{
init(numvert);
}
~graphm()
{
delete []mark;
for(int i = 0 ; i < numvertex; i++)
delete [] matrix[i];
delete [] matrix;
}
void init(int n)
{
int i;
numvertex = n;
numedge = 0;
mark = new int [n];
for(i = 0 ; i < numvertex; i++)
mark[i] = UNVISITED;
matrix = (int**)new int *[numvertex];
for(i = 0 ; i < numvertex ; i++)
matrix[i] = new int[numvertex];
for(i = 0 ; i < numvertex ; i++)
for(int j = 0; j < numvertex; j++)
matrix[i][j] = 0;
}
int n() {return numvertex;}
int e() {return numedge;}
int first(int v)
{
for(int i = 0; i < numvertex; i++)
if(matrix[v][i] != 0)
return i;
return numvertex;
}
int next(int v,int w)
{
for(int i = w+1; i < numvertex; i++)
if(matrix[v][i] != 0)
return i;
return numvertex;
}
void setedge(int v1,int v2,int wt)
{
//Assert(wt > 0,"Illegal weight value");
if(matrix[v1][v2] == 0) numedge++;
matrix[v1][v2] = wt;
}
void deledge(int v1,int v2)
{
if(matrix[v1][v2] != 0) numedge--;
matrix[v1][v2] = 0;
}
bool isedge(int i,int j)
{
return matrix[i][j] != 0;
}
void print()
{
for(int i = 0 ; i < numvertex ; i++)
{
for(int j = 0 ; j < numvertex ; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int find_out()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = 0;
for(int j = 0 ; j < numvertex ; j++)
if(matrix[i][j] != 0) sum++;
if(mymax <= sum)
{
answer = i ;
mymax = sum;
}
}
return answer;
}
int find_out_num()
{
int answer = 0;int mymax = 0;
for(int i = 0 ; i < numvertex ; i++)
{
int sum = 0;
for(int j = 0 ; j < numvertex ; j++)
if(matrix[i][j] != 0) sum++;
if(mymax <= sum)
{
answer = sum ;
mymax = sum;
}
}
return answer;
}
int weight(int v1,int v2) {return matrix[v1][v2];}
int getmark(int v) {return mark[v];}
void setmark(int v,int val) {mark[v] = val;}
};
#endif // GRAPHM_H_INCLUDED
list.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include<assert.h>
using namespace std;
template <typename E> class Link {
public:
E element; // Value forthis node 结点值
Link *next;
// Pointer to next node in list 结点指针:在链表中指向下一结点
// Constructors 构造函数
Link(const E& elemval, Link* nextval =NULL)
{ element =elemval; next = nextval; }
Link(Link*nextval =NULL) { next = nextval; }
};
template <typename E> class List { // List ADT 抽象数据类型定义
private:
void operator=(const List&) {}
List(const List&) {}
public:
List() {} //构造函数
virtual~List() {} //析构函数
// 清空列表中的内容
virtual void clear() = 0;
// 在当前位置插入元素item
virtual void insert(const E& item) = 0;
// 在表尾添加元素item
virtual void append(const E& item) = 0;
// 删除当前元素,并将其作为返回值
virtual E remove() = 0;
// 将当前位置设置为顺序表起始处
virtual void moveToStart() = 0;
//将当前位置设置为顺序表末尾
virtual void moveToEnd() = 0;
// 将当前位置左移一步,如果当前位置在首位就不变
virtual void prev() = 0;
// 将当前位置右移一步,如果当前位置在末尾就不变
virtual void next()= 0;
// 返回列表当前的元素个数
virtual int length() const = 0;
// 返回当前元素的位置
virtual int currPos() const = 0;
// 将当前位置设置为pos
virtual void moveToPos(int pos) = 0;
// 返回当前元素
virtual const E& getValue() const = 0;
};
template <typename E> class LList:public List<E> {
private:
Link<E>* head; // Pointer to list header 指向链表头结点
Link<E>* tail; // Pointer to last element 指向链表最后一个结点
Link<E>* curr; // Access to current element 指向当前元素
int cnt; // Size of list 当前列表大小
void init() { // Intialization helper method 初始化
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {
while(head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size=100) { init(); } // Constructor 构造函数
~LList() { removeall(); } // Destructor 析构函数
void print() const; // Print list contents 打印列表内容
void clear() { removeall(); init(); } // Clear list清空列表
// Insert "it" atcurrent position 在当前位置插入“it”
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if (tail == curr) tail = curr->next; // New tail 新的尾指针
cnt++;
}
void append(const E& it) { // Append "it" to list 在列表的尾部追加“it”
tail = tail->next = new Link<E>(it, NULL);
cnt++;
}
// Remove and return current element 删除并返回当前元素
E remove() {
assert(curr->next != NULL); //"Noelement" 若当前没有元素则中断程序
E it = curr->next->element; // Remember value 保存元素值
Link<E>*ltemp = curr->next; // Remember link node保存指针域信息
if (tail == curr->next) tail = curr; // Reset tail 重置尾指针
curr->next = curr->next->next; // Remove from list从列表中删除
delete ltemp; // Reclaim space 回收空间
cnt--; // Decrement the count 当前链表长度减一
return it;
}
void moveToStart() // Place curr at list start将curr设置在链表头部
{ curr = head; }
void moveToEnd() // Place curr at list end 将curr设置在链表尾部
{curr = tail; }
// Move curr one step left; no change ifalready at front
// 将curr指针往前移一步;如果已经指向头部了就不需要改变
void prev() {
if (curr == head) return; // No previous element 若当前指针是头指针直接返回
Link<E>* temp = head;
// March down list until we findthe previous element 循环链表直到找到前一个元素
while (temp->next!=curr) temp=temp->next;
curr = temp;
}
// Move curr one step right; no changeif already at end
//将curr指针往后移一步;如果已经指向尾部了就不需要改变
void next()
{if (curr != tail) curr = curr->next; }
// Move down list to "pos" position 向下移动到列表“pos”位置
void moveToPos(int pos) {
assert ((pos>=0)&&(pos<=cnt));//"Position out of range" 不在范围内
curr = head;
for(int i=0; i<pos; i++) curr = curr->next;
}
const E& getValue() const { // Return current element 返回当前元素
assert(curr->next != NULL);//"No value" 内容为空
return curr->next->element;
}
int length() const
{
return cnt;
}
int currPos() const
{
Link<E>* temp = head;
int i;
for(i = 0 ; curr != temp ; i++)
temp = temp->next;
return i;
}
};
#endif // LIST_H_INCLUDED
main.c
#include
#include “graphm.h”
#include “graphl.h”
#include “string.h”
using namespace std;
int main()
{
int numv,nume;
cin >> numv >> nume;
graphm mymap(numv);
char myvertex[numv];
for(int i = 0 ; i < numv ; i++)
cin >> myvertex[i];
for(int time = 0 ; time < nume ; time++)
{
char start , ending;
int key;
cin >> start >> ending >> key;
int v1 = 0 , v2 = 0;
for(int i = 0 ; i < numv ; i++)
{
if(start == myvertex[i])
{
v1 = i;
break;
}
}
for(int i = 0 ; i < numv ; i++)
{
if(ending == myvertex[i])
{
v2 = i;
break;
}
}
mymap.setedge(v1,v2,key);
}
mymap.print();
int address = mymap.find_out();
cout << myvertex[address] << " " << mymap.find_out_num() << endl;
return 0;
}