1、数据结构第三章到第九章练习题答案第三章 栈和队列3.10借助栈实现单链表上的逆置运算。/ stdafx.h #include #include #includetime.husing namespace std;/结点类struct LinkNode int data; LinkNode *link; LinkNode(const int& item, LinkNode *ptr = NULL) data=item; link=ptr; LinkNode( LinkNode *ptr = NULL)link=ptr;/list.h#pragma once/带附加头结点的单链表class Lis
2、tpublic: List(void);/构造函数 List(const int& x);/构造函数 List(void);/析构函数 void input(int n);/输入 void output(void);/输出 void makeEmpty(void);/清空 void inverse(void);/逆转函数protected: LinkNode* first;/链表头指针;/ stack.h#pragma once#include .list.h class stackpublic: stack(void); stack(void);protected: LinkNode* to
3、p; friend class List;public: void Push(const int& x); bool Pop(); bool IsEmpty(void)return(top=NULL)?true:false;/ list.cpp#include StdAfx.h#include .list.h#using List:List(const int& x) first= new LinkNode(x);List:List(void) first= new LinkNode;List:List(void) makeEmpty();void List:input(int n) Link
4、Node * newNode; first=new LinkNode; for(int i=0;ilink=first-link; first-link=newNode; void List:output(void) LinkNode *current=first-link; while(current!=NULL) coutdatalink; coutlink!=NULL) p=first-link; first-link=p-link; delete p; /逆转函数void List:inverse(void) stack s; LinkNode *p=first-link,*q; wh
5、ile(p!=NULL) s.Push(p-data); p=p-link; p=first-link; while(!s.IsEmpty() q=s.top; p-data=q-data; p=p-link; s.Pop(); / Stack.cpp#include StdAfx.h#include .stack.h#using stack:stack(void): top(NULL) stack:stack(void)/入栈void stack:Push(const int& x) top=new LinkNode(x,top);/出栈bool stack:Pop() if(IsEmpty
6、()=true) return false; LinkNode *p=top; top=top-link; delete p; return true;/Main.cpp#include stdafx.h#include .list.h#using using namespace System;int _tmain() srand(time(NULL); List l; /调用输入函数 l.input(10); /调用输出函数 l.output(); cout调用逆转函数endl; l.inverse(); /调用输出函数 l.output(); return 0;3.12编写一个算法,将一个
7、非负的十进制整数N转换为另一个基为B的B进制数。/ stdafx.h #include #include using namespace std;struct LinkNode int data; LinkNode *link; LinkNode(const int& item, LinkNode *ptr = NULL) data=item; link=ptr; LinkNode( LinkNode *ptr = NULL)link=ptr;/stack.h#pragma onceclass stackpublic: stack(void); stack(void);void Push(co
8、nst int& x); bool Pop(int& x); bool IsEmpty(void)return(top=NULL)?true:false;protected: LinkNode* top;/stack.cpp#include StdAfx.h#include .stack.h#using stack:stack(void): top(NULL) stack:stack(void) void stack:Push(const int& x) top=new LinkNode(x,top);bool stack:Pop(int& x) if(IsEmpty()=true) retu
9、rn false; LinkNode *p=top; top=top-link; x=p-data; delete p; return true;/main.cpp#include stdafx.h#include .stack.h#using using namespace System;int _tmain() int n, m, temp,yu; coutn; coutm; stack l; while(n!=0 & m!=0) temp=n%m; n=n/m; l.Push(temp); while(!l.IsEmpty() l.Pop(yu); coutyu; coutendl; r
10、eturn 0;3.21试编写一个算法,求解最大公因数问题:在求两个正整数m和n的最大公因数常常使用辗转相除法,反复计算直到余数为零为止。其递归定义为:/ stdafx.h #include #include #include using namespace std;int f(int n1,int n2);/main.cpp#include stdafx.h#using using namespace System; int _tmain() int n1,n2; coutEnter the first positive number: n1; coutEnter the second po
11、sitive number: n2; if(n10 & n20) cout n1 和 n2 的最大公约数是: f( n1, n2) endl; else cout非法数据endl; return 0;int f(int n1,int n2) int t; while(n2!=0) t= n1 % n2; n1= n2; n2= t; return n1;3.23假设以数组Qm存放循环队列中的元素,同时设置一个标志tag,以tag=0和tag=1来区别在对头指针(front)和对尾指针(rear)相等时,队列状态为“空”还是“满”。试编写与此结构相应的插入(EnQueue)和删除(DeQueue
12、)算法。/ stdafx.h#include #include #includeusing namespace std;/SeqQueue.h#pragma onceclass SeqQueuepublic: SeqQueue(int size=10); SeqQueue(void);protected: int rear,front,tag; int *element; int maxSize;public: bool EnQueue(const int& x); bool DeQueue(void); bool IsEmpty(void)return (front=rear & tag=0
13、)?true:false; bool IsFull(void)return (front=rear & tag=1)?true:false; friend ostream& operator (ostream& os,SeqQueue& Q);/SeqQueue.cpp#include StdAfx.h#include .SeqQueue.h#using SeqQueue:SeqQueue(int size):rear(0),front(0),tag(0),maxSize(size) element=new intmaxSize;SeqQueue:SeqQueue(void) delete e
14、lement;bool SeqQueue:EnQueue(const int& x) if(IsFull()=true) return false; elementrear=x; rear=(rear+1) % maxSize; tag=1; return true;bool SeqQueue:DeQueue(void) if(IsEmpty()=true) return false; front=(front+1) % maxSize; tag=0; return true;ostream& operator (ostream& os,SeqQueue& Q) int num; if(Q.f
15、ront=Q.rear & Q.tag=1) num=Q.maxSize; else num=(Q.rear-Q.front+Q.maxSize)%Q.maxSize; for(int i=0;inum;i+) os(i+Q.front)%Q.maxSize:Q.element(i+Q.front)%Q.maxSizet; return os;/main.cpp#include stdafx.h#include .SeqQueue.h#using using namespace System;int _tmain()srand(time(NULL); SeqQueue q; coutCall
16、the EnQueue functionendl; for(int i=0;i9;i+) q.EnQueue(rand()%100); coutqendl; coutCall the DeQueue functionendl; q.DeQueue(); q.DeQueue(); coutqendl; return 0;第四章数组、串与广义表4.12若矩阵Am*n中的某一元素Aij是第i行中的最小值,同时又是第j列中的最大值,则称此元素为该矩阵的一个鞍点。假设以二维数组存放矩阵,试编写一个函数,确定鞍点在数组中的位置(若鞍点存放在时),并分析该函数的时间复杂度。/ stdafx.h#includ
17、e #include using namespace std;/main.cpp#include stdafx.h#include#using using namespace System;int _tmain() int A1010,rsize,row,column,maxC,minD,max,min; srand(time(NULL); for(row=0;row10;row+) for (column=0;column10;column+) Arowcolumn=rand()%(100/(10-row)+100/(10-column); coutArowcolumnt; for(rsiz
18、e=0;rsize10;rsize+) minD=0; min=Arsize0; for(column=1;columnArsizecolumn) minD=column; min=ArsizeminD; maxC=0; max=A0minD; for(row=1;row10;row+) if(maxArowminD) maxC=row; max=ArowminD; if(max=min) coutArrayrsize+1minD+1是鞍点endl; return 0;时间复杂度分析:第二个for的嵌套循环是寻找鞍点的主要程序,假设数组Amn,则第一、二个内嵌循环时间代价分别为t1(n)=O(
19、f(n)和 t2(m)=O(g(m),外循环的时间复杂度为t(m)=O(F(m)则整个程序段的时间复杂度为T= t(m)(t1(n)+ t2(m))因为for语句里if后面语句被执行的平均次数为(n-1)+(n-2)+1+0=n/2;所以f(n)=n/2*2+n*3+2=n*4+2;则t1(n)=O(f(n)=O(n),同理得t2(m)=O(g(m)= O(m),而最后的if后面语句被执行的平均次数为(m +1)/2则T=O( (2+2+t1+2+t2+1)m+2+(m+1)/2)=O(maxmn , mm)4.13所谓回文,是指从前向后顺读和从后向前读都一样的不含空白字符的串。例如did,m
20、adamimadam,pop即是回文。试编写一个算法,以判断一个串是否是回文。/ stdafx.h#include #include #includeusing namespace std;/结点类#ifndef stdafx_h#define stdafx_htemplatestruct LinkNode T data; LinkNode *link; LinkNode(const T& item, LinkNode *ptr = NULL) data=item; link=ptr; LinkNode( LinkNode *ptr = NULL)link=ptr;#endif/stack.h
21、#pragma oncetemplateclass CCStackpublic: CCStack(void); CCStack(void);protected: LinkNode* top;public: void Push(const T& x); bool Pop(); bool IsEmpty(void)return(top=NULL)?true:false; T getTop();/stack.cpp#include StdAfx.h#include .cstack.h#using templateCCStack:CCStack(void): top(NULL)templateCCSt
22、ack:CCStack(void)templatevoid CCStack:Push(const T& x) top=new LinkNode(x,top);templatebool CCStack:Pop() if(IsEmpty()=true) return false; LinkNode *p=top; top=top-link; delete p; return true;templateT CCStack:getTop() T x; if(IsEmpty()=true) return NULL; x=top-data; return x;/main.cpp#include stdaf
23、x.h#include .cstack.cpp#using using namespace System;int _tmain() string a; bool temp=true; int i=0; CCStack st; cina; while(ai!=0) st.Push(ai); i+; i=0; while(ai!=0) if(ai=st.getTop() st.Pop(); i+; else temp=false; break; if(temp) cout此字符是回文endl; else cout此字符不是回文endl; return 0;4.15编写一个算法frequency,统
24、计在一个输入字符串中各个不同字符出现的频度。用适当的测试数据来验证这个算法。/ stdafx.h #include #include #includeusing namespace std;/main.cpp#include stdafx.h#using using namespace System;int _tmain() string a; int k,*num; char *A; cina; k=a.length(); A = new chark; num = new intk; frequency(a, A, num,k=0 ); for ( int i = 0; i k; i+ ) coutAi:numi个endl; delete A; delete num; return 0;void frequency( string s, char A , int C , int &k ) int i, j, len = s.length( ); A0 = s0; C0 = 1; /种类数 k = 1; for ( i = 1; i len; i+ ) Ci = 0; for ( i = 1; i len
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1