教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 小学教育 >

C++教材练习题参考答案(3)

来源:网络收集 时间:2026-09-06
导读: for(p=s;ps+3;p++) { coutp-num ; cout'\t'p-ave; cout'\t'p-CPPmid ; cout'\t'p-CPPend; coutendl; } } 中国铁道建设出版 4.[程序如下] struct link { int data; link*next; }; int sum(link *head) { } link *p;

for(p=s;p<s+3;p++)

{

cout<<p->num ;

cout<<'\t'<<p->ave;

cout<<'\t'<<p->CPPmid ;

cout<<'\t'<<p->CPPend;

cout<<endl;

}

}

中国铁道建设出版

4.[程序如下]

struct link

{

int data;

link*next;

};

int sum(link *head)

{

} link *p; int s=0; p=head->next; while(p) { } s+=p->data; p=p->next; return s;

5.[程序如下]

#include<iostream.h>

struct node

{

int data;

node *next;

};

node *create( )

{

node *head ; //头指针

node *p , *pend ;

int a;

cout<<"请输入数据(为零表示结束输入):" ;

cin>>a;

head=0 ;

while(a!=0)

{

p=new node ;

p->data=a ;

if(head==0)

{

head=p ;

pend=p ;

中国铁道建设出版

else { pend->next=p ; pend=p ; }

cout<<"请输入数据(为零表示结束输入):" ;

cin>>a;

}

if(head) pend->next=0 ;

return head ;

}

node * invert(node *head)

{

node *p,*q;

p=head->next;

if(p!=NULL)

{

head->next=NULL;

do

{

q=p->next;

p->next=head;

head=p;

p=q;;

}while(p!=NULL);

}

return head;

}

void print(node *head )

{

if(head==0) { cout<<" 链表为空!\n" ; return ; }

node *p=head ;

cout<<"链表上各个结点的值为:\n";

while(p!=0)

{

cout <<p-> data<<'\t' ;

p=p->next ;

}

}

void release(node *head )

中国铁道建设出版

if(head==0) { cout<<" 链表为空!\n" ; return ; }

node *p ;

while( head )

{

}

}

void main( )

{

node *head;

head=create( );

print(head);

head=invert(head);

cout<<"\n逆置后";

print(head);

release(head);

}

6.[程序如下]

#include <iostream.h>

struct Node

{

int x; //围成一圈时,人的序号

Node *next;

};

Node * DelNode(Node *head, int m) //依次输出环形链表中凡报到m者的序号 { Node *p;

int count; if(head==NULL) return head;;

while( head != head->next) //直到链表上只有一个结点为止

{

count=0; while(count < m-2) { count++; head = head->next; p=head; head=head->next ; delete p; cout<<"\n结点空间释放完毕!"; } p=head->next; //删除p所指向的结点 head->next = p->next;

C++教材练习题参考答案(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/43211.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)