#include
using namespace std;
const int N = 20;
int a[N] = {1,2,3,4,5,6,-1};
int b[N] = {10,3,2,1,-1};
typedef struct node{
int data;
struct node *next;
}Lnode,*Linklist;
typedef struct Dnode{
int data;
struct Dnode *llink,*rlink;
int freq;
}DLnode,*Dlinklist;
void print(Linklist L){
L = L->next;
while(L!=NULL){
printf("%d ", L->data);
L = L->next;
}
puts("");
}
void init_list2(Linklist &L,int a[]){
int cnt = 0;
L = (Lnode*)malloc(sizeof(Lnode));
L->next = NULL;
Lnode*s, *r = L;
while(a[cnt]!=-1){
s = (Lnode*)malloc(sizeof(Lnode));
s->data = a[cnt++];
r->next = s;
r = s;
}
r->next = L;
}
void init_dlist(Dlinklist &L,int a[]){
L = (Dlinklist)malloc(sizeof(DLnode));
L->llink = L;
L->rlink = L;
DLnode* s,*r = L;
int cnt = 0;
while(a[cnt]!=-1){
s = (DLnode*)malloc(sizeof(DLnode));
s->data = a[cnt++];
r->rlink = s;
s->llink = r;
r = s;
}
r->rlink = L;
L->llink = r;
}
void init_dlist1(Dlinklist &L,int a[]){
L = (Dlinklist)malloc(sizeof(DLnode));
L->llink = L;
L->rlink = L;
DLnode* s,*r = L;
int cnt = 0;
while(a[cnt]!=-1){
s = (DLnode*)malloc(sizeof(DLnode));
s->data = a[cnt++];
s->freq = 0;
r->rlink = s;
s->llink = r;
r = s;
}
r->rlink = NULL;
L->llink = NULL;
}
void print_dlist1(Dlinklist L){
DLnode *p = L->rlink;
while(p){
printf("%d ", p->data);
p = p->rlink;
}
}
void init_list1(Linklist &L,int a[]){
int cnt = 0;
L = (Lnode*)malloc(sizeof(Lnode));
L->next = NULL;
Lnode*s, *r = L;
while(a[cnt]!=-1){
s = (Lnode*)malloc(sizeof(Lnode));
s->data = a[cnt++];
r->next = s;
r = s;
}
r->next = NULL;
}
void init_list(Linklist &L){
int cnt = 0;
L = (Lnode*)malloc(sizeof(Lnode));
L->next = NULL;
Lnode*s, *r = L;
while(a[cnt]!=-1){
s = (Lnode*)malloc(sizeof(Lnode));
s->data = a[cnt++];
r->next = s;
r = s;
}
r->next = NULL;
}
void delet_x(Linklist &L,int x){
if(L == NULL) return;
Lnode* p;
if(L->data == x){
p = L;
L = L->next;
free(p);
delet_x(L, x);
}
else delet_x(L->next , x);
}
void delet_x2(Linklist &L,int x){
Lnode *pre = L,*q = L->next;
while(q != NULL){
if(q->data == x){
Lnode *tmp = q;
q = q->next;
pre->next = q;
free(tmp);
}
else{
pre = q;
q = q->next;
}
}
}
void print_reverse(Linklist L){
if(L->next != NULL) print_reverse(L->next);
printf("%d ", L->data);
}
void print_head(Linklist L){
if(L->next != NULL) print_reverse(L->next);
}
void delet_minx(Linklist &L){
Lnode *pre = L, *p = L->next , *minpre = L,*minp = L->next;
while(p != NULL){
if(p->data < minp->data){
minpre = pre;
minp = p;
}
pre = p;
p = p->next;
}
minpre->next = minp->next;
free(minp);
}
void reverse_list(Linklist &L){
Lnode *pre, *p = L->next,*r = p->next;
p->next = NULL;
while(r!=NULL){
pre = p;
p = r;
r = r->next;
p->next = pre;
}
L->next = p;
}
void reverse_list2(Linklist &L){
Lnode *p = L->next ,*r;
L->next = NULL;
while(p!=NULL){
r = p->next;
p->next = L->next;
L->next = p;
p = r;
}
}
void sort(Linklist &L){
Lnode *p = L->next , *pre,*r = p->next;
p->next = NULL;
p = r;
while(p!=NULL){
pre = L;
r = p->next;
while(pre->next != NULL && pre->next->data < p->data) pre = pre->next;
p->next = pre->next;
pre->next = p;
p = r;
}
}
void delet_x_y(Linklist &L,int x,int y){
Lnode *pre = L, *p = L->next;
while(p!=NULL){
if(p->data > x && p->data < y){
pre->next = p->next;
free(p);
p = pre->next;
}
else{
pre = p;
p = p->next;
}
}
}
int Length(Linklist L){
int cnt = 0;
while(L!=NULL){
cnt++;
L = L->next;
}
return cnt;
}
Linklist common_list(Linklist L1,Linklist L2){
int len1 = Length(L1), len2 = Length(L2);
int dis = 0;
if(len1 > len2){
dis = len1 - len2;
while(dis--){
L1 = L1->next;
}
}
else{
dis = len2 - len1;
while(dis--){
L2 = L2->next;
}
}
while(L1!=NULL){
if(L1 == L2) return L1;
else L1 = L1->next,L2 = L2->next;
}
return NULL;
}
void sort_print(Linklist &head){
while(head->next != NULL){
Lnode *pre = head,*p = head->next;
while(p->next!=NULL){
if(p->next->data < pre->next->data) pre = p;
p = p->next;
}
Lnode* tmp = pre->next;
printf("%d ",tmp->data);
pre->next = tmp->next;
free(tmp);
}
puts("");
}
Linklist divid(Linklist &A){
Linklist B = (Linklist)malloc(sizeof(Lnode));
B->next = NULL;
Lnode *a = A,*b = B;
int cnt = 0;
Lnode *p = A;
while(p != NULL){
cnt++;
p = p->next;
if(cnt % 2){
a->next = p;
a = p;
}
else{
b->next = p;
b = p;
}
}
return B;
}
Linklist divid2(Linklist &A){
Linklist B = (Linklist)malloc(sizeof(Lnode));
B->next = NULL;
Lnode* a = A, *b = B,*q;
Lnode* p = A->next;
while(p != NULL){
a->next = p;
a = p;
p = p->next;
if(p!=NULL){
q = p->next;
p->next = b->next;
b->next = p;
p = q;
}
}
a->next = NULL;
return B;
}
void distinct(Linklist &L){
if(L == NULL) return ;
Lnode *q,*p = L->next;
while(p->next!=NULL){
q = p->next;
if(p->data == q->data){
p->next = q->next;
free(q);
}
else p = p->next;
}
}
void merge1(Linklist &La,Linklist &Lb){
Lnode *a = La->next, *b = Lb->next, *r;
La->next = NULL;
while(a!=NULL&&b!=NULL){
if(a->data <= b->data){
r = a->next;
a->next = La->next;
La->next = a;
a = r;
}
else{
r = b->next;
b->next = La->next;
La->next = b;
b = r;
}
}
if(b) a = b;
if(a){
while(a!=NULL){
r = a->next;
a->next = La->next;
La->next = a;
a = r;
}
}
free(Lb);
}
Linklist merge2(Linklist La,Linklist Lb){
Linklist Lc = (Linklist)malloc(sizeof(Lnode));
Lc->next = NULL;
Lnode *r = Lc;
La = La->next;
Lb = Lb->next;
while(La&&Lb){
if(La->data < Lb->data){
La = La->next;
}
else if(La->data > Lb->data){
Lb = Lb->next;
}
else{
Lnode*s = (Lnode*)malloc(sizeof(Lnode));
s->data = La->data;
r->next = s;
r = s;
La = La->next;
Lb = Lb->next;
}
}
r->next = NULL;
return Lc;
}
Linklist merge3(Linklist &La,Linklist &Lb){
Lnode *a = La->next , *b = Lb->next;
La->next = NULL;
Lnode *r = La,*q;
while(a!=NULL&&b!=NULL){
if(a->data < b->data){
q = a;
a = a->next;
free(q);
}
else if(a->data > b->data){
q = b;
b = b->next;
free(q);
}
else{
r->next = a;
r = a;
a = a->next;
q = b;
b = b->next;
free(q);
}
}
if(b) a = b;
while(a){
q = a;
a = a->next;
free(q);
}
r->next = NULL;
return La;
}
bool judge_list(Linklist La, Linklist Lb){
if(La->next == NULL || Lb->next == NULL) return false;
Lnode *pa = La->next , *pb = Lb->next;
Lnode *p = pa->next;
while(pa&&pb){
if(pa->data == pb->data){
pa = pa->next;
pb = pb->next;
}
else{
pb = Lb->next;
pa = p;
if(p) p = p->next;
}
}
if(pb) return false;
return true;
}
bool judge_list1(Dlinklist L){
DLnode *p = L->rlink, *q = L->llink;
while(p!=q){
if(p->data == q->data){
if(p->rlink==q){
return true;
}
p = p->rlink;
q = q->llink;
}
else
return false;
}
return true;
}
Linklist link(Linklist &L1,Linklist &L2){
Lnode* p = L1;
while(p->next != L1) p = p->next;
p->next = L2;
p = L2;
while(p->next != L2) p = p->next;
p->next = L1;
return L1;
}
void delet_min(Linklist &L){
Lnode *pre , *p,*tmp;
while(L->next != L){
pre = L,p = L->next;
while(p->next!=L){
if(pre->next->data>p->next->data) pre = p;
p = p->next;
}
tmp = pre->next;
printf("%d ", tmp->data);
pre->next = tmp->next;
free(tmp);
}
free(L);
}
DLnode* Locate(Dlinklist &L,int x){
DLnode *p = L->rlink;
while(p!=NULL&&p->data!=x){
p = p->rlink;
}
if(p==NULL) return NULL;
else{
p->freq++;
if(p->llink == L || p->llink->freq > p->freq) return p;
p->llink->rlink = p->rlink;
if(p->rlink) p->rlink->llink = p->llink;
DLnode* q = p->llink;
while(q!=L){
if(q->freq <= p->freq) q = q->llink;
else break;
}
p->rlink = q->rlink;
if(q->rlink) q->rlink->llink = p;
q->rlink = p;
p->llink = q;
}
return p;
}
Lnode* find_loop(Linklist L){
Lnode *fast = L, *slow = L;
while(fast!=NULL&&fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow) break;
}
if(fast == NULL || fast->next == NULL) return NULL;
fast = L;
while(fast!=slow){
slow = slow->next;
fast = fast->next;
}
return fast;
}
int find_k(Linklist L,int k){
int cnt = 1;
Lnode *q = L->next, *p = L->next;
while(p && cnt < k){
p = p->next;
cnt++;
}
if(p == NULL) return 0;
while(p->next){
p = p->next;
q = q->next;
}
printf("%d\n",q->data);
return 1;
}
int search_k(Linklist L, int k){
Lnode *p = L->next , *q = L->next;
int cnt = 0;
while(p){
if(cnt < k) cnt++;
else q = q->next;
p = p->next;
}
if(cnt < k) return 0;
printf("%d\n",q->data);
return 1;
}
int length(Linklist L){
int len = 0;
while(L){
len++;
L = L->next;
}
return len;
}
Lnode* find_common_word(Linklist str1,Linklist str2){
Lnode* p = str1->next , *q = str2->next;
int m = length(str1);
int n = length(str2);
while(m > n){
p = p->next;
m--;
}
while(n > m){
q = q->next;
n--;
}
while(p!=q){
q = q->next;
p = p->next;
}
return p;
}
Linklist abs_distinct(Linklist L,int n){
int *vis = (int*)malloc(sizeof(int)*(n+1));
for(int i = 0 ; i < n+1; i++) vis[i] = 0;
Lnode *p = L;
while(p->next){
int x = p->next->data;
if(x<0) x = -x;
if(!vis[x]){
vis[x] = 1;
p = p->next;
}
else{
Lnode* r = p->next;
p->next = r->next;
free(r);
}
}
return L;
}
void sort_linklist(Linklist &L){
Lnode *p = L,*q = L;
while(q->next){
p = p->next;
q = q->next;
if(q->next) q = q->next;
}
q = p->next;
p->next = NULL;
Lnode *r;
while(q){
r = q->next;
q->next = p->next;
p->next = q;
q = r;
}
q = p->next;
p->next = NULL;
p = L->next;
while(q){
r = q->next;
q->next = p->next;
p->next = q;
p = q->next;
q = r;
}
}
int main(){