文档介绍:编辑版word
页脚下载后可删除,如有侵权请告知删除!
编辑版word<br****题1 结点数据之和:
#include<>
struct Node{
int data;
Node *next;
};
int sum(Node* head)
{
int s=0;
while(head){
s+=head->data;
if(head->next)cout<<head->data<<"->";
else cout<<head->data<<endl;
head=head->next;
}
return s;
}
void main()
{
Node node[10],*h=node;
for(int i=0;i<10;i++){//建立链表
node[i].data=i+1;
if(i==9)node[i].next=0;
else node[i].next=&node[i+1];
}
cout<<"链表为:";
int s=sum(h);
cout<<"各结点的数据域之和是:"<<s<<endl;
}<br****题2 合并链表:
#include<>
struct Node{
int data;
Node *next;
};
void print(Node* head)
{
while(head){
if(head->next)cout<<head->data<<"->";
else cout<<head->data<<endl;
head=head->next;
}
}
Node* merge(Node* head1,Node* head2)
编辑版word
页脚下载后可删除,如有侵权请告知删除!
编辑版word
{
Node* head,*p;
if(head1->data<head2->data){
head=head1;
head1=head1->next;
}
else{
head=head2;
head2=head2->next;
}
p=head;
while(head1&&head2){
if(head1->data<head2->data){
p->next=head1;
head1=head1->next;
}
else{
p->next=head2;
head2=head2->next;
}
p=p->next;
}
if(head1)p->next=head1;
else if(head2)p->next=head2;
return head;
}
void main()
{
Node node1[10],*h1=node1,*h;
Node node2[10],*h2=node2;
for(int i=0;i<10;i++){//建立链表
node1[i].data=i+1;
node2[i].data=2*i;
if(i==9){
node1[i].nex