简单图灵机

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

图灵机实现

图灵机编码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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值