1、stack:stack()count=0;bool stack:empty()constreturn count=0;error_code stack:get_top(DATA_TYPE &x)constif(empty()return underflow;elsex=datacount-1;return success;push(const DATA_TYPE x)if(full()return overflow;datacount=x;count+;pop()count-;full()constreturn count=MAXLEN;void main()stack S;int N,d;c
2、outNd;if(N=0)输出转换结果:Nwhile(N)S.push(N%d);N=N/d;while(!S.empty() S.get_top(N);N;S.pop();S.get_top(x);x;测试数据:N=1348 d=8运行结果:2.给出顺序队列的类定义和函数实现,并利用队列计算并打印杨辉三角的前n行的内容。(n=8)实验原理:杨辉三角的规律是每行的第一和最后一个数是1,从第三行开始的其余的数是上一行对应位置的左右两个数之和。因此,可用上一行的数来求出对应位置的下一行内容。为此,需要用队列来保存上一行的内容。每当由上一行的两个数求出下一行的一个数时,其中的前一个便需要删除,而新求
3、出的数就要入队。程序清单:success,underflow,overflowclass queue queue(); error_code get_front(DATA_TYPE & error_code append(const DATA_TYPE x); error_code serve(); int front,rear;queue:queue()rear=0;front=0;bool queue: return (front%MAXLEN=rear%MAXLEN);error_code queue:get_front(DATA_TYPE &x=datafront%MAXLEN;app
4、end(const DATA_TYPE x)datarear%MAXLEN=x;rear+;serve()front+;return(rear+1)%MAXLEN=front);queue Q;int num1,num2;int i=0;1Q.append(1);num1=0;num2=1;for(i=0;i=7;i+)int j=0;int k=0;for(j=0;j=i;j+)Q.get_front(num2);Q.serve();num1+num2data=x;next=top;top=s;x=top-data;LNode *u=new LNode;u=top;top=top-next;
5、delete u;linkstack()empty()pop();linkstack L;int n;请任意输入一个整数n:n;for(int i=1;=n;L.push(i);L.empty()L.get_top(i);L.pop();n=9 i=1实验二:单链表理解线性表的链式存储结构。熟练掌握动态链表结构及有关算法的设计。根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。实验任务:在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。1.实验数据:链表元素为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。给
6、出了要插入的条件,但没有给定插入位置。因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号。程序清单:typedef struct snode int data; struct snode *next;node;enum error_codearrange_error,success;class list list(); void create2(); int length() const; error_code get_element(const int i,int &x) const; error_code insert(const int &x); error_code delete
7、_element(const int i); node *locate(const int x) const; node *get_head()return head; void print(); node *head;list:list() head=new node; head-next=NULL; count=0;void list:create2() int x; node *p=head; node *s; cout while(x!=-1) s=new node; s- s- p-next=s; p=s; cout int list:length() const return co
8、unt;error_code list:get_element(const int i,int &x) const int j=1; node *p=head- while(p!=NULL&j!=i) p=p- j+; if(p=NULL) return arrange_error; x=p- return success;node *list:locate(const int x) const node *p=head-=NULL) if(p-data=x) return p; return NULL;insert(const int &x) node *q=head; node *p=he
9、ad-p-data s- q- count+; elsenext=q- q-delete_element(const int i) node *u; node *p=head; int j=0; while(j!=i-1&p! if(icount) u=p- p-next=u- delete u; count-;print() cout node *pb=B.get_head(); node *pc=C.get_head(); for(int i=0;pa!=NULL;i+,pa=pa-next) u=new node; u-data=pa- if(i%2=0) pb-next=u; pb=p
10、b- else pc- pc=pc- pb- list A,B,C; int x,y,z; A.create_R(); A.divide(B,C);原表: A.output();奇数表: B.output();偶数表: C.output();第一组数据:链表元素为 (1,2,3,4,5,6,7,8,9,10,20,30,40,50,60) 第二组数据:链表元素为 (10,20,30,40,50,60,70,80,90,100)3.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。设置两个指针怕,pa,pb分别依次指示A,B表中的元素,其初始值分别为A.head-next和B
11、.head-next。在pa,pb均非空时,根据其值的大小关系可能有如下三种情况。 (1).pa-data=pb-data:搜索到公共元素,应在C表表尾插入一个结点,其值为pa-data,然后继续A表中下一个元素的搜索,即pa=pa-next,同时pb也往后移。 (2). pa-datapb-表明A表中这一元素可能在B表当前元素的后面,因此要往B表的后面搜索,故而执行pb=pb-next,然后继续搜索。 (3). pa-表明A中这一元素在B中不存在,因而执行pa=pa-next以继续对A表中下一个元素的判断。 反复执行上述比较,直到pa,pb至少有一个为空为止。此时,剩余的非空部分没有所需要的公共元素,因而搜索结束。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1