6-1 求顺序表最大值 (10分)
int GetMax(SqList L){
int i,maxx;
for(i=0;i<L.length;i++){
if(maxx<L.elem[i]){
maxx=L.elem[i];
}
}
return maxx;
}
6-2 单链表逆置(*) (10分)
void LListReverse(LLIST *list){
NODE *p, *q;
NODE *zc=list->head;
p = zc->next; //P指向链表第一个元素
zc->next = NULL; //断开头结点与链表
while(p != NULL)
{
q = p;
p = p->next;
q->next = zc->next; //相当于前插法构建新的链表,和原来的相反
zc->next = q;
}
}
6-3 十进制转二进制(顺序栈设计和应用) (10分)
bool isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
void Push(int x){
mystack[++top]=x;
}
int getTop(){
return mystack[top];
}
void Pop(){
top--;
}
6-5 求二叉树高度 (20分)
int GetHeight( BinTree BT )
{
int l=0,r=0;
if(!BT)
return 0;
else{
l=GetHeight(BT->Left);
r=GetHeight(BT->Right);
return (l>=r?l:r)+1;
}
}
6-6 二叉树的遍历 (25分)
void InorderTraversal(BinTree bt)/
{
if (bt == NULL){
return;
} //递归调用的结束条件
else {
InorderTraversal(bt->Left);
printf(" %c",bt->Data); //前序递归遍历bt的左子树
InorderTraversal(bt->Right); //前序递归遍历bt的右子树
}
}
void PreorderTraversal(BinTree bt)///
{
if (bt == NULL){
return;
} //递归调用的结束条件
else {
printf(" %c",bt->Data);
PreorderTraversal(bt->Left); //前序递归遍历bt的左子树
PreorderTraversal(bt->Right); //前序递归遍历bt的右子树
}
}
void PostorderTraversal(BinTree bt)
{
if (bt == NULL){
return;
} //递归调用的结束条件
else {
PostorderTraversal(bt->Left); //前序递归遍历bt的左子树
PostorderTraversal(bt->Right);
printf(" %c",bt->Data); //前序递归遍历bt的右子树
}
}
void LevelorderTraversal(BinTree bt)
{
BinTree Q[100], q = NULL; //顺序队列最多100个结点
int front = -1, rear = -1; //队列初始化
if (bt == NULL) return; //二叉树为空,算法结束
Q[++rear] = bt; //根指针入队
while (front != rear) //当队列非空时
{
q = Q[++front];
printf(" %c",q->Data);
if (q->Left!= NULL) Q[++rear] = q->Left;
if (q->Right != NULL) Q[++rear] = q->Right;
}
}
6-7 先序输出叶结点 (15分)
void PreorderPrintLeaves( BinTree BT )
{
if (BT == NULL)
{
return;
} //递归调用的结束条件
else
{
if(BT->Left==NULL&&BT->Right==NULL){
printf(" %c",BT->Data);
}
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
}
6-8 邻接矩阵存储图的深度优先遍历 (20分)
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) ){
Visit(V);
Visited[V] = true;
for (int j = 0; j < Graph->Nv; j++)
if (Graph->G[V][j] == 1 && Visited[j] == false) DFS(Graph,j,Visit);
}
6-9 邻接表存储图的广度优先遍历 (20分)
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ){
int w, j, Q[1000]; //采用顺序队列
int front = -1, rear = -1; //初始化队列
PtrToAdjVNode p = NULL;
//cout << adjlist[v].vertex;
Visit(S);
Visited[S] = 1;
Q[++rear] = S; //被访问顶点入队
while (front != rear) //当队列非空时
{
w = Q[++front];
p=Graph->G[w].FirstEdge;
//p = adjlist[w].FirstEdge; //工作指针p指向顶点v的边表
while (p != NULL)
{
j = p->AdjV;
if (Visited[j] == 0) {
//cout << adjlist[j].vertex;
Visit(j);
Visited[j] = 1;
Q[++rear] = j;
}
p = p->Next;
}
}
6-10 二分查找 (20分)
Position BinarySearch( List L, ElementType X ){
for(int i=1;i<=L->Last;i++){
if(X==L->Data[i]){return i;}
}
return NotFound;
}
6-11 快速排序 (15分)
int Partition(SqList &L, int low, int high){
int pivot = L.r[low].key;
while(low < high){
while(low < high && L.r[high].key >= pivot){
high --;
}
L.r[low].key= L.r[high].key;
while(low < high && L.r[low].key <= pivot){
low ++;
}
L.r[high].key = L.r[low].key;
}
L.r[low].key = pivot;
return low;
}
void QuickSort(SqList &L, int low, int high){
if(low < high)
{
int pivot = Partition(L,low,high);
QuickSort(L,low,pivot - 1);
QuickSort(L, pivot + 1, high);
}
}
6-12 冒泡排序 (10分)
void bubbleSort(int arr[], int n){
int i,j,temp;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
版权声明:本文为qq_40468936原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。