您的位置:首页 > 编程语言 > C语言/C++

单链表数据中含有数字字符、字母字符、其他字符三种 。 构造3个循环链 ,使得循环链只有一种字符 。但必须要使用原来单链的结点空间作为三个表的结点空间

2013-10-17 17:23 435 查看
程序功能:

//      单链表数据中含有数字字符、字母字符、其他字符三种

//      构造3个循环链 ,使得循环链只有一种字符
//      但必须要使用原来单链的结点空间作为三个表的结点空间

//程序名:class.h
//      程序功能:
//      单链表数据中含有数字字符、字母字符、其他字符三种
//      构造3个循环链 ,使得循环链只有一种字符
//      但必须要使用原来单链的结点空间作为三个表的结点空间
//          作者:吴雨羲
//          日期:2013.10.17
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
using namespace std;
//定义单链表的结点结构
struct Node
{
char data;
Node *next;
};
//定义单链表类
class List
{
public:
List();//定义构造函数
~List(){};//析构函数
void Createlist();//尾插入建链函数
void List::Sort(int &z,int &s,int &q);//排序函数
void Cut(Node* &h1,Node* &h2,Node* &h3,int z,int s,int q);//循环链函数
void Print(Node*t);//输出函数
private:
Node *first;//表头指针
};


//程序名:doclass.cpp
//      程序功能:
//      单链表数据中含有数字字符、字母字符、其他字符三种
//      构造3个循环链 ,使得循环链只有一种字符
//      但必须要使用原来单链的结点空间作为三个表的结点空间
//          作者:吴雨羲
//          日期:2013.10.17
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
#include<ctype.h>
#include"class.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////////
//  构造函数
//  函数功能:定义一个表头节点无初值
//函数参数:无
//参数返回值:无
List::List()
{
first=new Node;
first->next=0;
}
//////////////////////////////////////////////////////////////////////////////
//  头插入建链函数
//  函数功能:以头插入方式建立有n个节点的链表
//函数参数:无
//
//参数返回值:无
//
void List::Createlist()
{
int n;
cout<<"输入序列长度"<<endl;
cin>>n;
cout<<"输入序列(数字、字母、其他字符的混合序列)"<<endl;
Node*temp=new Node,*p;
temp=first;
int i;
//先用char数组保持序列,才能一次过输入,不然p->data只获取一个字符
char* c=new char
;
for(i=0;i<n;i++)
cin>>c[i];

for(i=0;i<n;i++)
{
p=new Node;
p->data=c[i];
p->next=0;
temp->next=p;
temp=p;
}

}
//////////////////////////////////////////////////////////////////////////////
//  排序函数
//  函数功能:找同类元素以头插入的方式归类  获取各元素的个数
//函数参数:
//       z 字母字符个数
//       s 数字字符个数
//       q 其他字符个数
//
//参数返回值:无
//
void List::Sort(int &z,int &s,int &q)
{
z=s=q=0;
Node *head1=first,*head2=first,*head3=first;
Node *r;
while(head1->next!=0)
{
r=head1;
head1=head1->next;
//计算共有几个元素,省掉一个循环
q++;
if(isalpha(head1->data))
{
r->next=head1->next;
head1->next=first->next;
first->next=head1;
head1=r;
z++;
//防止首字符就是符合条件的字符导致循环不能继续
if(r==first)
head1=first->next;
}
}
while(head2->next!=0)
{
r=head2;
head2=head2->next;
if(isdigit(head2->data))
{
r->next=head2->next;
head2->next=first->next;
first->next=head2;
head2=r;
s++;
//同上
if(r==first)
head2=first->next;
}
}
q=q-s-z;
}
//////////////////////////////////////////////////////////////////////////////
//  循环链函数
//  函数功能:根据各元素的个数断链成循环链
//函数参数:
//       z 字母字符个数
//       s 数字字符个数
//       q 其他字符个数
//      h1 数字链链尾地址
//      h2 字母链链尾地址
//      h3 其他链链尾地址
//参数返回值:无
//
void List::Cut(Node* &h1,Node* &h2,Node* &h3,int z,int s,int q)
{
char t;
h1=h2=h3=0;
int i;
Node *p=first->next;

if(s)//判断各元素的个数分情况
{
h1=p;
for(i=1;i<s;i++)
{  t=p->data;
p=p->next;
}
h2=p->next;
p->next=h1;
h1=p;
}
if(z)
{
//判断各元素的个数分情况
if(s==0)
h2=first->next;

p=h2;
for(i=1;i<z;i++)
{ t=p->data;
p=p->next;
}
h3=p->next;
p->next=h2;
h2=p;
}
if(q)
{
//判断各元素的个数分情况
if(s==0&&z==0)
h3=first->next;
if(z==0&&s!=0)
{
h3=h2;
h2=0;
}

p=h3;
for(i=1;i<q;i++)
{t=p->data;
p=p->next;
}
p->next=h3;
h3=p;
}
}
//////////////////////////////////////////////////////////////////////////////
//  输出函数
//  函数功能:输出循环链
//函数参数:
//       l 各链链尾地址
//参数返回值:无
//
void List::Print(Node*l)
{
if(l!=0)
{
Node* p=l;
do
{
cout<<p->data<<" ";
p=p->next;
}while(p!=l);
cout<<endl;
}
else
cout<<"该序列没有元素"<<endl;
}


//程序名:3类型循环表.cpp
//      程序功能:
//      单链表数据中含有数字字符、字母字符、其他字符三种
//      构造3个循环链 ,使得循环链只有一种字符
//      但必须要使用原来单链的结点空间作为三个表的结点空间
//          作者:吴雨羲
//          日期:2013.10.17
//          版本:1.0
//      修改内容:无
//      修改日期:
//      修改作者:
//
#include<iostream>
#include"class.h"
using namespace std;
void main()
{
List list;
Node* a,* b,* c;
int z,s,q;

list.Createlist();
list.Sort(z,s,q);
list.Cut(a,b,c,z,s,q);

cout<<"数字字符序列: ";
list.Print(a);
cout<<"字母字符序列: ";
list.Print(b);
cout<<"其他字符序列: ";
list.Print(c);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐