递归/循环操作单链表

考虑到学校还要考察对递归的熟练程度,所以,对链表的操作递归与循环我都有写。

感觉目前递归的问题在于传值,对于链表,在于递归对下一位置的访问,希望我能在短时间内对一行GCD有更多的理解。

上代码:

//
//  main.cpp
//  List-PAT
//  CCUT MaLker
//  Created by MaLker on 01/04/2017.
//  Finshed at 03/04/2017
//  Copyright © 2017 MaLker. All rights reserved.
//

#include <iostream>
using namespace std;
struct Node  {
    int date;
    Node *next;
};
//Node *head=NULL;
Node* Creat_List(void){
    Node *p,*t,*head;
    p=t=head=new Node;
    int n;
    cin>>n;
    while (n--) {
        t=new Node;
        p->next=t;
        cin>>t->date;
        p=p->next;
        p->next=NULL;//如果为循环链表,指向头指针
    }
    return head;
}
void Show_List(Node *head){//用循环遍历数组
    Node* p=head->next;
    while (p) {
        cout<<p->date<<"    ";
        p=p->next;
    }
    cout<<endl;
}
void Use_Recursion_Show_List(Node *head,Node *p,int n){//用递归递归遍历
    if (n==1) {
        n=2;
        p=head->next;
        return Use_Recursion_Show_List(head, p, n);
    }
    else if (p==NULL){
        cout<<endl;
        return ;
    }
    else{
        cout<<p->date<<"    ";
        p=p->next;
        return Use_Recursion_Show_List(head, p, n);
    }
}
void Show_Ksit_Number(Node *head){//展示指定位置的数据
    int k;
    cin>>k;
    int cst=0;
    Node *p=head,*ksit=NULL;
    while (p&&cst<k) {//不能只判断书否到位置还要判断是否超长
        cst++;
        p=p->next;
        ksit=p;
    }
    if (p==NULL) {
        cout<<"False"<<endl;
    }
    else
    cout<<ksit->date<<endl;
}
void Use_Recursion_Show_Ksit_number(Node *head,Node *p,int cst,int k){
    if (cst==0) {
        cin>>k;
        p=head;
        cst=0;
        cst++;
        return Use_Recursion_Show_Ksit_number(head, p->next, cst,k);
    }
    else if (p==NULL){
        cout<<"False"<<endl;
        return ;
    }
    //↑↓两个判断的位置不能发生改变
    else if (cst==k){
        cout<<p->date<<endl;
        return ;
    }
    else
        return Use_Recursion_Show_Ksit_number(head, p->next, ++cst/*这个位置的加法很重要*/,k);
}
void Delet_Ksit_data(Node *head){
    int k;
    cin>>k;
    int cst=0;
    Node *p=head,*ksit=NULL;
    while (p&&cst<k) {
        cst++;
        ksit=p;
        p=p->next;
    }
    //cout<<ksit->date<<endl;//用来调试是否到达指定位置的代码
    ksit->next=p->next;
    delete p;
}
void Use_Recursion_Delet_Ksit_number(Node *head,Node *p,int cst,int k){//注意事项请安靠递归查找
    if (cst==0) {
        cin>>k;
        p=head;
        cst=0;
        cst++;
        return Use_Recursion_Delet_Ksit_number(head, p->next, cst,k);
    }
    else if (p==NULL){
        cout<<"False"<<endl;
        return ;
    }
    else if (cst==k-1){
        Node *t=p->next;
        p->next=t->next;
        delete t;
        return ;
    }
    else
        return Use_Recursion_Delet_Ksit_number(head, p->next, ++cst/*这个位置的加法很重要*/,k);
}
int main(int argc, const char * argv[]) {
    Node *head=NULL;
    head = Creat_List();
    Show_List(head);
    //Use_Recursion_Show_Ksit_number(head, NULL, 0, 0);
    //Node *p=NULL;
    //Use_Recursion_Show_List(head, p, 1);
    //Delet_Ksit_data(head);
    //Show_List(head);
    Use_Recursion_Show_Ksit_number(head, head, 0, 0);//所有需要地址p传值的函数,建议都为头指针
    Show_List(head);
    return 0;
}

我再补发一个递归建立好了:

这个思路和前面一样没有变化,所以,就没加注释(事实上就是想去睡觉了……)

//  递归实现链表的建立
//
//  Created by MaLker on 06/04/2017.
//  Copyright © 2017 MaLker. All rights reserved.
//

#include <iostream>
using namespace std;
struct Node{
    int data;
    Node *next;
};
Node *head;
void Creat_List(Node *p,int n,int cst){
    if (n==0) {
        cin>>n;
        p=new Node;
        cst=0;
        head=p;
        return Creat_List(p, n,cst);
    }
    else if (n!=cst){
        Node *t=NULL;
        t=new Node;
        cin>>t->data;
        t->next=NULL;
        p->next=t;
        return Creat_List(p->next, n, ++cst);
    }
    else if (n==cst){
        return ;
    }
}
void Show_List(void){
    Node *p=head->next;
    while (p) {
        cout<<p->data<<endl;
        p=p->next;
    }
}
int main(int argc, const char * argv[]) {
    Creat_List(NULL, NULL, NULL);
    Show_List();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值