图灵机实现
图灵机编码UN两个0之间插入多少给1就代表数为几。为实现UN+1以及UN*2的操作,就必须能实现这一串编码的操作向左或向右移动,所以选用链表无疑是最优选择。建立双向链表,用->next表示R向右移动,->prior表示L向左移动。以及使用链表的插入语句向编码最后插入0结点。
二.算法构造:
用双向链表即可实现编码的输入,->prior向左操作,->next向右操作,以及插入操作。
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
using namespace std;
typedef struct node //链表结构体
{
int data;
struct node *next,*prior;
}node,*linklist;
node *Create() //创建链表
{
node *head;
node *pnext;
node *plast;
int i,n;
head=(linklist)malloc(sizeof(node));
assert(NULL != head);
head->prior=NULL;
head->next=NULL;
printf("请输入链表长度:");
scanf("%d",&n);
while(n!=0)
{
plast=head;
for(i=0;i<n;i++)
{
pnext=(linklist)malloc(sizeof(node));
printf("第%d个结点的数据为:",i+1);
scanf("%d",&pnext->data);
plast->next=pnext;
pnext->prior=plast;
plast=plast->next;
}
pnext->next=NULL;
break;
}
return head;
}
void Show(node*head) //输出数据
{
node *temp;
int j=0;
temp=head;
while(temp->next!=NULL)
{
j++;
printf("%d\0",temp->next->data);
temp=temp->next;
}
cout<<endl;
}
void UN(linklist head) //UN+1操作
{
int flag=0;
node *p;
linklist s;
s=(linklist)malloc(sizeof(node));
s->data=0;
s->next=NULL;
s->prior=NULL;
p=head->next;
while(1)
{
if(p->data==0&&flag==0) //00->00R
{
flag=0;
p->data=0;
p=p->next; //p->next代表向右移动
}
else if(flag==0&&p->data==1) //01->11R
{
flag=1;
p->data=1;
p=p->next;
}
else if(flag==1&&p->data==0) //10->01Stop
{
flag=0;
p->data=1;
s->prior = p;
p->next = s;
s->next = p->next;
p->next->prior = s;
s->next=NULL; //在链表末尾再插入一个0结点 使链表长度+1
cout<<"推算过程:";
Show(head);
break; //break退出循环
}
else if(flag==1&&p->data==1) //11->11R
{
flag=1;
p->data=1;
p=p->next;
}
cout<<"推算过程:";
Show(head);
}
cout<<"最终输出:";
Show(head);
}
void UN2(linklist head) //UN*2 实现与UN+1基本相同;
{
int flag=0;
int i=0;
node *p,*s,*h,*j,*k;
s=(linklist)malloc(sizeof(node));
s->data=0;
s->next=NULL;
s->prior=NULL;
p=head->next;
h=(linklist)malloc(sizeof(node));
h->data=0;
while(1)
{
if(p->next==NULL/*&&flag!=11&&p->data!=0*/)
{
h->prior = p;
p->next = h;
h->next = p->next;
p->next->prior = h;
h->next=NULL;
}
else if(p->data==0&&flag==0) //00->00R
{
flag=0;
p->data=0;
p=p->next;
}
else if(flag==0&&p->data==1) //01->10R
{
flag=1;
p->data=0;
p=p->next;
}
else if(flag==1&&p->data==0) //10->101L
{
flag=10;
p->data=1;
p=p->prior; //用p->prior代表编码操作向左移动
}
else if(flag==1&&p->data==1) //11->11R
{
flag=1;
p->data=1;
p=p->next;
}
else if(flag==10&&p->data==0)
{
flag=11;
p->data=0;
p=p->next;
}
else if(flag==10&&p->data==1)
{
flag=100;
p->data=0;
p=p->next;
}
else if(flag==11&&p->data==0)
{
flag=0;
p->data=1;
s->prior = p;
p->next = s;
s->next = p->next;
p->next->prior = s;
s->next=NULL;
cout<<"推算过程:";
Show(head);
break;
}
else if(flag==11&&p->data==1)
{
flag=11;
p->data=1;
p=p->next;
}
else if(flag==100&&p->data==0)
{
flag=101;
p->data=1;
p=p->prior;
}
else if(flag==100&&p->data==1)
{
flag=100;
p->data=1;
p=p->next;
}
else if(flag==101&&p->data==0)
{
flag=10;
p->data=1;
p=p->prior;
}
else if(flag==101&&p->data==1)
{
flag=101;
p->data=1;
p=p->prior;
}
cout<<"推suan过程:";
Show(head);
}
cout<<"最终输出:";
Show(head);
}
int main()
{
struct node *head;
head=Create();
cout<<endl;
cout<<"这串编码为:"<<endl;
Show(head);
cout<<endl;
cout<<"实现UN+1的过程"<<endl;
UN(head);
cout<<endl;
cout<<"实现UN*2的过程"<<endl;
UN2(head);
return 0;
}