Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213183
  • 博文数量: 35
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 14:27
文章分类

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-03-11 15:52:11

/**设计一个算法,将一个单链表链接到另一个单链表的尾部**/
#include "iostream"
using namespace std;
template
struct Node
{
 Node(const T& e=T(),Node* next=NULL):element(e),link(next){}
  T element;
     Node* link;
};
template
class ANode
{
public:
 ANode();
 bool NewNode(T e);
 void printList();
 void DeleteAnode(ANode p);
 Node*  head; 
};
template
ANode::ANode()
{
 head=new Node(0,NULL);
}
template
bool ANode::NewNode(T e)
{
Node* temp=new Node(e,NULL);
if(head->link==NULL)

head->link=temp;
}
else
{
Node* y=head;
for(;y->link!=NULL;y=y->link);
y->link=temp;
}
return (temp!=NULL);
}
template
void ANode::printList()
{
Node* temp;
for(temp=head->link;temp!=NULL;temp=temp->link)
{
 cout<element<<',';
}
cout<}
template
void ANode::DeleteAnode(ANode p)
{
Node* temp;
for(temp=head;temp->link!=NULL;head=head.link)
{
 temp=head;
 temp->link=null;
}
}
template
ANode link(ANode a,ANode b) 
{
ANode x;
Node* temp;
for(temp=a.head->link;temp!=NULL;temp=temp->link)
x.NewNode(temp->element);
for(temp=b.head->link;temp!=NULL;temp=temp->link)
x.NewNode(temp->element);
return x;
}
int main()
{
ANode x,y,z;
x.NewNode(3);
x.NewNode(9);
x.NewNode(1);
x.NewNode(6);
y.NewNode(2);
y.NewNode(3);
y.NewNode(11);
z=link(x,y);
z.printList();
return 0;
}
阅读(1309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~