A.<c:if>
B.<c:set>
C.<c:out>
D.<c:foreach>
第1题
void BFSTraverse(MGraph G) {
// 对以邻接矩阵存储表示的图G进行广度优先搜索遍历
bool visited[G.vexnum];// 附设访问标识数组
Queue Q;// 附设队列 Q
for (v=0; v<G.vexnum; ++v) visited[v] = (1)
InitQueue(Q,G.vexnum);// (2)
for ( v=0; v<G.vexnum; ++v )
if ( (3)) {
visited[v] = TRUE; VisitFunc(G.vexs[v]);
// 访问图中第 v 个顶点
(4)// 顶点v 入队列
while (!QueueEmpty(Q)) {
DeQueue(Q, u); // 队头元素出队并置为 u
for ( w=0; w<G.vexnum; w++; )
if ( G.arcs[u, w].adj && ! visited[w] ) {
visited[w] = TRUE; VisitFunc(w); // (5)
EnQueue(Q, w); // 当前访问的顶点 w 入队列 Q
} // if
} // while
} // if
DestroyQueue(Q);
} // BFSTraverse
(1)
(2)
(3)
(4)
(5)
第2题
【说明】
对给定的字符集合及相应的权值,采用哈夫曼算法构造最优二叉树,并用结构数组存储最优二叉树。例如,给定字符集合{a,b,c,d}及其权值2、7、4、5,可构造如图6-15所示的最优二叉树,以及相应的结构数组Ht(如表6-14所示,其中数组元素Ht[0]不用)。
结构数组Ht的类型定义如下:
define MAXLEAFNUM 20
struct node{
char ch; /*扫当前节点表示的字符,对于非叶子节点,此域不用*/
Int weight; /*当前节点的权值*/
int parent; /*当前节点的父节点的下标,为0时表示无父节点*/
int lchild, rchild;
/*当前节点的左、右孩子节点的下标,为0时表示无对应的孩子节点*/
)Ht[2*MAXLEAFNUM];
用“0”或“广标识最优二叉树中分支的规则是:从一个节点进入其左(右)孩子节点,就用“0”(或“1”)标识该分支,如图6-15所示。
若用上述规则标识最优二叉树的每条分支后,从根节点开始到叶子节点为止,按经过分支的次序将相应标识依次排列,可得到由“0”、“1”组成的一个序列,称此序列为该叶子节点的前缀编码。例如,图6-15所示的叶子节点a、b、c、d的前缀编码分别是110、0、111、10。
函数void LeafCode(int root,int n)的功能是:采用非递归方法,遍历最优二叉树的全部叶子节点,为所有的叶子节点构造前缀编码。其中,形参root为最优二叉树的根节点下标;形参n为叶子节点个数。在函数void LeafCode(int root,int n)构造过程中,将Ht[p].weight域用做被遍历节点的遍历状态标志。
函数void Decode(char *buff,int root)的功能是:将前缀编码序列翻译成叶子节点的字符序列,并输出。其中,形参root为最优二叉树的根节点下标;形参buff指向前缀编码序列。
【函数4.1】
char **HC;
void LeafCode(int root, int n)
{ /*为最优二叉树中的n个叶子节点构造前缀编码,root是树的根节点下标*/
int I,p=root,cdlen=0;
char code[20];
Hc = (char **)malloc((n+1)*sizeof(char *)); /*申请字符指针数组*/
For(i = 1;i<= p;++I)
Ht [i]. weight = 0; /*遍历最优二叉树时用做被遍历节点的状态标志* /
While (p) { /*以非递归方法遍历最优二叉树,求树中每个叶子节点的编码*/
If(Ht[p].weight == 0) { /*向左*/
Ht[p].weight = 1;
If(Ht[p].lchild != 0) {
p = Ht[p].lchild;
code[cdlen++] = '0';
}
else if(Ht[p].rchild == 0) { /*若是叶子节点,则保存其前缀编码*/
Hc[p] = (char *)malloc((cdlen+1)*sizeof(char));
(1);
strcpy (Hc [p],code);
}
}
else if(Ht[p].weight == 1) { /*向右*/
Ht [p].weight = 2;
If(Ht[p].rchild != 0) {
p = Ht [p].rchild;
code[cdlen++] ='1';
}
}
else { /*Ht[p].weight == 2,回退/
Ht [p].weight = 0;
p =(2);
(3); /*退回父节点*/
}
} / *while .结束* /
}
【函数4.2】
void Decode(char *buff,int root)
{ int pre = root,p;
while(*buff != '\0') {
p = root;
&
第6题
A、int x; for (x=0; x<9; x++)> B、int x; for (x=0; x<10; x++)> C、int x; for (x=1; x<10; x++)> D、int x; for (x=1; x<=10; x++)>
第7题
[说明]
对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图6-1显示了各个类间的关系。以下是JAVA语言实现,能够正确编译通过。
[图6-1]
[Java代码]
//Iterator. java文件
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}
//Aggregate. java文件
public interface Aggregate {
public abstract Iterator iterator();
}
//Book. java
public class Book {
//省略具体方法和属性
}
//BookshelfIterator. java文件
public class Bookshelf工terator (1) Iterator{
private BookShelf bookShelf;
private int index;
public BookshelfIterator(BookShelf bookShelf) {
this. bookShelf = bookShelf;
this. index = 0;
}
public boolean hasNext(){//判断是否还有下一个元素
if(index < bookShelf. getLength()){
return true;
}else{
return false;
}
}
public Object next()f//取得下一个元素
Book book = bookShelf. getBookAt(index);
index++;
return book;
}
}
//BookShelf. java
import java. util. Vector;
public class BookShelf {
private Vector books;
public BookShelf(int initialsize){
this. books = new Vector(initialsize);
}
public Book getBookAt(int index){
return(Book)books.get(index);
}
public int getLength(){
return books.size();
}
public Iterator iterator(){
return new BookShelfIterator( (2) );
}
}
//Main. java文件
public class Main {
public static void main(String args){
BookShelf bookShelf = new BookShelf(4);
//将书籍上架,省略代码
Iterator it = bookShelf. (3) ;
while( (4) ){//遍历书架,输出书名
Book book = (Book)it. (5) ;
System.out.printin(" "+book.getName());
}
}
}
第9题
[说明]
对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。
[图5-1]
[C++代码]
template (1) >
class Iterator{
public:
virtual bool hasNext() = 0;
(2) Object* next() = 0;
};
class Book{
//省略具体方法和属性
};
class BookShelf{
private:
vector books;
public:
BookShelf(){
}
Book* getBookAt(int index){
return &booksindex;
}
int getLength(){
return books. size();
}
};
template
class BookshelfIterator : public (3) {
private:
BookShelf * bookShelf;
int index;
public:
BookshelfIterator(BookShelf *bookShelf){
this->bookShelf = bookShelf;
index = 0;
}
bool hasNext(){//判断是否还有下一个元素
if(index < bookShelf->getLength()){
return true;
}else{
return false;
}
}
Objeot* next(){//取得下一个元素
return bookShelf->getBookAt(index++);
}
};
int main()
{
BookShelf bookShelf;
//将书籍上架,省略代码
Book *book;
Iterator *it = new BookShelfIterator( (4) );
while( (5) ){//遍历书架,输出书名
book=(Book*)it->next();
/*访问元素*/
}
return 0;
}
第10题
B.用foreach循环
C.用while循环
D.用do..while循环
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!