mirror of
https://github.com/xusun0623/exam_code_for_408.git
synced 2026-02-03 02:14:02 +08:00
add 2012
This commit is contained in:
63
code_doublelink_2012.cpp
Normal file
63
code_doublelink_2012.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
typedef struct node {
|
||||
char data;
|
||||
struct node* link;
|
||||
}NODE;
|
||||
|
||||
void create(NODE* p, char a[], int pos, int length) {
|
||||
if (pos < length) {
|
||||
p->data = a[pos];
|
||||
p->link = (NODE*)malloc(sizeof(NODE));
|
||||
p->link->data = '\0';//用\0作为结束标志
|
||||
create(p->link, a, pos + 1, length);
|
||||
}
|
||||
}
|
||||
|
||||
NODE* getSameNode(NODE* a, NODE* b) {
|
||||
int a_length = 0, b_length = 0;
|
||||
NODE* a_p = a->link, * b_p = b->link;
|
||||
while (a_p->data != '\0') {//统计a的节点数
|
||||
a_length++;
|
||||
a_p = a_p->link;
|
||||
}
|
||||
while (b_p->data != '\0') {//统计b的节点数
|
||||
b_length++;
|
||||
b_p = b_p->link;
|
||||
}
|
||||
NODE* a_scan = a->link, * b_scan = b->link;
|
||||
while (a_length < b_length) {//快指针先走
|
||||
b_scan = b_scan->link;
|
||||
b_length--;
|
||||
}
|
||||
while (b_length < a_length) {//快指针先走
|
||||
a_scan = a_scan->link;
|
||||
a_length--;
|
||||
}
|
||||
while (a_length-- > 0) {
|
||||
if (a_scan == b_scan) {
|
||||
return a_scan;
|
||||
}
|
||||
else {
|
||||
a_scan = a_scan->link;
|
||||
b_scan = b_scan->link;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
NODE a, b;
|
||||
create(&a, "loading", 0, 7);
|
||||
create(&b, "being", 0, 5);
|
||||
NODE* tmp = b.link->link->link;
|
||||
b.link->link = a.link->link->link->link;
|
||||
free(tmp);
|
||||
//两条交叉链构建完毕
|
||||
NODE a_with_head, b_with_head;
|
||||
a_with_head.link = &a;
|
||||
b_with_head.link = &b;
|
||||
NODE* same = getSameNode(&a_with_head, &b_with_head);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user