提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、题目
二、代码解答
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;
}