老师对不起,我昨天忘记提交作业这是任务二十 #include<iostream> using namespace std; typedef struct BSTNode { int data; struct BSTNode*lchild, *rchild; }BSTNode, *BSTree; void Insert_BST(BSTree &T, int element) { if (!T) { T = (BSTree)malloc(sizeof(BSTNode)); if (!T) { return; } T->lchild = T->rchild = NULL; T->data = element; } else if (element == T->data) { return; } else if (element < T->data) { Insert_BST(T->lchild, element); } else { Insert_BST(T->rchild, element); } } void Create_BST(BSTree &T, int arr[], int n) { for (int i = 0; i < n; i++) { Insert_BST(T, arr[i]); } } bool Traver_BST(BSTree T) { if (T) { if (T->lchild) { if (T->lchild->data > T->data) { return false; } } if (T->rchild) { if (T->rchild->data < T->data) { return false; } } Traver_BST(T->lchild); Traver_BST(T->rchild); } return true; } int main(void) { BSTree T; T = NULL; int arr[] = { 5,4,3,2,1 }; Create_BST(T, arr, 5); bool f=Traver_BST(T); if (f) { cout << "是叉" << endl; } else { cout << "非叉" << endl; } return 0; }
老师,任务二十一昨天没有及时交上作业,现在补上
已知一棵二叉排序树上所有关键字中的最小值为-max,最大值为max,又-max<x<max。编写具有如下功能的函数:求该二叉树上小于x且最靠近x的值a和大于x且最靠近x的b。 typedef struct BTNode { int data; BTNode *lchild,*rchild; }BTNode,*BiTree; void fun(BiTree T,int x,int *a,int *b) { if(T) { if(x<T->data) { *b=T->data; fun(T->lchild,x,a,b); } else { if(x>T->data) { *a=T->data; fun(T->rchild,x,a,b); } else { if(T->lchild) { BiTree p=T->lchild; while(T->rchild) p=p->rchild; *a=p->data; } if(T->rchild) { BiTree p=T->rchild; while(p->lchild) p=p->lchild; *b=p->data; } } } return; } }
老师,任务十九排序纸质作业已上交,但我忘记雪梨上回复了。
不好意思,老师,这个是任务十
#include<iostream> #include<stdlib.h> using namespace std; typedef struct LNode { int data; struct LNode *next; }LNode,*Linklist; void Initlist(Linklist *l) { (*l) = (Linklist)malloc(sizeof(LNode)); (*l)->next = NULL; } void createlist(Linklist *l,int n) { int i; Linklist s; for (i = 0; i < n; i++) { s = (Linklist)malloc(sizeof(LNode)); cout << "请输入数据:"; cin >> s->data; s->next = (*l)->next; (*l)->next = s; } } void sum(Linklist l1, Linklist l2, Linklist *l3) { int t=0; Linklist p=l1->next, q = l2->next, j=(*l3),m; while (p&&q) { if ((p->data + q->data) >= 10) { m = (Linklist)malloc(sizeof(LNode)); m->data = (p->data + q->data) % 10+t; m->next =j->next; j->next = m; j = m; t = 1; } else { m = (Linklist)malloc(sizeof(LNode)); m->data = (p->data + q->data) % 10 + t; m->next = j->next; j->next = m; j = m; t = 0; } p = p->next; q = q->next; } } void visit(Linklist l1, Linklist l2, Linklist l3) { Linklist p; cout << "输入:"; for (p = l1->next; p; p = p->next) { cout << p->data; if(p->next!=NULL) cout << "->"; } cout << "+"; for (p = l2->next; p; p = p->next) { cout << p->data; if (p->next != NULL) cout << "->"; } cout << endl; cout << "输出"<<"="; for (p = l3->next; p; p = p->next) { cout << p->data; if (p->next != NULL) cout << "->"; } } void reverseprint(Linklist l) { if (l->next != NULL) { reverseprint(l->next); cout << l->next->data; } } int main(void) { Linklist l1, l2, l3; Initlist(&l1); Initlist(&l2); Initlist(&l3); createlist(&l1,3); createlist(&l2,3); sum(l1, l2, &l3); reverseprint(l3); system("pause"); return 0; }
抱歉老师,任务十八
#include<iostream> using namespace std; #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define MAX_VERTEX_NUM 20 typedef int InfoType; typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; InfoType weight; }ArcNode; typedef char VertexType; typedef struct VNode { VertexType data; ArcNode *firstarc; }VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum, arcnum; }ALGraph; typedef int ElemType; typedef struct { ElemType *base; ElemType *top; int stacksize; }SqStack; typedef int Status; #define OK 1 #define ERROR 0 Status InitStack(SqStack &S) { S.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if (!S.base) { return ERROR; } S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } Status Push(SqStack &S, ElemType e) { if (S.top - S.base >= S.stacksize) { S.base = (ElemType *)realloc( S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType) ); if (!S.base) { return ERROR; } S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK; } ElemType Pop(SqStack &S) { if (S.base == S.top) { exit(1); } return *--S.top; } Status StackEmpty(SqStack S) { if (S.base == S.top) { return 1; } return 0; } void CreateALGraph(ALGraph &G) { cout << "顶点数目:"; cin >> G.vexnum; cout << "弧边数目:"; cin >> G.arcnum; for (int i = 0; i < G.vexnum; i++) { ArcNode *p_Tail; cout << "Seq " << i << "th:"; cin >> G.vertices[i].data; G.vertices[i].firstarc = NULL; int _do; cout << "Do:"; cin >> _do; if (!_do) { continue; } else { cout << "依次输入以" << G.vertices[i].data << "为尾的弧的弧头与权值:" << endl; for (int j = 0; j < _do; j++) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->nextarc = NULL; cin >> p->adjvex; cin >> p->weight; if (!G.vertices[i].firstarc) { G.vertices[i].firstarc = p; p_Tail = p; } else { p_Tail->nextarc = p; p_Tail = p; } } } } } //ve初始化 int ve[MAX_VERTEX_NUM]; Status IfNecessaryPath(ALGraph G,SqStack &T) { SqStack S; InitStack(S); InitStack(T); int Indegree[MAX_VERTEX_NUM] = {0}; for (int i = 0; i < G.vexnum; i++) { if (!G.vertices[i].firstarc) { continue; } else { ArcNode *p = G.vertices[i].firstarc; while(p) { Indegree[p->adjvex]++; p = p->nextarc; } } } //0入度的节点入栈S for (int i = 0; i < G.vexnum; i++) { if (!Indegree[i]) { Push(S,i); } } int count = 0; while (!StackEmpty(S)) { int j = Pop(S); //j号顶点入栈T并且计数 Push(T,j); count++; for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { if (--Indegree[p->adjvex] == 0) { Push(S, p->adjvex); } //MAX{ } if (ve[j] + p->weight > ve[p->adjvex]) { ve[p->adjvex] = ve[j] + p->weight; } } } if (count < G.vexnum) { return ERROR; } else { return OK; } } int vl[MAX_VERTEX_NUM]; Status CalculatePath(ALGraph G) { SqStack T; if (!IfNecessaryPath(G, T)) { return ERROR; } //初始化事件最晚时间 for (int i = 0; i < G.vexnum; i++) { vl[i] = ve[G.vexnum-1]; } //按拓扑排序的逆序求顶点 while (!StackEmpty(T)) { int j = Pop(T); for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { int k = p->adjvex; int w = p->weight; //MIN{ } if (vl[k] - w < vl[j]) { vl[j] = vl[k] - w; } } } for (int j = 0; j < G.arcnum; j++) { for (ArcNode *p = G.vertices[j].firstarc; p; p = p->nextarc) { int k = p->adjvex, w = p->weight; int el = vl[k]-w, ee = ve[j]; if (ee == el) { cout <<"<"<< G.vertices[j].data <<"," << G.vertices[k].data <<">"<< endl; } } } } int main(void) { ALGraph G; CreateALGraph(G); CalculatePath(G); return 0; } #include<iostream> using namespace std; #define MAX_VERTEX_NUM 20 typedef int VRType; typedef struct ArcCell { VRType adj; int *info; }ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef char VertexType; typedef struct { VertexType vexs[MAX_VERTEX_NUM]; AdjMatrix arcs; int vexnum, arcnum; }MGraph; int LocateVex(MGraph G, VertexType V) { int i; G.vexs[G.vexnum] = V; for (i = 0; G.vexs[i] != V; i++); return i; } void CreateMGraph(MGraph &G) { cout << "顶点:"; cin >> G.vexnum; cout << "边数:"; cin >> G.arcnum; cout << "构造顶点向量" << endl << "请依次输入顶点:"; for (int i = 0; i < G.vexnum; i++) { cin >> G.vexs[i]; } for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { G.arcs[i][j] = { 0,NULL }; } } cout << "邻接矩阵赋值" << endl; for (int k = 0; k < G.arcnum; k++) { cout << "依次输入第" << k + 1 << "条边的弧尾、弧头、权值:"; VertexType a, b; VRType w; cin >> a >> b >> w; int i = LocateVex(G, a); int j = LocateVex(G, b); G.arcs[i][j].adj = w; } } int SimpleLine(MGraph G, int m_i, int m_j, int k) { //最终矩阵 int Matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //过渡矩阵 int TMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; cout << "单位矩阵:" << endl; for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { if (i == j) { TMatrix[i][j] = 1; } else { TMatrix[i][j] = 0; } cout << TMatrix[i][j] << " "; } cout << endl; } for (int n = 0; n < k; n++) { //矩阵的相乘次数 cout << n + 1 << "次:" << endl; for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { int temp = 0; for (int k = 0; k < G.vexnum; k++) { temp += TMatrix[i][k] * G.arcs[k][j].adj; } Matrix[i][j] = temp; cout << Matrix[i][j] << " "; } cout << endl; } for (int i = 0; i < G.vexnum; i++) { for (int j = 0; j < G.vexnum; j++) { TMatrix[i][j] = Matrix[i][j]; } } cout << endl; } return Matrix[m_i][m_j]; } void IFHavePath(MGraph &G,int i,int j) { for (int n = 1; n <= G.arcnum; n++) { if (SimpleLine(G, i, j, n)) { cout << "长度为" << n << "的路径有。"<<endl; } else { cout << "长度为" << n << "的路径没有。"<<endl; } } } int main(void) { MGraph G; CreateMGraph(G); int i, j; cin >> i >> j; IFHavePath(G,i,j); return 0; }没有及时提交
不好意思老师,2017-12-25有关哈夫曼的练习题纸质作业忘记提交。
抱歉老师,任务十二没有及时提交
(1) 求出以T为根或子树的结点个数: int countNodes(tnode<T> *t) { if (t->left == NULL && t->right == NULL) return 1; if (t->left == NULL) return 1 + countNodes(t->right); if (t->right == NULL) return countNodes(t->left) + 1; return countNodes(t->left) + countNodes(t->right) + 1; } (2) 求出以T为根或子树的高度: int depth(T) { if(!T) depthval=0; else { depthLeft=depth(T->lchild); depthRight=depth(T->rchild); depthval=(depthLeft>depthRight?depthLeft:depthRight); } return depthval; } 2 bool isBalance3(TreeNode *root, int &height) { if (root == NULL) { height = 0; return true; } int leftHeight = 0, rightHeight = 0; bool leftRet = isBalance3(root->left, leftHeight); bool rightRet = isBalance3(root->right, rightHeight); height = max(leftHeight, rightHeight) + 1; if (abs(leftHeight - rightHeight) > 1) return false; return leftRet && rightRet; } 3 void Algo(BiTree T) { if(T) { if(T->lchild) if((T->lchild->data)&&(T->lchild->data,T->data)) { printf(“(”); Algo(T->lchild); printf(“)”); } else Algo(T->lchild); } printf(“%c”,T->data); if(T->rchild) { if((T->rchild->data)&&(Trchild->data,T->data)) { printf(“(”); Algo(T->rchild); printf(“)”); } else Algo(T->rchild); } }
抱歉老师,作业没有及时提交
任务九
1、 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE 100 typedef int ElemType; typedef struct { ElemType *base; int top1; int top2; }SNode; //初始化 bool init(SNode *S) { S->base=(ElemType *)malloc(MAXSIZE*sizeof(ElemType)); if (!S) { return false; } S->top1=0; S->top2=MAXSIZE-1; } //入栈 bool inserve(SNode *S,int i,ElemType e) { if (S->top1==S->top2+1) { return false; } if (i==1) { S->base[S->top1]=e; S->top1++; } else { if (i==2) { S->base[S->top2]=e; S->top2--; } else return false; } return true; } //出栈 bool del(SNode *S,int i,ElemType *e) { if (i==1) { S->top1--; *e=S->base[S->top1]; } else { if (i==2) { S->top2++; *e=S->base[S->top2]; } else return false; } return true; } 2、 //队空:length==0; 队满:length==m; #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE 10 typedef int QElemType; typedef struct { QElemType SQ[MAXSIZE]; int rear; int length; }QNode; //初始化 bool init(QNode *Q) { Q->rear=0; Q->length=0; return true; } //插入 bool inserve(QNode *Q,QElemType e) { if (Q->length==MAXSIZE) { return false; } if (Q->length==0) { Q->SQ[Q->rear]=e; Q->length++; } else { Q->rear=(Q->rear+1)%MAXSIZE; Q->SQ[Q->rear]=e; Q->length++; } return true; } //删除 bool del(QNode *Q,QElemType *e) { if (Q->length==0) { return false; } *e=Q->SQ[(Q->rear+MAXSIZE-Q->length+1)%MAXSIZE]; Q->length--; return true; }任务八
3、char 4、完成将队列的元素逆置 5、(1)1423 (2)3214 (3)3412 实践作业 1、 void test(int *sum) { SqStack S; int x; InitStack(&S); while (1) { scanf("%d",&x); if (x==0) { *sum=0; break; } else { Push(&S, x); } } while (S.base!=S.top) { printf("%d",*sum); *sum+=Pop(&S); } printf("%d",*sum); DestroyStack(&S); } 2、 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define STACK_INIT_SIZE 5 typedef int ElemType; typedef struct { ElemType *base; ElemType *top0; ElemType *top1; }SqStack; //初始化 bool InitStack(SqStack *tws) { tws->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if (!tws->base) { return false; } tws->top0=tws->base; tws->top1=tws->base+(STACK_INIT_SIZE-1); return true; } //插入 bool Push(SqStack *tws,int i,ElemType x) { if (tws->top0==tws->top1+1) { return false; } if (i==0) { *(tws->top0)=x; tws->top0++; } else { if (i==1) { *(tws->top1)=x; tws->top1--; } else { return false; } } return true; } //删除 bool Pop(SqStack *tws,int i) { if (tws->top0==tws->base&&tws->top1==tws->base+STACK_INIT_SIZE-1) { return false; } if (i==0) { tws->top0--; } else { if (i==1) { tws->top1++; } else { return false; } } return true; } //销毁 void DestroyStack(SqStack *tws) { free(tws->base); tws->base=NULL; } 3、 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char ElemType; typedef struct Stack { ElemType *base; ElemType *top; int stacksize; } SqStack; //初始化 bool InitStack(SqStack *S) { S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if (!S->base) { return false; } S->top=S->base; S->stacksize=STACK_INIT_SIZE; return true; } //入栈 bool Push(SqStack *S, ElemType e) { ElemType *base; if (S->top-S->base>=S->stacksize) { base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); if (!base) { return false; } S->base=base; S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top=e; ++S->top; return true; } //出栈 bool Pop(SqStack *S, ElemType *e) { if (S->top==S->base) { return false; } --S->top; *e=*(S->top); return true; } //销毁 void DestroyStack(SqStack *S) { free(S->base); S->base=NULL; } //判断 bool fun(char *p) { SqStack S; int i=0; char ch; InitStack(&S); while (p[i]!='\0') { if (p[i]=='('||p[i]=='['||p[i]=='{') { Push(&S,p[i]); } else { if (p[i]==')'||p[i]==']'||p[i]=='}') { if (Pop(&S, &ch)) { switch (p[i]) { case ')':if(ch != '(') { Push(&S,ch); Push(&S,p[i]); } break; case ']':if(ch != '[') { Push(&S,ch); Push(&S,p[i]); } break; case '}':if(ch != '{') { Push(&S,ch); Push(&S,p[i]); } break; } } } } i++; } if (S.base==S.top) { DestroyStack(&S); return true; } DestroyStack(&S); return false; } 4、 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char ElemType; typedef struct Stack { ElemType *base; ElemType *top; int stacksize; } SqStack; //初始化 bool InitStack(SqStack *S) { S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if (!S->base) { return false; } S->top=S->base; S->stacksize=STACK_INIT_SIZE; return true; } //入栈 bool Push(SqStack *S, ElemType e) { ElemType *base; if (S->top-S->base>=S->stacksize) { base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); if (!base) { return false; } S->base=base; S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top=e; ++S->top; return true; } //出栈 bool Pop(SqStack *S, ElemType *e) { if (S->top==S->base) { return false; } --S->top; *e=*(S->top); return true; } //销毁 void DestroyStack(SqStack *S) { free(S->base); S->base=NULL; } bool fun(char *p) { SqStack S; int i=0; char ch; InitStack(&S); while (p[i]!='@') { Push(&S, p[i]); i++; } for (i = 0; S.base!=S.top; i++) { Pop(&S, &ch); if (p[i]!=ch) { DestroyStack(&S); return false; } } DestroyStack(&S); return true; }
老师由于电脑网络和系统的原因作业没有提交上去,现补交
任务 未提交
7-杨凯静
0
粉丝
0
关注
@ 我的
评论 1
赞
收藏
我的课程
我的任务
搜索
未提交 7
已完成
已完结
任务八 第三章作业题续 今天截止 未提交
展开任务 交作业
2016级数据结构
取消 提交
任务九 第三章编程作业(提交可执行代码到雪梨) 今天截止 未提交
展开任务 交作业
2016级数据结构
任务06_3 改错题 2017-11-17 截止 未提交
展开任务 交作业
2016级面向对象程序设计(C++)
任务06_2 写出构造、析构顺序 2017-11-17 截止 未提交
展开任务 交作业
2016级面向对象程序设计(C++)
任务06_1 统计箱子的数量 2017-11-17 截止 未提交
展开任务 交作业
2016级面向对象程序设计(C++)
任务七 使用场景法和状态转换法设计测试用例 2017-11-20 截止 未提交
展开任务 交作业
软件测试基础2016级7、8班
« 1 2 »
地址: 石家庄市裕华区南二环东路20号 ® 石家庄佳诚网络技术有限公司
冀ICP备12011972号-4 站长统计
×
作业预览
所属任务: 任务八 第三章作业题续
7-杨凯静
#include
老师我申请加入同步课没通过
抱歉老师没有及时提交作业,6班张思嘉2016011622
1.O(log2 n) count = log(n,2)-2
2.int num(sqlist &L,elemtype i){
int x = 0;
for(int d = 0 ;;d++){
x++;
if(L.elem[d] == i){
return x;
}
}
}
3.int length(List L){
if(L->next){
return (L+1)->next;
}
else
return 1;
}
时间复杂度O(n) 空间复杂度为O(1)
第一次作业
1.O(log(n)),count=log(n,2)-2;
2.int x,i=0;string s1;cin<<x;while(x!=s1[i]){i++;if(i==s1.length())break;}O(n)次
3.int length(Node*h){h==Null ? return 1:return 1+length(h->nex)t;}时间:O(n)次,空间:n