简单图灵机

本文介绍了如何使用双向链表来实现图灵机编码,通过在两个0之间插入1来表示数值,并探讨了如何进行UN+1和UN*2的操作。链表的->next表示向右移动,->prior表示向左移动,同时详细说明了如何利用链表的插入语句进行编码的输入和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图灵机实现

图灵机编码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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值