有两个循环链表,链头指针分别为L1和L2,要求写出算法将L2链表链到L1链表之后,且连接后仍保持循环链表形式。
void connextLink(LNode *L1, LNode *L2){ LNode *rear1 = L1; LNode *rear2 = L2; //找到L1和L2的表尾 while(rear1->next != L1) rear1 = rear1->next; while(rear2->next != L2) rear2 = rear2->next; rear1->next = L2; rear2->next = L1; }
void connextLink(LNode *L1, LNode *L2){
LNode *rear1 = L1;
LNode *rear2 = L2;
//找到L1和L2的表尾
while(rear1->next != L1) rear1 = rear1->next;
while(rear2->next != L2) rear2 = rear2->next;
rear1->next = L2;
rear2->next = L1;
}
void link(btnode*l1,btnode*l2){
btnode *p1,*p2;
p1=l1;
p2=l2;
while(p1->next!=p1){
p1=p1->next;
while(p2->next!=p2){
p2=p2->next;
p1->next=l2;
p2->next=l1;
void Contact(LinkList &L1, LinkList &L2){
LNode *p = L1;
while(p->next!=L1){
p = p->next;
p->next = L2;
p = L2;
while(p->next!=L2){
p->next = L1;
void Merge(LinkList *L1,LinkList *L2){
LinkList *rear1 = L1;
LinkList *rear2 = L2;
while(rear1 -> next != L1){
rear1 = rear1 -> next;
while(rear2 -> next != L2){
rear2 = rear2 -> next;
rear1 -> next = L2;
rear2 -> next = L1;
return L1;
void connect(LNode* L1, LNode* L2){ if(!L1 || !L2) return; LNode* p = L1, *q = L2; while(p->next != L1){ p = p->next; } while(q->next != L2){ q = q->next; } p->next = L2; q->next = L1; return; }
void connect(Lnode *L1, Lnode *L2) { if(!L1 || !L2) return ; Lnode *p, *q; p = L1 -> next; q = L2 -> next; //找到L1,L2尾结点 while(p) { if(p -> next == L1) break; p = p -> next; } while(q) { if(q -> next == L2) break; q = q -> next; } //合并 p -> next = L2; q -> next = L1; }
void func(LNode *L1,LNode *L2){
LNode *p=L1->next;
LNode *q=L2->next;
while(p!=L1){
p=p->next;
while(q!=L2){
q=q->next;
p->next=L2;
q->next=L1;
首尾相连
void merge(Lnode *L1, Lnode *L2)
{Lnode *p = L1, *q = L2 ;
while(p->next!=L1)
while(q->next!=L2)
q->next=L1; p->next =L2;
头尾相连
//先各自循环各表的尾结点 再连接
找到L1的尾结点后续连接L2,再找到L2的尾结点后续连接L1
if(L1==NULL||L2==NULL)return;
node *p=L1;
while(p->next!=L1)p=p->next;
node *q=L2;
while(q->next!=L2)q=q->next;
ff
#include<stdio.h>
typedef struct node {
int data;
struct node *next;
} person;
//初始化循环链表
person *initlink(int a[],int size) {
person *head=(person*)malloc(sizeof(person));
head->data=a[0];
head->next=NULL;
person *cyclic=head;
for(int i=1; i<size; i++) {
person *temp=(person*)malloc(sizeof(person));
temp->data=a[i];
temp->next=NULL;
cyclic->next=temp;
cyclic=cyclic->next;
cyclic->next=head;
return head;
//输出循环链表
void displaycyclic(person *p) {
person *temp=p;
do {
printf("%d",temp->data);
if(temp->next!=p) {
printf("->");
temp=temp->next;
} while(temp!=p);
putchar(10);
//连接
person *linklist(person *p1,person *p2) {
person *temp1=p1;
person *temp2=p2;
while(temp1->next!=p1) {
temp1=temp1->next;
while(temp2->next!=p2) {
temp2=temp2->next;
temp1->next=p2;
temp2->next=p1;
return p1;
int main()
{
int a[]= {1,2,3,4,5},b[]= {6,7,8,9,10};
int size1=sizeof(a)/sizeof(a[0]);
int size2=sizeof(b)/sizeof(b[0]);
person *L1=initlink(a,size1);
person *L2=initlink(b,size2);
displaycyclic(L1);
displaycyclic(L2);
person *head=linklist(L1,L2);
displaycyclic(head);
return 0;
typedef struct LNode{
LNode *next;
}LNode,*LinkList;
LinkList Link(LinkList &L1,LinkList &L2){
LNode *p,*q;
p=L1;
q=L2;
void merge(Lnode *L1,Lnode *L2){ Lnode *p=L1,*q=L2; while(p->next!=L1) p=p->next; while(q->next!=L2) q=q->next; p->next=L2; q->next=L1; }
void merge(LinkList L1, LinkList L2){
LinkNode *p = L1, *q = L2;
while(p->next != L1)
while(q->next != L2)
q = q->next;
q->next = L1;
struct RLnode{ int data; RLnode *l,*r; } void join(RLnode * l1,RLnode * l2){ RLnode * p = l1 , p1=l2; while(p->next!= l1){ p = p->next; } while(p1->next!= l2){ p1 = p1->next; } p->next = l2; p1 = next = l1; }
struct RLnode{ int data; RLnode *l,*r; } void join(RLnode * l1,RLnode * l2){ RLnode * l1_l = l1 , * l1_r = l1->l, * l2_l = l2, * l2_r = l2->l ; l1_r->r = l2_l; l2_l->l = l1_r; l2_r->r = l1_l l1_l->l = l2_r; }
lNode p,q;
p =l1 q=l2
while(p->next != l1){
p = p->next
while(q->next != l2){
q = q->next
q->next=L1
p->next=L2
void merge(Lnode *L1,Lnode *L2){
Lnode *p=L1;Lnode *q=L2;
while (p->next!=L1) p=p->next; //找到L1链尾
while (q->next!=L2) q=q->next; //找到L2链尾
p->next=L2;q->next=L1;
L2->pre = L1;
L1->pre = L2;
List* l1 = L1; while(l1->next != L1){ l1 = l1->next; } l1->next = L2;
List* l2 = L2; while(l2->next != L2){ l2=l2->next; } l2->next = L1;
//注意是链头指针且是循环链表 尾部必然指向头部
Lnode *p =L1,*q = L2;
while(p->next != L1) p = p->next; //找链表尾
p -> next =L2; //L2链接到L1尾部
while(q->next != L2) q = q->next;
q -> next = L1;
L1->rear->next = L2->head->next;
L2->rear->next=L1->head
void merge(Lnode *L1, Lnode *L2) {
Lnode *p = L1, *q = L2 ;
while(p->next!=L1)//找到L1链尾
while(q->next!=L2)//找到L2链尾
void Link_Two_List(LinkList &L1,LinkList &L2){
LNode *p=L1,*q=L2;
if(q->next==q) {free(L2);return;}//L2为空
while(p->next!=L1){//找到L1的尾节点
while(q->next!=L2){//找到L2的尾节点
q->next=p->next;
p->next=L2->next;
free(L2);
return;
void Connect(LinkNode* L1,LinkNode* L2)
LinkNode* p;
while(p->next!=L1) p=p->next;
while(p->next!=L2) p=p->next;
p->next=L1;
1 分别遍历L1 L2 找到各自Rear结点
1 L1的rear结点next指向L2
2 L2的rear的next指向L1
void MergeCycleList(Lnode *l1,Lnode* l2)
Lnode* p=l1;Lnode* q=l2;
while(p->next!=l1)
while(q->next!=l2)
p->next=l2;q->next=l1;
void merge(L1,L2)
p = L1; q = L2;
while(p->next != L1) p = p->next;
if(q->next != L2) p->next = q->next;
//我认为题意将L2链接到L1后应该不包括L2的头结点! 头结点唯一表示链表,并不是用来存储数据的节点,所以这里进行处理
LinkList* connectList(LinkList* L1,LinkList* L2){ //L1尾接到L2头,L2尾接到L1头 LinkList* p1 = L1->next; LinkList* p2 = L2->next; while(p1->next!=L1) p1 = p1->next; p1->next = L2;//L1尾接到L2头节点 while(p2->next!=L2) p2 = p2->next; p2->next = L1;//L2尾接到L1头节点 return L1; }
int merge(LinkList L1,LinkList L2){
LinkList* p=L1;
LinkList* q=L2;
while(p->next==null || q->next==null){
return false;
while(p->next != L1){
while(q=>next != L2){
p->next=q;
return true;
山药男孩 回复 山药男孩: p->next = L2;
//用LinkList表示传入是一个链表,LNode表示节点,只是对同一结构的不同命名
int merge(LinkList L1, LinkList L2)
//避免传入的是空表
if ((L1->next == NULL) || (L2->next == NULL)) {
LNode *q = L2;
while (p->next != L1) {
p = p->next;//遍历l1尾部节点
while (q->next != L2) {
q = q->next;//遍历L2的尾部节点
p->next = L2;//将L2接入L1的表尾部
q->next = L1;//将L2的表尾连L1形成循环链表
//用LinkList表示传入是一个链表,LNode表示节点,只是对同一结构的不同命名 int merge(LinkList L1, LinkList L2) { //避免传入的是空表 if ((L1->next == NULL) || (L2->next == NULL)) { return false; } LNode *p = L1; LNode *q = L2; while (p->next != L1) { p = p->next;//遍历l1尾部节点 } while (q->next != L2) { q = q->next;//遍历L2的尾部节点 } p->next = L2;//将L2接入L1的表尾部 q->next = L1;//将L2的表尾连L1形成循环链表 return true; }
思路:一、分别遍历得到传入循环链表的表尾
二、实现链表连接,并保证循环的特性
void merge(Lnode *L1, Lnode *L2) {Lnode *p = L1, *q = L2 ; while(p->next!=L1) p=p->next; while(q->next!=L2) q=q->next; q->next=L1; p->next =L2; }
思路
1.遍历找到循环链表的尾结点
2将L1的尾结点连接到L2的头结点,L2的尾结点连接到L1的头结点
void connect(struct LNode* L1, struct LNode* L2) { if (L1->next = NULL) (L2->next = NULL) { return FALSE; } struct LNode* r1 = L1->next; while (r1->next != L1) { r1 = r1->next;//遍历查找尾结点 } struct LNode* r2 = L2->next; while (r2->next = L2) { r2 = r2->next; } r1->next = L2; r2->next = L1; }
下一个
void(List &L1,List &L2)
List p,q;
if(L1->next&&L2->next) retutn error;
p=p->next; }
q=q->next; }
void merge(Lnode *L1, Lnode *L2){
Lnode *p,*q;
voidmerge(Lnode*L1,Lnode*L2)<br /> {Lnode*p,*q;<br /> while(p->next!=L1)<br /> p=p->next;<br /> while(q->next!=L2)<br /> q=q->next;<br /> q->next=L1;p->next=L2;<br /> }
L1->prior->next = L2;
L2->prior ->next = L1;
p = L1->prior;
L1->prior = L2->prior;
L2->prior = p;
LNode* head1=L1;
LNode *tai1l=head1;
while(head1!=tail1){
tail1=tail1->next;
LNode* head2=L2;
LNode *tail2=head2;
while(head2!=tail2){
tail2=tail2->next;
tail1.next=head2;
tail2.next=head1;
void connectLinkedLists(struct ListNode* L1, struct ListNode* L2) { if (L1 == NULL || L2 == NULL) { return; } struct ListNode* L1_last = L1->next; while (L1_last->next != L1) { L1_last = L1_last->next; } struct ListNode* L2_last = L2->next; while (L2_last->next != L2) { L2_last = L2_last->next; } L1_last->next = L2; L2_last->next = L1; }
void merge(LinkList L1,LinkList L2){
LinkNode *p=L1,*q=L2;
while(q->next!=L2) q=q->next; q->next=L1;
void mergeHa(LinkList L1, LinkList L2){ LNode *L1Tail = L1->next, *L2Tail = L2->next; while (L1Tail->next != L1) L1Tail = L1->next; while (L2Tail->next != L2) L2Tail = L2->next; L1Tail->next = L2; L2Tail->next = L1; }
{Lnode *p,*q ;
void Merge(List *L1,List *L2)
List *p,*q;
while (p->next!=L1)
while (q->next!=L2)
print(L1);
#include<stdio.h> #include<stdlib.h> #define M (List)malloc(sizeof(Node)) typedef struct Node{ int data; Node* next; }*List,Node;
//创建循环链表 List create(int n){ int i; List head,tail; head=tail=NULL; for(i=n;i<n+10;i++){ List p=M; p->data=i; if(head==NULL){ head=p; tail=p; }else{ tail->next=p; tail=p; } } tail->next=head; return head; } //合并两个单链表 void merge(List L1,List L2){ List p1=L1,p2=L2; while(1){ //找到第一个链表的末尾 if(p1->next==L1) break; p1=p1->next; } while(1){ //找到第二个链表的末尾 if(p2->next==L2) break; p2=p2->next; } //开始合并 p1->next=L2; p2->next=L1; } //打印循环链表 void print(List head){ List p=head; while(1){ printf("%d ",p->data); p=p->next; if(p==head) break; } printf("\n"); } int main(){ List L1=create(0); List L2=create(10); print(L1); merge(L1,L2); print(L1); return 0; }
void connect(List L1,List L1){ List *p,*q; while(p->next!=L1){ p=p->next;} while(q->next!=L2){ q=q->next;} p->next=L2; q->next=L1; }
bool ConnectLinkList(LinkList &L1, LinkList L2){
void merge(LNode *L1,LNODE *L2){ LNode *p,*q; p = L1; q = L2; while(p->next != L1){ p = p->next; } p->next = L2; while(q->next != L2){ q = q->next; } q->next = L1; }
void Twicelist(Linklist &L1,LinkList &L2){
Linklist *Lo,*Lt;
if(L1->next==NULL||L2->next==NULL){
return ERROR;
Lo=L1;
Lt=L2;
while(Lo->next!=L1){
Lo=Lo->next;
while(Lt->next!=L2){
Lt=Lt->next;
Lo->next=L2;
Lt->next=L1;
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
ListNode *r1=l1;
ListNode *r2=l2;
while(r1->next!=l1)
r1=r1->next;
while(r2->next!=l2)
r2=r2->next;
r1->next=l2;
r2->next=l1;
return l1;
答案:void merge(Lno...
用户登录可进行刷题及查看答案
答案:void merge(Lnode *L1, Lnode *L2)
登录后提交答案