2023王道数据结构线性表--单链表课后习题部分代码

#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;
}
//1.
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);
}
//2.
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;
        }
    } 
}
//3.
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);
}
//4.
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);
}
//5.
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;
    }
}
//6.
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;
    }
}
//7.
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;
        }
        
    }
}
//8.找公共结点,即第一个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;
}
//9.
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;//pre最小值的前驱
            p = p->next;
        }
        Lnode* tmp = pre->next;
        printf("%d ",tmp->data);
        pre->next = tmp->next;
        free(tmp);
    }
    puts("");
}
//10.
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;
}
//11.
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;//插完a后插b
        if(p!=NULL){
            q = p->next;//记录p的后继
            p->next = b->next;
            b->next = p;
            p = q;
        }
    }
    a->next = NULL;
    return B;
}
//12.
void distinct(Linklist &L){
    if(L == NULL) return ;
    Lnode *q,*p = L->next;
    while(p->next!=NULL){
        q = p->next;//q记录p的后继结点
        if(p->data == q->data){
            p->next = q->next;
            free(q);
        }
        else p = p->next;
    }
}
//13.
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);
}
//14.
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;
}
//15.
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;
}
//16.带头结点
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;
}
//17.
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;
}
//18.
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;
}
//19.
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);
}
//20.
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;
}
//21.
Lnode* find_loop(Linklist L){
    /*
    快慢指针的思想:设置两个指针,快指针一次走两步,慢指针一次走一步。
    快指针和慢指针都从头结点开始,如果最后两个指针相遇,那么一定存在环
    为什么会相遇:当慢指针刚到环的入口的时候,快指针已经在环中,
    假设快指针离追上慢指针还差N格距离,由于每走一次,快指针与慢指针的距离会缩短一格,那么走N次即会相遇。
    */
    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;
}
//22.
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;
}
//23.
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;
}
//24.
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;
}
//25.
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(){
    // Linklist L;
    // init_list(L);
    //print(L);
   
    //1.
    //delet_x(L,3);
    //print(L);

    //2.
    //delet_x2(L,3);
    //print(L);
    
    //3.
    //print_head(L);

    //4.
    // delet_minx(L);
    // print(L);

    //5.
    // reverse_list2(L);
    // print(L);

    //6.
    // sort(L);
    // print(L);

    //7.
    // delet_x_y(L,1,3);
    // print(L);

    //8.

    //9.
    //sort_print(L);

    //10.
    // Linklist B = divid(L);
    // print(B);
    // print(L);

    //11.
    // Linklist B = divid2(L);
    // print(B);
    // print(L);

    //12.
    // distinct(L);
    // print(L);

    //13.
    // Linklist L1;
    // init_list(L1);
    // merge1(L,L1);
    // print(L);

    //14.
    // Linklist L1;
    // init_list(L1);
    // L = merge2(L,L1);
    // print(L);

    //15.
    // Linklist L1;
    // init_list(L1);
    // L = merge3(L,L1);
    // print(L);

    //16.
    // Linklist L1;
    // init_list1(L1,b);
    // cout<

    //17.
    // Dlinklist L1;
    // init_dlist(L1,a);
    // cout<

    //18.
    // Linklist L;
    // init_list2(L);

    //19.
    // Linklist L;
    // init_list2(L,a);
    // delet_min(L);

    //20.
    // Dlinklist L;
    // init_dlist1(L,a);
    // DLnode *p = Locate(L,2);
    // print_dlist1(L);

    //22.
    // Linklist L;
    // init_list1(L,a);
    // cout<
    
    //24.
    // Linklist L;
    // init_list1(L,a);
    // L = abs_distinct(L,30);
    // print(L);

    //25.
    Linklist L;
    init_list1(L,a);
    sort_linklist(L);
    print(L);
}

你可能感兴趣的:(数据结构)