闲暇有余,威盛第10题的解法(高手看看有问题不)
(原题) 题目10Please give a description of the algorithm to renmove the dead code in a program (That is,”dead code elimination ”),(An instruction and a variable are dead if it computes values which are not used on any executable path to the procedure’s exit.)For example, in below program,(请给出一个算法,实现移除死代码的功能)
b=a+c
d=c+f
d=d*a
d=d*c
return d;
Instruction “b=a+c”is dead since b has no effect to the final output d.(比如说上面的代码中b=a+c 就对最后结果d一点用处都没有)
We need an algorithm to find out such dead code and remove them from the final program.
思路:
根据每一个表达式 逐渐建立一张 有向图 以邻接表方式存储
struct _adjacentNode{
char nodeName[16];
char *nodeNameList;
int Listlength; // 出度数
}adjacentNode;
adjacentNode map[MAX_NODE_NUM];
比如 读到 d = c + f
遍历一下 d 节点的出度表 有没有c f如果没有 则加入 并且出度+2
整个图建立完后 根据 return 哪一个结点 开始 以该节点单向(出)遍历整个图,得到连通图,没有包括的结点就是dead expression
代码:
class-a.h
#ifndef _CLASS_A_H_
#define _CLASS_A_H_
struct listNode;
struct adjacentNode{
char nodeName;
struct listNode *nlist;
int degree; //出度
};
#endif
class-b.h
#ifndef _CLASS_B_H_
#define _CLASS_B_H_
struct adjacentNode;
struct listNode{
struct adjacentNode *pNode;
struct listNode *next;
};
#endif
//之所以放2个头件 主要是这两结构互相嵌套
algorithm.h
#ifndef _ALGORITHM_H_
#define _ALGORITHM_H_
#include <algorithm>
#include <vector>
#include <deque>
#include <stack>
using namespace std;
//广度优先
int MAP_BFS_Search(struct adjacentNode *pmap,struct adjacentNode *pStart,vector<char> &connGraph);
//深度优先
int MAP_DFS_Search(struct adjacentNode *pmap,struct adjacentNode *pStart,vector<char> &connGraph);
#endif
algorithm.cpp
#include "class-a.h"
#include "class-b.h"
#include "util.h"
#include "algorithm.h"
int MAP_BFS_Search(struct adjacentNode *pmap,struct adjacentNode *pStart,vector<char> &connGraph)
{
if(pStart == NULL || pStart->degree == -1)
return -1;
deque<char> charQueue;
connGraph.push_back(pStart->nodeName);
for(struct listNode *p1 = pStart->nlist; p1 !=NULL;charQueue.push_back(p1->pNode->nodeName),p1=p1->next);
while(!charQueue.empty())
{
char pop = charQueue.front();
charQueue.pop_front();
connGraph.push_back(pop);
struct adjacentNode *p2 = &(pmap[pop-'a']);
for(struct listNode *p3 = p2->nlist;p3 != NULL; p3 = p3->next)
{
char c = p3->pNode->nodeName;
if(find(connGraph.begin(),connGraph.end(),c) == connGraph.end())
{
charQueue.push_back(c);
}
}
}
return 0;
}
int MAP_DFS_Search(struct adjacentNode *pmap,struct adjacentNode *pStart,vector<char> &connGraph)
{
if(pStart == NULL || pStart->degree == -1)
return -1;
deque<char> charStack;
charStack.push_front(pStart->nodeName);
while(!charStack.empty())
{
char c = charStack.front();
struct listNode *p1 = NULL;
for(p1 = pmap[c-'a'].nlist; p1 != NULL; p1 = p1->next)
{
char d = p1->pNode->nodeName;
//已经遍历过
if(find(connGraph.begin(),connGraph.end(),d) != connGraph.end())
continue;
//是祖先
if(find(charStack.begin(),charStack.end(),d) != charStack.end())
continue;
charStack.push_front(d);
break;
}
if(p1 != NULL)
continue;
connGraph.push_back(c);
charStack.pop_front();
}
return 0;
}
util.h
#ifndef _UTIL_H_
#define _UTIL_H_
int ProcessFile(char *filename,struct adjacentNode *pmap,char &result);
int ProcessLine(struct adjacentNode *pmap,const char *line,char &result);
void InitializeMap(struct adjacentNode *pmap,int size);
bool IsNodeExist(struct listNode *listhead,char nodeName);
void PrintMap(struct adjacentNode *pmap,int size);
#endif