美文网首页工作生活
王道2.16、2.17、2.18

王道2.16、2.17、2.18

作者: 啦啦啦_9a5f | 来源:发表于2019-07-01 09:23 被阅读0次

2.16

//两个整数序列A=a1,a2,a3,...,am和B= b1,b2,b3,...,bn已经存入两个单链表中,

#include <iostream>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

typedef struct Lnode{

    int data;

    struct Lnode *next;

}Lnode,*LinkList;

void init_LinkList(LinkList &head){

    int x;

    head = (LinkList)malloc(sizeof(Lnode));

    head->next= NULL;

    Lnode *p,*r = head;

    cout<<"请输入数字(-1结束):";

    cin>>x;

    while(x != -1){

        p = (Lnode*)malloc(sizeof(Lnode));

        p->next = r->next;

        p->data = x;

        r->next = p;

        r= p;

        cout<<"请输入数字(-1结束):";

        cin>>x;

        }

}

//设计一个算法,判断序列B是否是序列A的连续子序列。

bool continuPartLinkList(LinkList A,LinkList B){

    if(A == NULL||B == NULL){

        return false;

    }

    Lnode *p1 = A->next,*p2 = B->next;

    while(p1&&p2){

        if(p1->data!=p2->data){

            p1 = p1->next;

            p2 = B->next;

        }else{

            p1 = p1->next;

            p2 = p2->next;

        }

    }

      if( p2 == NULL){

        return true;

       }else{

        return false;

    }

}

int main(){

    LinkList L1,L2;

    init_LinkList(L1);

    init_LinkList(L2);

    Lnode *p; p = L1->next;

    while(p != NULL){

        cout<<p->data<<" ";

        p = p->next;

    }

    cout<<endl;

    p = L2->next;

    while(p != NULL){

        cout<<p->data<<" ";

        p = p->next;

    }

    cout<<endl;

    bool flag = continuPartLinkList(L1,L2);

    if(flag==true){

        cout<<"字符串B是字符串A的连续子串。";

    }else{

        cout<<"字符串B不是A的连续子串。";

    }

}

2.17

//设计一个算法用于判断带头结点的循环双联表是否对称。

#include <iostream>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

typedef struct Lnode{

    int data;

    struct Lnode *pre;

    struct Lnode *next;

}Lnode,*LinkList;

void init_LinkList(LinkList &head){

    int x;

    head = (LinkList)malloc(sizeof(Lnode));

    head->next= NULL;

    head->pre = NULL;

    Lnode *p,*r= head;

    cout<<"请输入数字(-1结束):";

    cin>>x;

    while(x != -1){

        p = (Lnode*)malloc(sizeof(Lnode));   

        p->next = r->next;

        p->pre = r;

        p->data = x;

        r->next = p;

        r= p;

        cout<<"请输入数字(-1结束):";

        cin>>x;

    }

    head->pre = r;

    r->next = head;

}

//判断双联表是否对称

bool is_symm(LinkList L){

    Lnode *p=L->next,*q=L->pre;//p指向第一个结点,q指向最后一个结点

    while(p != q){

        if(p->data == q->data){

            p = p->next;

            q = q->pre;

          }else{

            break;

        }

    }

    if(p == q){

        return true;

    }else{

        return false;

    }

}

int main(){

    LinkList L,p;

    init_LinkList(L);

    p = L->next;

    while(p != L){

        cout<<p->data<<" ";

        p = p->next;

    }

    bool flag = is_symm(L);

    if(flag){

        cout<<"循环双链表对称";

    }else{

        cout<<"循环双链表不对称";

    }

}

2.18

//有两个循环单链表,链表头指针分为h1和h2,

//编写一个函数两链表h2链接到链表h1之后,

//要求链接后的链表仍保持循环链表的形式

#include <iostream>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

typedef struct Lnode{

    int data;

    struct Lnode *next;

}Lnode,*LinkList;

void init_LinkList(LinkList &head){

    int x;

    head = (LinkList)malloc(sizeof(Lnode));

    head->next= NULL;

    Lnode *p,*r= head;

    cout<<"请输入数字(-1结束):";

    cin>>x;

    while(x != -1){

        p = (Lnode*)malloc(sizeof(Lnode));   

        p->next = r->next;

        p->data = x;

        r->next = p;

        r= p;

        cout<<"请输入数字(-1结束):";

        cin>>x;

         }

        r->next = head;

    }

void link_LinkList(LinkList &h1,LinkList &h2){

    LinkList p1 = h1->next,p2 = h2->next;

    while(p1->next != h1){

        p1 = p1->next;

    }

    p1->next = p2;

    free(h2);

    while(p2->next != h2){

        p2 = p2->next;

    }

        p2->next = h1;

}

int main(){

    LinkList h1,h2,p;

    init_LinkList(h1);

    init_LinkList(h2);

    p = h1->next;

    while(p != h1){

        cout<<p->data<<" ";

        p = p->next;

    }

    cout<<endl;

    p = h2->next;

    while(p != h2){

        cout<<p->data<<" ";

        p = p->next;

    }

    cout<<endl;

    link_LinkList(h1,h2);

    p = h1->next;

    while(p != h1){

        cout<<p->data<<" ";

        p = p->next;

    }

}

相关文章

  • 王道2.16、2.17、2.18

    2.16 //两个整数序列A=a1,a2,a3,...,am和B= b1,b2,b3,...,bn已经存入两个单链...

  • 第2章 信息的表示和处理

    2.16 2.17 2.18 A : 736 B : -88 C : 40 D : 48 E : 120 F : ...

  • 2019 工作

    2.16古滇王国 2.17古滇王国 2.18古滇王国 2.19古滇王国 2.20古滇王国 2.21古滇王国 2.2...

  • 2.17 2.18

    终于办好了网线,玩游戏贼6 但是也要开始认真的照顾好自己的学业,新的学期更要努力。 又逃掉了日语课,真的很不喜欢那...

  • 水性笔练画日常五🌸(2.6-2.20)

    2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2...

  • 2.16-2.17

    假期最后两天了,简单写写吧。 16日去看了电影人潮汹涌,华仔无论多老都魅力依旧,认真记录的样子好帅。万茜很美,肖央...

  • 读《大数据白皮书》笔记

    正处在疫情期间,我2.3返京,2.16有一位室友返京,2.17返京隔离满14天。可2.16晚上收到工行新防疫要求:...

  • 【考研日记】2.16~2.17

    这两天周末,窝在宿舍看了两天的电影。周末晚上带着帅郭见了刚结婚的老友,吃了顿饭。两天没有学习,嗯嗯。就是又舒坦又愧疚。

  • 真诚就是最好的答案

    线上洞见生涯职场游戏复盘 研发者:赵昂老师 引导人:六哥 2.16-2.17日我参...

  • 橘子郡每天读《论语》第268天/共270天

    橘子郡每天读《论语》第268天/共270天 2.16: 子曰:“攻乎异端 ,斯害也已!” 2.17: 子曰:“由!...

网友评论

    本文标题:王道2.16、2.17、2.18

    本文链接:https://www.haomeiwen.com/subject/pmuwcctx.html