2.16
//兩個整數(shù)序列A=a1,a2,a3,...,am和B= b1,b2,b3,...,bn已經(jīng)存入兩個單鏈表中唾那,
#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<<"請輸入數(shù)字(-1結(jié)束):";
????cin>>x;
????while(x != -1){
????????p = (Lnode*)malloc(sizeof(Lnode));
????????p->next = r->next;
????????p->data = x;
????????r->next = p;
????????r= p;
????????cout<<"請輸入數(shù)字(-1結(jié)束):";
????????cin>>x;
????????}
}
//設(shè)計一個算法访锻,判斷序列B是否是序列A的連續(xù)子序列。
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的連續(xù)子串闹获。";
????}else{
????????cout<<"字符串B不是A的連續(xù)子串期犬。";
????}
}
2.17
//設(shè)計一個算法用于判斷帶頭結(jié)點的循環(huán)雙聯(lián)表是否對稱避诽。
#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<<"請輸入數(shù)字(-1結(jié)束):";
????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<<"請輸入數(shù)字(-1結(jié)束):";
????????cin>>x;
????}
????head->pre = r;
????r->next = head;
}
//判斷雙聯(lián)表是否對稱
bool is_symm(LinkList L){
????Lnode *p=L->next,*q=L->pre;//p指向第一個結(jié)點沙庐,q指向最后一個結(jié)點
????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<<"循環(huán)雙鏈表對稱";
????}else{
????????cout<<"循環(huán)雙鏈表不對稱";
????}
}
2.18
//有兩個循環(huán)單鏈表,鏈表頭指針分為h1和h2棉安,
//編寫一個函數(shù)兩鏈表h2鏈接到鏈表h1之后,
//要求鏈接后的鏈表仍保持循環(huán)鏈表的形式
#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<<"請輸入數(shù)字(-1結(jié)束):";
????cin>>x;
????while(x != -1){
????????p = (Lnode*)malloc(sizeof(Lnode));? ?
????????p->next = r->next;
????????p->data = x;
????????r->next = p;
????????r= p;
????????cout<<"請輸入數(shù)字(-1結(jié)束):";
????????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;
????}
}