C++程序设计教程 第3版——习题九11-15

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、题目

请添加图片描述

二、代码解答

1.11-13

代码如下(示例):

11.#include<iostream>
#include<cstring>
using namespace std;
void swap(char*p1,char*p2,char*p3)
{
	char s[80]={0};
	char*f;
	f=s;
	if(strcmp(p1,p2)==1)
	{
		strcpy(f,p1);
		strcpy(p1,p2);
		strcpy(p2,f);
	}
	if(strcmp(p1,p3)==1)
	{
		strcpy(f,p1);
		strcpy(p1,p3);
		strcpy(p3,f);
	}
	if(strcmp(p2,p3)==1)
	{
		strcpy(f,p2);
		strcpy(p2,p3);
		strcpy(p3,f);
	}
}
int main()
{
	char str1[80],str2[80],str3[80];
	char*p1,*p2,*p3;
	p1=str1;
	p2=str2;
	p3=str3;
    cin.getline(str1,80);
	cin.getline(str2,80);
	cin.getline(str3,80);
	swap(p1,p2,p3);
	cout<<str1<<'\n'<<str2<<'\n'<<str3<<endl;
	return 0;
}
12.#include<iostream>
using namespace std;
int main()
{
	char*p[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
	int n;
	cin>>n;
	cout<<p[n-1]<<endl;
	return 0;
}
13.#include<iostream>
#include<iomanip>
using namespace std;
struct node
{
	int data;
	node *next;
};
node *create()
{
	node *p1,*p2=NULL,*head;
	int a;
	head=NULL;
	cout<<"正在创建一条无序列表...\n";
	cout<<"请输入一个正整数,以0结束:";
	cin>>a;
	while(a!=0)
	{
		p1=new node;
		p1->data=a;
		if(head==NULL)
			head=p2=p1;
		else
		{
			p2->next=p1;
			p2=p1;
		}
		cout<<"请输入一个正整数,以0结束:";
		cin>>a;
	}
	if(head!=NULL)
		p2->next=NULL;
	return(head);
}
node *reverse(node *head)
{
	node *p1,*p2,*p3;
	if(head==NULL) return 0;
	if(head->next==NULL) return(head);
	p1=head;
	p2=head->next;
	p3=(head->next)->next;
	do
	{
		p2->next=p1;
		p1=p2;
		p2=p3;
	}while(p3&&((p3=p3->next)||1));
	head->next=NULL;
	head=p1;
	return(head);
}
void print(const node*head)
{
	const node *p;
	p=head;
	cout<<"链表中各节点数据为:";
	while(p!=NULL)
	{
		cout<<setw(4)<<(p->data);
		p=p->next;
	}
	cout<<endl;
}
void free(node*head)
{
	node*p;
	while(head)
	{
		p=head;
		head=head->next;
		delete p;
	}
}
int main()
{
	node *head;
	head=create();
	print(head);
	head=reverse(head);
	print(head);
	free(head);
	cout<<"释放了无序链表"<<endl;
	return 0;
}

14-15

代码如下(示例):

14.#include<iostream>
#include<iomanip>
using namespace std;
struct node
{
	int data;
	node *next;
};
node *insert(node *head,node *p)
{
	node *p1,*p2=NULL;
	if(head==NULL)
	{
		head=p;
		p->next=NULL;
		return(head);
	}
	p1=head;
	while((p->data)>=(p1->data)&&p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if((p->data)<(p1->data))
	{
		p->next=p1;
		if(head==p1) head=p;
		else p2->next=p;
	}
	else
	{
		p1->next=p;
		p->next=NULL;
	}
	return(head);
}
node *sort()
{
	node *p3,*head=NULL;
	int a;
	cout<<"正在创建一条升序链表...\n";
	cout<<"请输入一个整数,以-1结束:";
	cin>>a;
	while(a!=-1)
	{
		p3=new node;
		p3->data=a;
		head=insert(head,p3);
		cout<<"请输入一个整数,以-1结束:";
		cin>>a;
	}
	return(head);
}
void print(const node*head)
{
	const node *p2;
	p2=head;
	cout<<"链表中各节点数据为:";
	while(p2!=NULL)
	{
		cout<<setw(4)<<(p2->data);
		p2=p2->next;
	}
	cout<<endl;
}
int main()
{
	node*head=NULL,*p1;
	p1=sort();
	head=sort();
	print(head);
	insert(head,p1);
	print(head);
	return 0;
}
15.#include<iostream>
#include<iomanip>
using namespace std;
struct node
{
	int data;
	node *next;
};
node *create()
{
	node *p1,*p2=NULL,*head;
	int a;
	head=NULL;
	cout<<"正在创建一条无序列表...\n";
	cout<<"请输入一个正整数,以0结束:";
	cin>>a;
	while(a!=0)
	{
		p1=new node;
		p1->data=a;
		if(head==NULL)
			head=p2=p1;
		else
		{
			p2->next=p1;
			p2=p1;
		}
		cout<<"请输入一个正整数,以0结束:";
		cin>>a;
	}
	if(head!=NULL)
		p2->next=NULL;
	return(head);
}
void print(const node*head)
{
	const node *p;
	p=head;
	cout<<"链表中各节点数据为:";
	while(p!=NULL)
	{
		cout<<setw(4)<<(p->data);
		p=p->next;
	}
	cout<<endl;
}
void swap(node*p1,node*p2)
{
	int t;
	t=(p2->data);
	(p2->data)=(p1->data);
	(p1->data)=t;
}
node*sort(node*head)
{
	node*p1,*p2,*p3;
	p1=head;
	for(;(p1->next)!=NULL;p1=p1->next)
	{
		p2=p1;
		for(p3=p2->next;p3!=NULL;p3=p3->next)
			if((p3->data)<(p2->data)) p2=p3;
		if(p2!=p1) swap(p1,p2);
	}
	return(head);
}
int main()
{
	node*head,*p;
	head=create();
	p=sort(head);
	print(p);
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值