单向链表的建立:
我们先来看看简单的代码
struct x{
int n;char k[20];
struct x *next;
};
int main(){
int n;char k[20];
struct x *head,*tail,*p;
head=tail=NULL;
while(){ //这个地方我们要写清楚跳出的条件
p=(x*)malloc(sizeof(struct x)); //将结构的首地址给p
scanf("%s %d",k,n);
p->n=n;strcpy(p->k,k);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p; //这里的tail相当于上一个p
tail=p;
}
for(p=head;p!=NULL;p=p->next)
printf("%d",p->n);
return 0;
}
然后我们要有一个简单的思维导图简单加深理解
汉诺塔的简单理解:
先将n-1个从a——>c;
然后将n从a——>b;
最后将n-1再从c->b;
完成任务