/*************************************************************************************
****题目3: **
****若S和T是用结点大小为1的单链表存储的两个串,设计算法将S中首次与T匹配的子串逆置。 **
*************************************************************************************/
#include "iostream"
using namespace std;
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linkstring;
linkstring *creat_linkstring(void)
{
datatype ch;
linkstring *head,*rear,*s;
head = NULL;
rear = head;
while ((ch=getchar())!='\n')
{
s = new linkstring;
s->data = ch;
if (head == NULL)
head = s;
else
rear->next = s;
rear = s;
}
if (rear != NULL)
rear->next = NULL;
return head;
}
linkstring * turn(linkstring *head,linkstring *rear)
{
linkstring *p,*q,*r;
p = head;
q = head->next;
if (q == rear)
return head;
r = q->next;
if (r == rear)
{
q->next = p;
p->next = rear;
return q;
}
while (r != rear)
{
q->next = p;
p = q;
q = r;
r = r->next;
}
q->next = p;
head->next = rear;
return q;
}
linkstring * operate(linkstring *S,linkstring *T)
{
int flag = 0;
linkstring *p,*q,*head,*rear,*temp;
p = S;
q = T;
while (1)
{
if (p && p->next != NULL && flag == 0)
temp = p->next;
if (p && q && p->data == q->data)
{
if (flag == 0)
{
head = p;
flag = 1;
}
p = p->next;
q = q->next;
continue;
}
if (!q)
{
rear = p;
//逆置
temp = turn(head,rear);
p = S;
if (S == head)
return temp;
while (p->next != head)
p = p->next;
p->next = temp;
return S; //找到了!
}
else if (!p)
{
cout<<"string S doesnot have string T!"<<endl;
return NULL;
}
else
{
flag = 0;
q = T;
p = temp;
}
if (!p->next)
{
cout<<"string S doesnot have string T!"<<endl;
return NULL;
}
}
}
void print_string (linkstring *s)
{
linkstring *p;
p = s;
if (s == NULL)
cout<<"string is empty!"<<endl;
while (p)
{
cout<<p->data<<"\t";
p = p->next;
}
cout<<endl;
}
void main(void)
{
linkstring *head1,*head2;
head1 = creat_linkstring();
head2 = creat_linkstring();
head1 = operate(head1,head2);
print_string(head1);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
实验5 串运算的实现.rar (35个子文件)
实验5 串运算的实现
2.cpp 879B
2.dsw 527B
1.dsw 527B
Debug
vc60.pdb 108KB
1.exe 244KB
1.ilk 355KB
2.ilk 753KB
2.exe 520KB
1.obj 42KB
3.exe 244KB
vc60.idb 81KB
3.ilk 356KB
3.pdb 1.03MB
1.pdb 561KB
2.pdb 1.03MB
3.pch 1.93MB
3.obj 44KB
2.pch 1.93MB
2.obj 145KB
1.pch 1.93MB
2.opt 48KB
1.plg 1KB
2.ncb 41KB
3.opt 50KB
3.ncb 41KB
1.opt 49KB
1.dsp 3KB
3.dsp 3KB
1.cpp 1KB
3.dsw 527B
3.cpp 2KB
1.ncb 33KB
3.plg 1KB
2.dsp 3KB
2.plg 1KB
共 35 条
- 1
资源评论
- 冰荷婉竹2015-06-19串运算功能实现完善
wangliang3984337123
- 粉丝: 38
- 资源: 110
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功