王道2.16、2.17、2.18

2.16

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

#include

#include

#include

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<data<<" ";

        p = p->next;

    }

    cout<

    p = L2->next;

    while(p != NULL){

        cout<data<<" ";

        p = p->next;

    }

    cout<

    bool flag = continuPartLinkList(L1,L2);

    if(flag==true){

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

    }else{

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

    }

}

2.17

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

#include

#include

#include

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<data<<" ";

        p = p->next;

    }

    bool flag = is_symm(L);

    if(flag){

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

    }else{

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

    }

}

2.18

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

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

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

#include

#include

#include

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<data<<" ";

        p = p->next;

    }

    cout<

    p = h2->next;

    while(p != h2){

        cout<data<<" ";

        p = p->next;

    }

    cout<

    link_LinkList(h1,h2);

    p = h1->next;

    while(p != h1){

        cout<data<<" ";

        p = p->next;

    }

}

你可能感兴趣的:(王道2.16、2.17、2.18)