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

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-03-17 10:01:50

/**单链表中每个结点存放一个字符,设计一个算法,将该单链表按字母、数字和其他字符拆成三个单循环链表(利用原有结点)**/
#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
void append(Node** head,Node** rear,Node** temp)
{
 if((*head)->link==NULL)
 {
    (*rear)=(*temp);
 (*head)->link=(*rear);
 }
 else
 {
 (*rear)->link=*temp;
 (*rear)=(*temp);
 }
 (*temp)=(*temp)->link;
 (*rear)->link=NULL; 
}
template
void split(Node** head,Node** h1,Node** h2,Node** h3)
{
Node* temp;
temp=(*head)->link;
Node* rear1=NULL;
Node* rear2=NULL;
Node* rear3=NULL;
while(temp!=NULL)
{
if(('0'<=temp->element)&&(temp->element<='9'))
{
append(h1,&rear1,&temp);
}
else if(('a'<=temp->element)&&(temp->element<='z')||('A'<=temp->element)&&(temp->element<='Z'))
{
append(h2,&rear2,&temp);
}
else
{
append(h3,&rear3,&temp);
}
}
}
template
void printList(Node* head)
{
Node* temp;
for(temp=head->link;temp!=NULL;temp=temp->link)
{
 cout<element<<',';
}
cout<}

Node* createList()
{
 Node* r;
 Node* p;
 Node* head=new Node('0',NULL);
 char c;
 char e;
 cout<<"input? y/n"< while((c=getchar())=='\n');
 while(tolower(c)!='n')
 {
              cin>>e;
     p=new Node(e,NULL);
     if(head->link==NULL)
      head->link=p;
     else
      r->link=p;
     r=p;
      cout<<"input? y/n"<  while((c=getchar())=='\n');
 }
 return head;
}
int main()
{
Node* head=createList();
printList(head);
Node* h1;
Node* h2;
Node* h3;
h1=new Node('0',NULL);
h2=new Node('0',NULL);
h3=new Node('0',NULL);
split(&head,&h1,&h2,&h3);
printList(h1);
printList(h2);
printList(h3);
return 0;
}

阅读(1242) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~